2019-04-12 17:17:19 +02:00
|
|
|
{ config, lib, pkgs, utils, ... }:
|
2012-10-06 06:39:56 +02:00
|
|
|
|
|
|
|
# TODO:
|
|
|
|
#
|
2015-12-26 11:35:56 +01:00
|
|
|
# asserts
|
2012-10-06 06:39:56 +02:00
|
|
|
# ensure that the nl80211 module is loaded/compiled in the kernel
|
|
|
|
# wpa_supplicant and hostapd on the same wireless interface doesn't make any sense
|
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2012-10-06 06:39:56 +02:00
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.hostapd;
|
2015-12-26 11:35:56 +01:00
|
|
|
|
2019-04-12 17:17:19 +02:00
|
|
|
escapedInterface = utils.escapeSystemdPath cfg.interface;
|
|
|
|
|
2015-12-26 11:35:56 +01:00
|
|
|
configFile = pkgs.writeText "hostapd.conf" ''
|
2012-10-06 06:39:56 +02:00
|
|
|
interface=${cfg.interface}
|
|
|
|
driver=${cfg.driver}
|
|
|
|
ssid=${cfg.ssid}
|
|
|
|
hw_mode=${cfg.hwMode}
|
|
|
|
channel=${toString cfg.channel}
|
2020-05-04 03:38:43 +02:00
|
|
|
${optionalString (cfg.countryCode != null) ''country_code=${cfg.countryCode}''}
|
2020-05-05 02:28:56 +02:00
|
|
|
${optionalString (cfg.countryCode != null) ''ieee80211d=1''}
|
2012-10-06 06:39:56 +02:00
|
|
|
|
|
|
|
# logging (debug level)
|
|
|
|
logger_syslog=-1
|
2020-05-04 03:39:14 +02:00
|
|
|
logger_syslog_level=${toString cfg.logLevel}
|
2012-10-06 06:39:56 +02:00
|
|
|
logger_stdout=-1
|
2020-05-04 03:39:14 +02:00
|
|
|
logger_stdout_level=${toString cfg.logLevel}
|
2012-10-06 06:39:56 +02:00
|
|
|
|
2018-12-19 22:39:39 +01:00
|
|
|
ctrl_interface=/run/hostapd
|
2012-10-06 06:39:56 +02:00
|
|
|
ctrl_interface_group=${cfg.group}
|
|
|
|
|
2019-06-23 21:33:14 +02:00
|
|
|
${optionalString cfg.wpa ''
|
2018-06-28 07:35:50 +02:00
|
|
|
wpa=2
|
2012-10-06 06:39:56 +02:00
|
|
|
wpa_passphrase=${cfg.wpaPassphrase}
|
2019-06-23 21:33:14 +02:00
|
|
|
''}
|
|
|
|
${optionalString cfg.noScan "noscan=1"}
|
2012-10-06 06:39:56 +02:00
|
|
|
|
2015-12-26 11:35:56 +01:00
|
|
|
${cfg.extraConfig}
|
|
|
|
'' ;
|
2012-10-06 06:39:56 +02:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.hostapd = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
2020-04-20 20:05:26 +02:00
|
|
|
type = types.bool;
|
2012-10-06 06:39:56 +02:00
|
|
|
default = false;
|
|
|
|
description = ''
|
2012-10-09 21:19:09 +02:00
|
|
|
Enable putting a wireless interface into infrastructure mode,
|
2015-12-05 11:54:27 +01:00
|
|
|
allowing other wireless devices to associate with the wireless
|
|
|
|
interface and do wireless networking. A simple access point will
|
|
|
|
<option>enable hostapd.wpa</option>,
|
|
|
|
<option>hostapd.wpaPassphrase</option>, and
|
|
|
|
<option>hostapd.ssid</option>, as well as DHCP on the wireless
|
|
|
|
interface to provide IP addresses to the associated stations, and
|
|
|
|
NAT (from the wireless interface to an upstream interface).
|
2012-10-06 06:39:56 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
interface = mkOption {
|
|
|
|
default = "";
|
2015-12-26 11:35:56 +01:00
|
|
|
example = "wlp2s0";
|
2012-10-06 06:39:56 +02:00
|
|
|
description = ''
|
2015-12-26 11:35:56 +01:00
|
|
|
The interfaces <command>hostapd</command> will use.
|
2012-10-06 06:39:56 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-06-23 21:33:14 +02:00
|
|
|
noScan = mkOption {
|
2020-04-27 09:04:07 +02:00
|
|
|
type = types.bool;
|
2019-06-23 21:33:14 +02:00
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Do not scan for overlapping BSSs in HT40+/- mode.
|
|
|
|
Caution: turning this on will violate regulatory requirements!
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2012-10-06 06:39:56 +02:00
|
|
|
driver = mkOption {
|
|
|
|
default = "nl80211";
|
|
|
|
example = "hostapd";
|
2019-08-08 22:48:27 +02:00
|
|
|
type = types.str;
|
2015-12-05 11:54:27 +01:00
|
|
|
description = ''
|
|
|
|
Which driver <command>hostapd</command> will use.
|
|
|
|
Most applications will probably use the default.
|
|
|
|
'';
|
2012-10-06 06:39:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ssid = mkOption {
|
|
|
|
default = "nixos";
|
|
|
|
example = "mySpecialSSID";
|
2019-08-08 22:48:27 +02:00
|
|
|
type = types.str;
|
2012-10-06 06:39:56 +02:00
|
|
|
description = "SSID to be used in IEEE 802.11 management frames.";
|
|
|
|
};
|
|
|
|
|
|
|
|
hwMode = mkOption {
|
2015-12-26 11:35:56 +01:00
|
|
|
default = "g";
|
2016-11-16 14:36:26 +01:00
|
|
|
type = types.enum [ "a" "b" "g" ];
|
2015-12-05 11:54:27 +01:00
|
|
|
description = ''
|
|
|
|
Operation mode.
|
|
|
|
(a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11g).
|
|
|
|
'';
|
2012-10-06 06:39:56 +02:00
|
|
|
};
|
|
|
|
|
2015-12-26 11:35:56 +01:00
|
|
|
channel = mkOption {
|
2012-10-06 06:39:56 +02:00
|
|
|
default = 7;
|
|
|
|
example = 11;
|
|
|
|
type = types.int;
|
2015-12-26 11:35:56 +01:00
|
|
|
description = ''
|
2012-10-06 06:39:56 +02:00
|
|
|
Channel number (IEEE 802.11)
|
2015-12-05 11:54:27 +01:00
|
|
|
Please note that some drivers do not use this value from
|
|
|
|
<command>hostapd</command> and the channel will need to be configured
|
|
|
|
separately with <command>iwconfig</command>.
|
2015-12-26 11:35:56 +01:00
|
|
|
'';
|
2012-10-06 06:39:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
default = "wheel";
|
|
|
|
example = "network";
|
2019-08-08 22:48:27 +02:00
|
|
|
type = types.str;
|
2015-12-05 11:54:27 +01:00
|
|
|
description = ''
|
|
|
|
Members of this group can control <command>hostapd</command>.
|
|
|
|
'';
|
2012-10-09 21:19:09 +02:00
|
|
|
};
|
2012-10-06 06:39:56 +02:00
|
|
|
|
|
|
|
wpa = mkOption {
|
2020-04-27 09:04:07 +02:00
|
|
|
type = types.bool;
|
2012-10-06 06:39:56 +02:00
|
|
|
default = true;
|
2015-12-05 11:54:27 +01:00
|
|
|
description = ''
|
|
|
|
Enable WPA (IEEE 802.11i/D3.0) to authenticate with the access point.
|
|
|
|
'';
|
2012-10-09 21:19:09 +02:00
|
|
|
};
|
2012-10-06 06:39:56 +02:00
|
|
|
|
|
|
|
wpaPassphrase = mkOption {
|
|
|
|
default = "my_sekret";
|
|
|
|
example = "any_64_char_string";
|
2019-08-08 22:48:27 +02:00
|
|
|
type = types.str;
|
2015-12-26 11:35:56 +01:00
|
|
|
description = ''
|
2012-10-09 21:19:09 +02:00
|
|
|
WPA-PSK (pre-shared-key) passphrase. Clients will need this
|
2015-12-05 11:54:27 +01:00
|
|
|
passphrase to associate with this access point.
|
|
|
|
Warning: This passphrase will get put into a world-readable file in
|
|
|
|
the Nix store!
|
2015-12-26 11:35:56 +01:00
|
|
|
'';
|
2012-10-09 21:19:09 +02:00
|
|
|
};
|
2012-10-06 06:39:56 +02:00
|
|
|
|
2020-05-04 03:39:14 +02:00
|
|
|
logLevel = mkOption {
|
|
|
|
default = 2;
|
|
|
|
type = types.int;
|
|
|
|
description = ''
|
|
|
|
Levels (minimum value for logged events):
|
|
|
|
0 = verbose debugging
|
|
|
|
1 = debugging
|
|
|
|
2 = informational messages
|
|
|
|
3 = notification
|
|
|
|
4 = warning
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-05-04 03:38:43 +02:00
|
|
|
countryCode = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "US";
|
|
|
|
type = with types; nullOr str;
|
|
|
|
description = ''
|
|
|
|
Country code (ISO/IEC 3166-1). Used to set regulatory domain.
|
|
|
|
Set as needed to indicate country in which device is operating.
|
|
|
|
This can limit available channels and transmit power.
|
|
|
|
These two octets are used as the first two octets of the Country String
|
2020-05-05 02:28:56 +02:00
|
|
|
(dot11CountryString).
|
|
|
|
If set this enables IEEE 802.11d. This advertises the countryCode and
|
|
|
|
the set of allowed channels and transmit power levels based on the
|
|
|
|
regulatory limits.
|
2020-05-04 03:38:43 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-12-26 11:35:56 +01:00
|
|
|
extraConfig = mkOption {
|
2012-10-06 06:39:56 +02:00
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
auth_algo=0
|
|
|
|
ieee80211n=1
|
|
|
|
ht_capab=[HT40-][SHORT-GI-40][DSSS_CCK-40]
|
|
|
|
'';
|
2016-10-23 19:33:41 +02:00
|
|
|
type = types.lines;
|
2015-12-05 11:54:27 +01:00
|
|
|
description = "Extra configuration options to put in hostapd.conf.";
|
2012-10-06 06:39:56 +02:00
|
|
|
};
|
|
|
|
};
|
2012-10-09 21:19:09 +02:00
|
|
|
};
|
2012-10-06 06:39:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
environment.systemPackages = [ pkgs.hostapd ];
|
|
|
|
|
2020-05-04 03:38:43 +02:00
|
|
|
services.udev.packages = optional (cfg.countryCode != null) [ pkgs.crda ];
|
2020-05-04 01:42:18 +02:00
|
|
|
|
2013-02-24 12:11:45 +01:00
|
|
|
systemd.services.hostapd =
|
|
|
|
{ description = "hostapd wireless AP";
|
|
|
|
|
2015-12-26 11:35:56 +01:00
|
|
|
path = [ pkgs.hostapd ];
|
2019-04-12 17:17:19 +02:00
|
|
|
after = [ "sys-subsystem-net-devices-${escapedInterface}.device" ];
|
|
|
|
bindsTo = [ "sys-subsystem-net-devices-${escapedInterface}.device" ];
|
2018-10-17 09:18:52 +02:00
|
|
|
requiredBy = [ "network-link-${cfg.interface}.service" ];
|
2019-06-23 21:30:51 +02:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2013-02-24 12:11:45 +01:00
|
|
|
|
2015-12-26 11:35:56 +01:00
|
|
|
serviceConfig =
|
2013-02-24 12:11:45 +01:00
|
|
|
{ ExecStart = "${pkgs.hostapd}/bin/hostapd ${configFile}";
|
|
|
|
Restart = "always";
|
|
|
|
};
|
2012-10-06 06:39:56 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|