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