2015-10-06 18:51:52 +02:00
|
|
|
{ config, lib, utils, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.networking.supplicant;
|
|
|
|
|
|
|
|
# We must escape interfaces due to the systemd interpretation
|
|
|
|
subsystemDevice = interface:
|
|
|
|
"sys-subsystem-net-devices-${utils.escapeSystemdPath interface}.device";
|
|
|
|
|
|
|
|
serviceName = iface: "supplicant-${if (iface=="WLAN") then "wlan@" else (
|
|
|
|
if (iface=="LAN") then "lan@" else (
|
|
|
|
if (iface=="DBUS") then "dbus"
|
2022-12-12 02:36:03 +01:00
|
|
|
else (replaceStrings [" "] ["-"] iface)))}";
|
2015-10-06 18:51:52 +02:00
|
|
|
|
|
|
|
# TODO: Use proper privilege separation for wpa_supplicant
|
|
|
|
supplicantService = iface: suppl:
|
|
|
|
let
|
|
|
|
deps = (if (iface=="WLAN"||iface=="LAN") then ["sys-subsystem-net-devices-%i.device"] else (
|
|
|
|
if (iface=="DBUS") then ["dbus.service"]
|
|
|
|
else (map subsystemDevice (splitString " " iface))))
|
|
|
|
++ optional (suppl.bridge!="") (subsystemDevice suppl.bridge);
|
|
|
|
|
|
|
|
ifaceArg = concatStringsSep " -N " (map (i: "-i${i}") (splitString " " iface));
|
|
|
|
driverArg = optionalString (suppl.driver != null) "-D${suppl.driver}";
|
|
|
|
bridgeArg = optionalString (suppl.bridge!="") "-b${suppl.bridge}";
|
|
|
|
confFileArg = optionalString (suppl.configFile.path!=null) "-c${suppl.configFile.path}";
|
2022-12-12 02:36:03 +01:00
|
|
|
extraConfFile = pkgs.writeText "supplicant-extra-conf-${replaceStrings [" "] ["-"] iface}" ''
|
2015-10-06 18:51:52 +02:00
|
|
|
${optionalString suppl.userControlled.enable "ctrl_interface=DIR=${suppl.userControlled.socketDir} GROUP=${suppl.userControlled.group}"}
|
|
|
|
${optionalString suppl.configFile.writable "update_config=1"}
|
|
|
|
${suppl.extraConf}
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{ description = "Supplicant ${iface}${optionalString (iface=="WLAN"||iface=="LAN") " %I"}";
|
2016-10-19 00:09:49 +02:00
|
|
|
wantedBy = [ "multi-user.target" ] ++ deps;
|
|
|
|
wants = [ "network.target" ];
|
2015-10-06 18:51:52 +02:00
|
|
|
bindsTo = deps;
|
|
|
|
after = deps;
|
|
|
|
before = [ "network.target" ];
|
|
|
|
|
|
|
|
path = [ pkgs.coreutils ];
|
|
|
|
|
|
|
|
preStart = ''
|
2021-11-16 13:54:34 +01:00
|
|
|
${optionalString (suppl.configFile.path!=null && suppl.configFile.writable) ''
|
2021-05-01 15:34:04 +02:00
|
|
|
(umask 077 && touch -a "${suppl.configFile.path}")
|
2015-10-06 18:51:52 +02:00
|
|
|
''}
|
|
|
|
${optionalString suppl.userControlled.enable ''
|
2021-05-01 15:34:04 +02:00
|
|
|
install -dm770 -g "${suppl.userControlled.group}" "${suppl.userControlled.socketDir}"
|
2015-10-06 18:51:52 +02:00
|
|
|
''}
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig.ExecStart = "${pkgs.wpa_supplicant}/bin/wpa_supplicant -s ${driverArg} ${confFileArg} -I${extraConfFile} ${bridgeArg} ${suppl.extraCmdArgs} ${if (iface=="WLAN"||iface=="LAN") then "-i%I" else (if (iface=="DBUS") then "-u" else ifaceArg)}";
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
networking.supplicant = mkOption {
|
2016-09-11 11:14:34 +02:00
|
|
|
type = with types; attrsOf (submodule {
|
|
|
|
options = {
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
configFile = {
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
path = mkOption {
|
2017-02-05 12:50:20 +01:00
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression "/etc/wpa_supplicant.conf";
|
2016-09-11 11:14:34 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
External `wpa_supplicant.conf` configuration file.
|
|
|
|
The configuration options defined declaratively within `networking.supplicant` have
|
|
|
|
precedence over options defined in `configFile`.
|
|
|
|
'';
|
|
|
|
};
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
writable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether the configuration file at `configFile.path` should be written to by
|
|
|
|
`wpa_supplicant`.
|
|
|
|
'';
|
|
|
|
};
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
};
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
extraConf = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
ap_scan=1
|
|
|
|
device_name=My-NixOS-Device
|
|
|
|
device_type=1-0050F204-1
|
|
|
|
driver_param=use_p2p_group_interface=1
|
|
|
|
disable_scan_offload=1
|
|
|
|
p2p_listen_reg_class=81
|
|
|
|
p2p_listen_channel=1
|
|
|
|
p2p_oper_reg_class=81
|
|
|
|
p2p_oper_channel=1
|
|
|
|
manufacturer=NixOS
|
|
|
|
model_name=NixOS_Unstable
|
|
|
|
model_number=2015
|
|
|
|
'';
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Configuration options for `wpa_supplicant.conf`.
|
|
|
|
Options defined here have precedence over options in `configFile`.
|
|
|
|
NOTE: Do not write sensitive data into `extraConf` as it will
|
|
|
|
be world-readable in the `nix-store`. For sensitive information
|
|
|
|
use the `configFile` instead.
|
|
|
|
'';
|
|
|
|
};
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
extraCmdArgs = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
2018-12-19 22:40:58 +01:00
|
|
|
example = "-e/run/wpa_supplicant/entropy.bin";
|
2016-09-11 11:14:34 +02:00
|
|
|
description =
|
|
|
|
lib.mdDoc "Command line arguments to add when executing `wpa_supplicant`.";
|
|
|
|
};
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
driver = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = "nl80211,wext";
|
|
|
|
description = lib.mdDoc "Force a specific wpa_supplicant driver.";
|
|
|
|
};
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
bridge = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
|
|
|
description = lib.mdDoc "Name of the bridge interface that wpa_supplicant should listen at.";
|
|
|
|
};
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
userControlled = {
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Allow normal users to control wpa_supplicant through wpa_gui or wpa_cli.
|
|
|
|
This is useful for laptop users that switch networks a lot and don't want
|
|
|
|
to depend on a large package such as NetworkManager just to pick nearby
|
|
|
|
access points.
|
|
|
|
'';
|
|
|
|
};
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
socketDir = mkOption {
|
|
|
|
type = types.str;
|
2018-12-19 22:40:58 +01:00
|
|
|
default = "/run/wpa_supplicant";
|
2016-09-11 11:14:34 +02:00
|
|
|
description = lib.mdDoc "Directory of sockets for controlling wpa_supplicant.";
|
|
|
|
};
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "wheel";
|
|
|
|
example = "network";
|
|
|
|
description = lib.mdDoc "Members of this group can control wpa_supplicant.";
|
|
|
|
};
|
2020-08-07 15:43:58 +02:00
|
|
|
|
2016-09-11 11:14:34 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2015-10-06 18:51:52 +02:00
|
|
|
|
|
|
|
default = { };
|
|
|
|
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''
|
2017-09-15 20:19:42 +02:00
|
|
|
{ "wlan0 wlan1" = {
|
2018-07-04 00:14:45 +02:00
|
|
|
configFile.path = "/etc/wpa_supplicant.conf";
|
2017-09-15 20:19:42 +02:00
|
|
|
userControlled.group = "network";
|
|
|
|
extraConf = '''
|
|
|
|
ap_scan=1
|
|
|
|
p2p_disabled=1
|
|
|
|
''';
|
|
|
|
extraCmdArgs = "-u -W";
|
|
|
|
bridge = "br0";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
2015-10-06 18:51:52 +02:00
|
|
|
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Interfaces for which to start {command}`wpa_supplicant`.
|
|
|
|
The supplicant is used to scan for and associate with wireless networks,
|
|
|
|
or to authenticate with 802.1x capable network switches.
|
|
|
|
|
|
|
|
The value of this option is an attribute set. Each attribute configures a
|
|
|
|
{command}`wpa_supplicant` service, where the attribute name specifies
|
|
|
|
the name of the interface that {command}`wpa_supplicant` operates on.
|
|
|
|
The attribute name can be a space separated list of interfaces.
|
|
|
|
The attribute names `WLAN`, `LAN` and `DBUS`
|
|
|
|
have a special meaning. `WLAN` and `LAN` are
|
|
|
|
configurations for universal {command}`wpa_supplicant` service that is
|
|
|
|
started for each WLAN interface or for each LAN interface, respectively.
|
|
|
|
`DBUS` defines a device-unrelated {command}`wpa_supplicant`
|
|
|
|
service that can be accessed through `D-Bus`.
|
|
|
|
'';
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf (cfg != {}) {
|
|
|
|
|
|
|
|
environment.systemPackages = [ pkgs.wpa_supplicant ];
|
|
|
|
|
|
|
|
services.dbus.packages = [ pkgs.wpa_supplicant ];
|
|
|
|
|
|
|
|
systemd.services = mapAttrs' (n: v: nameValuePair (serviceName n) (supplicantService n v)) cfg;
|
|
|
|
|
|
|
|
services.udev.packages = [
|
|
|
|
(pkgs.writeTextFile {
|
|
|
|
name = "99-zzz-60-supplicant.rules";
|
|
|
|
destination = "/etc/udev/rules.d/99-zzz-60-supplicant.rules";
|
|
|
|
text = ''
|
|
|
|
${flip (concatMapStringsSep "\n") (filter (n: n!="WLAN" && n!="LAN" && n!="DBUS") (attrNames cfg)) (iface:
|
|
|
|
flip (concatMapStringsSep "\n") (splitString " " iface) (i: ''
|
2022-12-12 02:36:03 +01:00
|
|
|
ACTION=="add", SUBSYSTEM=="net", ENV{INTERFACE}=="${i}", TAG+="systemd", ENV{SYSTEMD_WANTS}+="supplicant-${replaceStrings [" "] ["-"] iface}.service", TAG+="SUPPLICANT_ASSIGNED"''))}
|
2015-10-06 18:51:52 +02:00
|
|
|
|
|
|
|
${optionalString (hasAttr "WLAN" cfg) ''
|
2022-05-05 13:23:32 +02:00
|
|
|
ACTION=="add", SUBSYSTEM=="net", ENV{DEVTYPE}=="wlan", TAG!="SUPPLICANT_ASSIGNED", TAG+="systemd", PROGRAM="/run/current-system/systemd/bin/systemd-escape -p %E{INTERFACE}", ENV{SYSTEMD_WANTS}+="supplicant-wlan@$result.service"
|
2015-10-06 18:51:52 +02:00
|
|
|
''}
|
|
|
|
${optionalString (hasAttr "LAN" cfg) ''
|
2022-05-05 13:23:32 +02:00
|
|
|
ACTION=="add", SUBSYSTEM=="net", ENV{DEVTYPE}=="lan", TAG!="SUPPLICANT_ASSIGNED", TAG+="systemd", PROGRAM="/run/current-system/systemd/bin/systemd-escape -p %E{INTERFACE}", ENV{SYSTEMD_WANTS}+="supplicant-lan@$result.service"
|
2015-10-06 18:51:52 +02:00
|
|
|
''}
|
|
|
|
'';
|
|
|
|
})];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|