git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #12015 from mayflower/wpa_supplicant-service

Basic Declaritive Network Configuration in wpa_supplicant Service

commit94e6323de09f028bbf9186272cc00b6b5b6521a0 authorPeter Simons <simons@cryp.to> dateTue 5 Jan 2016, 10:53 (11 years ago) parentb0548514bf7afc
2 files changed+87−87
nixos/doc/manual/configuration/wireless.xml+12−2 nixos/modules/services/networking/wpa_supplicant.nix+75−85
nixos/doc/manual/configuration/wireless.xml+12−2
@@ -18,8 +18,18 @@ NixOS will start wpa_supplicant for you if you enable this setting:
1818networking.wireless.enable = true;
1919</programlisting>
2020
21−NixOS currently does not generate wpa_supplicant's
22−configuration file, <literal>/etc/wpa_supplicant.conf</literal>. You should edit this file
21+NixOS lets you specify networks for wpa_supplicant declaratively:
22+<programlisting>
23+networking.wireless.networks = {
24+ echelon = {
25+ psk = "abcdefgh";
26+ };
27+ "free.wifi" = {};
28+}
29+</programlisting>
30+
31+When no networks are set it will default to using a configuration file at
32+<literal>/etc/wpa_supplicant.conf</literal>. You should edit this file
2333yourself to define wireless networks, WPA keys and so on (see
2434wpa_supplicant.conf(5)).
2535</para>
nixos/modules/services/networking/wpa_supplicant.nix+75−85
@@ -3,51 +3,30 @@
33with lib;
44
55let
6−
76 cfg = config.networking.wireless;
8− configFile = "/etc/wpa_supplicant.conf";
9−
10− ifaces =
11− cfg.interfaces ++
12− optional (config.networking.WLANInterface != "") config.networking.WLANInterface;
13−
14−in
15−
16−{
17−
18− ###### interface
19−
7+ configFile = if cfg.networks != {} then pkgs.writeText "wpa_supplicant.conf" ''
8+ ${optionalString cfg.userControlled.enable ''
9+ ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=${cfg.userControlled.group}
10+ update_config=1''}
11+ ${concatStringsSep "\n" (mapAttrsToList (ssid: networkConfig: ''
12+ network={
13+ ssid="${ssid}"
14+ ${optionalString (networkConfig.psk != null) ''psk="${networkConfig.psk}"''}
15+ ${optionalString (networkConfig.psk == null) ''key_mgmt=NONE''}
16+ }
17+ '') cfg.networks)}
18+ '' else "/etc/wpa_supplicant.conf";
19+in {
2020 options = {
21−
22− networking.WLANInterface = mkOption {
23− default = "";
24− description = "Obsolete. Use <option>networking.wireless.interfaces</option> instead.";
25− };
26−
2721 networking.wireless = {
28− enable = mkOption {
29− type = types.bool;
30− default = false;
31− description = ''
32− Whether to start <command>wpa_supplicant</command> to scan for
33− and associate with wireless networks. Note: NixOS currently
34− does not manage <command>wpa_supplicant</command>'s
35− configuration file, <filename>${configFile}</filename>. You
36− should edit this file yourself to define wireless networks,
37− WPA keys and so on (see
38− <citerefentry><refentrytitle>wpa_supplicant.conf</refentrytitle>
39− <manvolnum>5</manvolnum></citerefentry>), or use
40− networking.wireless.userControlled.* to allow users to add entries
41− through <command>wpa_cli</command> and <command>wpa_gui</command>.
42− '';
43− };
22+ enable = mkEnableOption "wpa_supplicant";
4423
4524 interfaces = mkOption {
4625 type = types.listOf types.str;
4726 default = [];
4827 example = [ "wlan0" "wlan1" ];
4928 description = ''
50− The interfaces <command>wpa_supplicant</command> will use. If empty, it will
29+ The interfaces <command>wpa_supplicant</command> will use. If empty, it will
5130 automatically use all wireless interfaces.
5231 '';
5332 };
@@ -58,6 +37,34 @@ in
5837 description = "Force a specific wpa_supplicant driver.";
5938 };
6039
40+ networks = mkOption {
41+ type = types.attrsOf (types.submodule {
42+ options = {
43+ psk = mkOption {
44+ type = types.nullOr types.str;
45+ default = null;
46+ description = ''
47+ The network's pre-shared key in plaintext defaulting
48+ to being a network without any authentication.
49+ '';
50+ };
51+ };
52+ });
53+ description = ''
54+ The network definitions to automatically connect to when
55+ <command>wpa_supplicant</command> is running. If this
56+ parameter is left empty wpa_supplicant will use
57+ /etc/wpa_supplicant.conf as the configuration file.
58+ '';
59+ default = {};
60+ example = literalExample ''
61+ echelon = {
62+ psk = "abcdefgh";
63+ };
64+ "free.wifi" = {};
65+ '';
66+ };
67+
6168 userControlled = {
6269 enable = mkOption {
6370 type = types.bool;
@@ -68,10 +75,8 @@ in
6875 to depend on a large package such as NetworkManager just to pick nearby
6976 access points.
7077
71− When you want to use this, make sure ${configFile} doesn't exist.
72− It will be created for you.
73−
74− Currently it is also necessary to explicitly specify networking.wireless.interfaces.
78+ When using a declarative network specification you cannot persist any
79+ settings via wpa_gui or wpa_cli.
7580 '';
7681 };
7782
@@ -85,64 +90,49 @@ in
8590 };
8691 };
8792
93+ config = mkMerge [
94+ (mkIf cfg.enable {
95+ environment.systemPackages = [ pkgs.wpa_supplicant ];
8896
89− ###### implementation
90−
91− config = mkIf cfg.enable {
92−
93− environment.systemPackages = [ pkgs.wpa_supplicant ];
94−
95− services.dbus.packages = [ pkgs.wpa_supplicant ];
97+ services.dbus.packages = [ pkgs.wpa_supplicant ];
9698
97− # FIXME: start a separate wpa_supplicant instance per interface.
98− jobs.wpa_supplicant =
99− { description = "WPA Supplicant";
99+ # FIXME: start a separate wpa_supplicant instance per interface.
100+ systemd.services.wpa_supplicant = let
101+ ifaces = cfg.interfaces;
102+ in {
103+ description = "WPA Supplicant";
100104
101105 wantedBy = [ "network.target" ];
102106
103107 path = [ pkgs.wpa_supplicant ];
104108
105− preStart = ''
106− touch -a ${configFile}
107− chmod 600 ${configFile}
108− '' + optionalString cfg.userControlled.enable ''
109− if [ ! -s ${configFile} ]; then
110− echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=${cfg.userControlled.group}" >> ${configFile}
111− echo "update_config=1" >> ${configFile}
112− fi
109+ script = ''
110+ ${if ifaces == [] then ''
111+ for i in $(cd /sys/class/net && echo *); do
112+ DEVTYPE=
113+ source /sys/class/net/$i/uevent
114+ if [ "$DEVTYPE" = "wlan" -o -e /sys/class/net/$i/wireless ]; then
115+ ifaces="$ifaces''${ifaces:+ -N} -i$i"
116+ fi
117+ done
118+ '' else ''
119+ ifaces="${concatStringsSep " -N " (map (i: "-i${i}") ifaces)}"
120+ ''}
121+ exec wpa_supplicant -s -u -D${cfg.driver} -c ${configFile} $ifaces
113122 '';
114−
115− script =
116− ''
117− ${if ifaces == [] then ''
118− for i in $(cd /sys/class/net && echo *); do
119− DEVTYPE=
120− source /sys/class/net/$i/uevent
121− if [ "$DEVTYPE" = "wlan" -o -e /sys/class/net/$i/wireless ]; then
122− ifaces="$ifaces''${ifaces:+ -N} -i$i"
123− fi
124− done
125− '' else ''
126− ifaces="${concatStringsSep " -N " (map (i: "-i${i}") ifaces)}"
127− ''}
128− exec wpa_supplicant -s -u -D${cfg.driver} -c ${configFile} $ifaces
129− '';
130123 };
131124
132− powerManagement.resumeCommands =
133− ''
125+ powerManagement.resumeCommands = ''
134126 ${config.systemd.package}/bin/systemctl try-restart wpa_supplicant
135127 '';
136128
137− assertions = [{ assertion = !cfg.userControlled.enable || cfg.interfaces != [];
138− message = "user controlled wpa_supplicant needs explicit networking.wireless.interfaces";}];
139−
140− # Restart wpa_supplicant when a wlan device appears or disappears.
141− services.udev.extraRules =
142− ''
129+ # Restart wpa_supplicant when a wlan device appears or disappears.
130+ services.udev.extraRules = ''
143131 ACTION=="add|remove", SUBSYSTEM=="net", ENV{DEVTYPE}=="wlan", RUN+="${config.systemd.package}/bin/systemctl try-restart wpa_supplicant.service"
144132 '';
145−
146− };
147−
133+ })
134+ {
135+ meta.maintainers = with lib.maintainers; [ globin ];
136+ }
137+ ];
148138}