1{ config, lib, pkgs, utils, ... }:
2
3# TODO:
4#
5# asserts
6# ensure that the nl80211 module is loaded/compiled in the kernel
7# wpa_supplicant and hostapd on the same wireless interface doesn't make any sense
8
9with lib;
10
11let
12
13 cfg = config.services.hostapd;
14
15 escapedInterface = utils.escapeSystemdPath cfg.interface;
16
17 configFile = pkgs.writeText "hostapd.conf" ''
18 interface=${cfg.interface}
19 driver=${cfg.driver}
20 ssid=${cfg.ssid}
21 hw_mode=${cfg.hwMode}
22 channel=${toString cfg.channel}
23 ieee80211n=1
24 ieee80211ac=1
25 ${optionalString (cfg.countryCode != null) "country_code=${cfg.countryCode}"}
26 ${optionalString (cfg.countryCode != null) "ieee80211d=1"}
27
28 # logging (debug level)
29 logger_syslog=-1
30 logger_syslog_level=${toString cfg.logLevel}
31 logger_stdout=-1
32 logger_stdout_level=${toString cfg.logLevel}
33
34 ctrl_interface=/run/hostapd
35 ctrl_interface_group=${cfg.group}
36
37 ${optionalString cfg.wpa ''
38 wpa=2
39 wpa_pairwise=CCMP
40 wpa_passphrase=${cfg.wpaPassphrase}
41 ''}
42 ${optionalString cfg.noScan "noscan=1"}
43
44 ${cfg.extraConfig}
45 '' ;
46
47in
48
49{
50 ###### interface
51
52 options = {
53
54 services.hostapd = {
55
56 enable = mkOption {
57 type = types.bool;
58 default = false;
59 description = lib.mdDoc ''
60 Enable putting a wireless interface into infrastructure mode,
61 allowing other wireless devices to associate with the wireless
62 interface and do wireless networking. A simple access point will
63 {option}`enable hostapd.wpa`,
64 {option}`hostapd.wpaPassphrase`, and
65 {option}`hostapd.ssid`, as well as DHCP on the wireless
66 interface to provide IP addresses to the associated stations, and
67 NAT (from the wireless interface to an upstream interface).
68 '';
69 };
70
71 interface = mkOption {
72 example = "wlp2s0";
73 type = types.str;
74 description = lib.mdDoc ''
75 The interfaces {command}`hostapd` will use.
76 '';
77 };
78
79 noScan = mkOption {
80 type = types.bool;
81 default = false;
82 description = lib.mdDoc ''
83 Do not scan for overlapping BSSs in HT40+/- mode.
84 Caution: turning this on will violate regulatory requirements!
85 '';
86 };
87
88 driver = mkOption {
89 default = "nl80211";
90 example = "hostapd";
91 type = types.str;
92 description = lib.mdDoc ''
93 Which driver {command}`hostapd` will use.
94 Most applications will probably use the default.
95 '';
96 };
97
98 ssid = mkOption {
99 default = config.system.nixos.distroId;
100 defaultText = literalExpression "config.system.nixos.distroId";
101 example = "mySpecialSSID";
102 type = types.str;
103 description = lib.mdDoc "SSID to be used in IEEE 802.11 management frames.";
104 };
105
106 hwMode = mkOption {
107 default = "g";
108 type = types.enum [ "a" "b" "g" ];
109 description = lib.mdDoc ''
110 Operation mode.
111 (a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11g).
112 '';
113 };
114
115 channel = mkOption {
116 default = 7;
117 example = 11;
118 type = types.int;
119 description = lib.mdDoc ''
120 Channel number (IEEE 802.11)
121 Please note that some drivers do not use this value from
122 {command}`hostapd` and the channel will need to be configured
123 separately with {command}`iwconfig`.
124 '';
125 };
126
127 group = mkOption {
128 default = "wheel";
129 example = "network";
130 type = types.str;
131 description = lib.mdDoc ''
132 Members of this group can control {command}`hostapd`.
133 '';
134 };
135
136 wpa = mkOption {
137 type = types.bool;
138 default = true;
139 description = lib.mdDoc ''
140 Enable WPA (IEEE 802.11i/D3.0) to authenticate with the access point.
141 '';
142 };
143
144 wpaPassphrase = mkOption {
145 default = "my_sekret";
146 example = "any_64_char_string";
147 type = types.str;
148 description = lib.mdDoc ''
149 WPA-PSK (pre-shared-key) passphrase. Clients will need this
150 passphrase to associate with this access point.
151 Warning: This passphrase will get put into a world-readable file in
152 the Nix store!
153 '';
154 };
155
156 logLevel = mkOption {
157 default = 2;
158 type = types.int;
159 description = lib.mdDoc ''
160 Levels (minimum value for logged events):
161 0 = verbose debugging
162 1 = debugging
163 2 = informational messages
164 3 = notification
165 4 = warning
166 '';
167 };
168
169 countryCode = mkOption {
170 default = null;
171 example = "US";
172 type = with types; nullOr str;
173 description = lib.mdDoc ''
174 Country code (ISO/IEC 3166-1). Used to set regulatory domain.
175 Set as needed to indicate country in which device is operating.
176 This can limit available channels and transmit power.
177 These two octets are used as the first two octets of the Country String
178 (dot11CountryString).
179 If set this enables IEEE 802.11d. This advertises the countryCode and
180 the set of allowed channels and transmit power levels based on the
181 regulatory limits.
182 '';
183 };
184
185 extraConfig = mkOption {
186 default = "";
187 example = ''
188 auth_algo=0
189 ieee80211n=1
190 ht_capab=[HT40-][SHORT-GI-40][DSSS_CCK-40]
191 '';
192 type = types.lines;
193 description = lib.mdDoc "Extra configuration options to put in hostapd.conf.";
194 };
195 };
196 };
197
198
199 ###### implementation
200
201 config = mkIf cfg.enable {
202
203 environment.systemPackages = [ pkgs.hostapd ];
204
205 services.udev.packages = optionals (cfg.countryCode != null) [ pkgs.crda ];
206
207 systemd.services.hostapd =
208 { description = "hostapd wireless AP";
209
210 path = [ pkgs.hostapd ];
211 after = [ "sys-subsystem-net-devices-${escapedInterface}.device" ];
212 bindsTo = [ "sys-subsystem-net-devices-${escapedInterface}.device" ];
213 requiredBy = [ "network-link-${cfg.interface}.service" ];
214 wantedBy = [ "multi-user.target" ];
215
216 serviceConfig =
217 { ExecStart = "${pkgs.hostapd}/bin/hostapd ${configFile}";
218 Restart = "always";
219 };
220 };
221 };
222}