···
cfg = config.networking.wireless;
8
-
configFile = "/etc/wpa_supplicant.conf";
12
-
optional (config.networking.WLANInterface != "") config.networking.WLANInterface;
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}
11
+
${concatStringsSep "\n" (mapAttrsToList (ssid: networkConfig: ''
14
+
${optionalString (networkConfig.psk != null) ''psk="${networkConfig.psk}"''}
15
+
${optionalString (networkConfig.psk == null) ''key_mgmt=NONE''}
18
+
'' else "/etc/wpa_supplicant.conf";
22
-
networking.WLANInterface = mkOption {
24
-
description = "Obsolete. Use <option>networking.wireless.interfaces</option> instead.";
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>.
22
+
enable = mkEnableOption "wpa_supplicant";
type = types.listOf types.str;
example = [ "wlan0" "wlan1" ];
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
automatically use all wireless interfaces.
···
description = "Force a specific wpa_supplicant driver.";
40
+
networks = mkOption {
41
+
type = types.attrsOf (types.submodule {
44
+
type = types.nullOr types.str;
47
+
The network's pre-shared key in plaintext defaulting
48
+
to being a network without any authentication.
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.
60
+
example = literalExample ''
···
to depend on a large package such as NetworkManager just to pick nearby
71
-
When you want to use this, make sure ${configFile} doesn't exist.
72
-
It will be created for you.
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.
···
89
-
###### implementation
91
-
config = mkIf cfg.enable {
93
-
environment.systemPackages = [ pkgs.wpa_supplicant ];
95
+
environment.systemPackages = [ pkgs.wpa_supplicant ];
95
-
services.dbus.packages = [ pkgs.wpa_supplicant ];
97
+
services.dbus.packages = [ pkgs.wpa_supplicant ];
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;
103
+
description = "WPA Supplicant";
wantedBy = [ "network.target" ];
path = [ pkgs.wpa_supplicant ];
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}
110
+
${if ifaces == [] then ''
111
+
for i in $(cd /sys/class/net && echo *); do
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"
119
+
ifaces="${concatStringsSep " -N " (map (i: "-i${i}") ifaces)}"
121
+
exec wpa_supplicant -s -u -D${cfg.driver} -c ${configFile} $ifaces
117
-
${if ifaces == [] then ''
118
-
for i in $(cd /sys/class/net && echo *); do
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"
126
-
ifaces="${concatStringsSep " -N " (map (i: "-i${i}") ifaces)}"
128
-
exec wpa_supplicant -s -u -D${cfg.driver} -c ${configFile} $ifaces
132
-
powerManagement.resumeCommands =
125
+
powerManagement.resumeCommands = ''
${config.systemd.package}/bin/systemctl try-restart wpa_supplicant
137
-
assertions = [{ assertion = !cfg.userControlled.enable || cfg.interfaces != [];
138
-
message = "user controlled wpa_supplicant needs explicit networking.wireless.interfaces";}];
140
-
# Restart wpa_supplicant when a wlan device appears or disappears.
141
-
services.udev.extraRules =
129
+
# Restart wpa_supplicant when a wlan device appears or disappears.
130
+
services.udev.extraRules = ''
ACTION=="add|remove", SUBSYSTEM=="net", ENV{DEVTYPE}=="wlan", RUN+="${config.systemd.package}/bin/systemctl try-restart wpa_supplicant.service"
135
+
meta.maintainers = with lib.maintainers; [ globin ];