1{ config, lib, pkgs, ... }:
2
3# TODO:
4#
5# asserts
6# ensure that the nl80211 module is loaded/compiled in the kernel
7# hwMode must be a/b/g
8# channel must be between 1 and 13 (maybe)
9# wpa_supplicant and hostapd on the same wireless interface doesn't make any sense
10# perhaps an assertion that there is a dhcp server and a dns server on the IP address serviced by the hostapd?
11
12with lib;
13
14let
15
16 cfg = config.services.hostapd;
17
18 configFile = pkgs.writeText "hostapd.conf"
19 ''
20 interface=${cfg.interface}
21 driver=${cfg.driver}
22 ssid=${cfg.ssid}
23 hw_mode=${cfg.hwMode}
24 channel=${toString cfg.channel}
25
26 # logging (debug level)
27 logger_syslog=-1
28 logger_syslog_level=2
29 logger_stdout=-1
30 logger_stdout_level=2
31
32 ctrl_interface=/var/run/hostapd
33 ctrl_interface_group=${cfg.group}
34
35 ${if cfg.wpa then ''
36 wpa=1
37 wpa_passphrase=${cfg.wpaPassphrase}
38 '' else ""}
39
40 ${cfg.extraCfg}
41 '' ;
42
43in
44
45{
46 ###### interface
47
48 options = {
49
50 services.hostapd = {
51
52 enable = mkOption {
53 default = false;
54 description = ''
55 Enable putting a wireless interface into infrastructure mode,
56 allowing other wireless devices to associate with the wireless interface and do
57 wireless networking. A simple access point will enable hostapd.wpa, and
58 hostapd.wpa_passphrase, hostapd.ssid, dhcpd on the wireless interface to
59 provide IP addresses to the associated stations, and nat (from the wireless
60 interface to an upstream interface).
61 '';
62 };
63
64 interface = mkOption {
65 default = "";
66 example = "wlan0";
67 description = ''
68 The interfaces <command>hostapd</command> will use.
69 '';
70 };
71
72 driver = mkOption {
73 default = "nl80211";
74 example = "hostapd";
75 type = types.string;
76 description = "Which driver hostapd will use. Most things will probably use the default.";
77 };
78
79 ssid = mkOption {
80 default = "nixos";
81 example = "mySpecialSSID";
82 type = types.string;
83 description = "SSID to be used in IEEE 802.11 management frames.";
84 };
85
86 hwMode = mkOption {
87 default = "b";
88 example = "g";
89 type = types.string;
90 description = "Operation mode (a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11g";
91 };
92
93 channel = mkOption {
94 default = 7;
95 example = 11;
96 type = types.int;
97 description =
98 ''
99 Channel number (IEEE 802.11)
100 Please note that some drivers do not use this value from hostapd and the
101 channel will need to be configured separately with iwconfig.
102 '';
103 };
104
105 group = mkOption {
106 default = "wheel";
107 example = "network";
108 type = types.string;
109 description = "members of this group can control hostapd";
110 };
111
112 wpa = mkOption {
113 default = true;
114 description = "enable WPA (IEEE 802.11i/D3.0) to authenticate to the access point";
115 };
116
117 wpaPassphrase = mkOption {
118 default = "my_sekret";
119 example = "any_64_char_string";
120 type = types.string;
121 description =
122 ''
123 WPA-PSK (pre-shared-key) passphrase. Clients will need this
124 passphrase to associate with this access point. Warning: This passphrase will
125 get put into a world-readable file in the nix store.
126 '';
127 };
128
129 extraCfg = mkOption {
130 default = "";
131 example = ''
132 auth_algo=0
133 ieee80211n=1
134 ht_capab=[HT40-][SHORT-GI-40][DSSS_CCK-40]
135 '';
136 type = types.string;
137 description = "Extra configuration options to put in the hostapd.conf";
138 };
139 };
140 };
141
142
143 ###### implementation
144
145 config = mkIf cfg.enable {
146
147 environment.systemPackages = [ pkgs.hostapd ];
148
149 systemd.services.hostapd =
150 { description = "hostapd wireless AP";
151
152 path = [ pkgs.hostapd ];
153 wantedBy = [ "network.target" ];
154
155 after = [ "${cfg.interface}-cfg.service" "nat.service" "bind.service" "dhcpd.service"];
156
157 serviceConfig =
158 { ExecStart = "${pkgs.hostapd}/bin/hostapd ${configFile}";
159 Restart = "always";
160 };
161 };
162 };
163}