service.nix (1748B)
1 { 2 liminix, 3 svc, 4 hostapd, 5 output-template, 6 writeText, 7 lib, 8 }: 9 { interface, params }: 10 let 11 inherit (liminix.services) longrun; 12 inherit (lib) concatStringsSep mapAttrsToList unique; 13 inherit (builtins) 14 map 15 filter 16 attrValues 17 length 18 head 19 typeOf 20 ; 21 22 # This is not a friendly interface to configuring a wireless AP: it 23 # just passes everything straight through to the hostapd config. 24 # When we've worked out what the sensible options are to expose, 25 # we'll add them as top-level attributes and rename params to 26 # extraParams 27 28 name = "${interface.name}.hostapd"; 29 defaults = { 30 driver = "nl80211"; 31 logger_syslog = "-1"; 32 logger_syslog_level = 1; 33 ctrl_interface = "/run/${name}"; 34 ctrl_interface_group = 0; 35 }; 36 attrs = defaults // params; 37 literal_or_output = 38 o: 39 ( 40 { 41 string = builtins.toJSON; 42 int = builtins.toJSON; 43 lambda = (o: "output(${builtins.toJSON (o "service")}, ${builtins.toJSON (o "path")})"); 44 } 45 .${builtins.typeOf o} 46 ) 47 o; 48 49 conf = ( 50 writeText "hostapd.conf.in" ( 51 (concatStringsSep "\n" (mapAttrsToList (n: v: "${n}={{ ${literal_or_output v} }}") attrs)) + "\n" 52 ) 53 ); 54 service = longrun { 55 inherit name; 56 dependencies = [ interface ]; 57 run = '' 58 mkdir -p /run/${name} 59 chmod 0700 /run/${name} 60 ${output-template}/bin/output-template '{{' '}}' < ${conf} > /run/${name}/hostapd.conf 61 exec ${hostapd}/bin/hostapd -i $(output ${interface} ifname) -P /run/${name}/hostapd.pid -S /run/${name}/hostapd.conf 62 ''; 63 }; 64 watch = filter (f: typeOf f == "lambda") (attrValues attrs); 65 in 66 svc.secrets.subscriber.build { 67 inherit service watch; 68 action = "restart-all"; 69 }