Extract modules from roles

This commit is contained in:
Fabian Hauser 2020-11-28 22:14:34 +00:00
parent 120e6e66b5
commit 0a5c15105b
14 changed files with 9 additions and 22 deletions

View file

@ -0,0 +1,171 @@
{ config, lib, pkgs, ... }:
with lib;
let
routerCfg = config.services.router;
cfg = config.services.router.wireless;
in {
options.services.router.wireless = {
enable = mkEnableOption "router wireless service";
wleInterface24Ghz = mkOption {
type = with types; nullOr str;
default = null;
example = "wlp1";
description = ''
Wireless interface name for 2.4 GHz wireless band.
'';
};
wleInterface5Ghz = mkOption {
type = with types; nullOr str;
default = null;
example = "wlp2";
description = ''
Wireless interface name for 5 GHz wireless band.
'';
};
ssid = mkOption {
type = types.str;
example = "MyNetwork";
description = ''
Wireless network SSID.
'';
};
passphrase = mkOption {
type = types.str;
description = ''
Passphrase of wireless network. May be encrypted with <literal>wpa_passphrase &lt;wleSSID&gt; &lt;passphrase&gt;</literal>.
'';
};
regulatoryCountryCode = mkOption {
type = types.str;
default = "US";
description = ''
Regulatory wireless country code.
'';
};
};
#imports = mkIf cfg.enable [ ./hostapd5ghz.nix ];
imports = [ ./hostapd5ghz.nix ];
config = let
wle24GhzEnabled = cfg.wleInterface24Ghz != null;
wle5GhzEnabled = cfg.wleInterface5Ghz != null;
in mkIf cfg.enable {
boot.extraModprobeConfig = ''
options cfg80211 ieee80211_regdom=${cfg.regulatoryCountryCode}
'';
services.udev.packages = [ pkgs.crda ]; # TODO: Still required with 20.03?
services.hostapd5ghz = {
enable = wle5GhzEnabled;
interface = cfg.wleInterface5Ghz;
hwMode = "a";
ssid = cfg.ssid;
wpaPassphrase = cfg.passphrase;
channel = 36;
extraConfig = ''
max_num_sta=255
#Details for Connecting Clients via WPA2 TKIP
auth_algs=1
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
#802.11d Regulatory Restrictions Designations for Which Frequencies and Channels are Legal
ieee80211d=1
# DFS
#ieee80211h=1
country_code=${cfg.regulatoryCountryCode}
#802.11n Configurations
ieee80211n=1
#802.11ac Configurations
ieee80211ac=1
vht_oper_chwidth=1
vht_oper_centr_freq_seg0_idx=42
#How Many Units of Time Between Beacon Transmissions
#beacon_int=100
#Multiplier of How Many Units of Time Between Beacon Transmissions
#dtim_period=2
#(e.g. 100 milliseconds(ms) * 2 = 200 ms between beacons)
#QoS Type of Traffic Management Based on Traffic Type
wmm_enabled=1
##Background
#wmm_ac_bk_cwmin=4
#wmm_ac_bk_cwmax=10
#wmm_ac_bk_aifs=7
#wmm_ac_bk_txop_limit=0
#wmm_ac_bk_acm=0
##Best Effort
#wmm_ac_be_aifs=3
#wmm_ac_be_cwmin=4
#wmm_ac_be_cwmax=10
#wmm_ac_be_txop_limit=0
#wmm_ac_be_acm=0
##Video
#wmm_ac_vi_aifs=2
#wmm_ac_vi_cwmin=3
#wmm_ac_vi_cwmax=4
#wmm_ac_vi_txop_limit=94
#wmm_ac_vi_acm=0
##Voice
#wmm_ac_vo_aifs=2
#wmm_ac_vo_cwmin=2
#wmm_ac_vo_cwmax=3
#wmm_ac_vo_txop_limit=47
#wmm_ac_vo_acm=0
'';
};
services.hostapd = {
enable = wle24GhzEnabled;
interface = cfg.wleInterface24Ghz;
hwMode = "g";
ssid = cfg.ssid;
wpaPassphrase = cfg.passphrase;
channel = 6;
extraConfig = ''
#macaddr_acl sets options for mac address filtering. 0 means "accept unless in deny list"
macaddr_acl=0
#setting ignore_broadcast_ssid to 1 will disable the broadcasting of ssid
ignore_broadcast_ssid=0
#Sets authentication algorithm
#1 - only open system authentication
#2 - both open system authentication and shared key authentication
auth_algs=1
#####Sets WPA2 authentication#####
#sets wpa key management
wpa_key_mgmt=WPA-PSK
#sets encryption used by WPA
wpa_pairwise=TKIP
#sets encryption used by WPA2
rsn_pairwise=CCMP
#### even more options ####
wme_enabled=1
ieee80211n=1
'';
};
};
}

View file

@ -0,0 +1,217 @@
# Based on https://github.com/NixOS/nixpkgs/blob/ef0f57ff8ace1040fe9bc983bfe384352cbedcec/nixos/modules/services/networking/hostapd.nix
{ config, lib, pkgs, utils, ... }:
# TODO:
#
# asserts
# 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
with lib;
let
cfg = config.services.hostapd5ghz;
escapedInterface = utils.escapeSystemdPath cfg.interface;
configFile = pkgs.writeText "hostapd5ghz.conf" ''
interface=${cfg.interface}
driver=${cfg.driver}
ssid=${cfg.ssid}
hw_mode=${cfg.hwMode}
channel=${toString cfg.channel}
${optionalString (cfg.countryCode != null)
"country_code=${cfg.countryCode}"}
${optionalString (cfg.countryCode != null) "ieee80211d=1"}
# logging (debug level)
logger_syslog=-1
logger_syslog_level=${toString cfg.logLevel}
logger_stdout=-1
logger_stdout_level=${toString cfg.logLevel}
ctrl_interface=/run/hostapd
ctrl_interface_group=${cfg.group}
${optionalString cfg.wpa ''
wpa=2
wpa_passphrase=${cfg.wpaPassphrase}
''}
${optionalString cfg.noScan "noscan=1"}
${cfg.extraConfig}
'';
in {
###### interface
options = {
services.hostapd5ghz = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
Enable putting a wireless interface into infrastructure mode,
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).
'';
};
interface = mkOption {
default = "";
example = "wlp2s0";
description = ''
The interfaces <command>hostapd</command> will use.
'';
};
noScan = mkOption {
type = types.bool;
default = false;
description = ''
Do not scan for overlapping BSSs in HT40+/- mode.
Caution: turning this on will violate regulatory requirements!
'';
};
driver = mkOption {
default = "nl80211";
example = "hostapd";
type = types.str;
description = ''
Which driver <command>hostapd</command> will use.
Most applications will probably use the default.
'';
};
ssid = mkOption {
default = "nixos";
example = "mySpecialSSID";
type = types.str;
description = "SSID to be used in IEEE 802.11 management frames.";
};
hwMode = mkOption {
default = "g";
type = types.enum [ "a" "b" "g" ];
description = ''
Operation mode.
(a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11g).
'';
};
channel = mkOption {
default = 7;
example = 11;
type = types.int;
description = ''
Channel number (IEEE 802.11)
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>.
'';
};
group = mkOption {
default = "wheel";
example = "network";
type = types.str;
description = ''
Members of this group can control <command>hostapd</command>.
'';
};
wpa = mkOption {
type = types.bool;
default = true;
description = ''
Enable WPA (IEEE 802.11i/D3.0) to authenticate with the access point.
'';
};
wpaPassphrase = mkOption {
default = "my_sekret";
example = "any_64_char_string";
type = types.str;
description = ''
WPA-PSK (pre-shared-key) passphrase. Clients will need this
passphrase to associate with this access point.
Warning: This passphrase will get put into a world-readable file in
the Nix store!
'';
};
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
'';
};
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
(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.
'';
};
extraConfig = mkOption {
default = "";
example = ''
auth_algo=0
ieee80211n=1
ht_capab=[HT40-][SHORT-GI-40][DSSS_CCK-40]
'';
type = types.lines;
description = "Extra configuration options to put in hostapd.conf.";
};
};
};
###### implementation
config = mkIf cfg.enable {
environment.systemPackages = [ pkgs.hostapd ];
services.udev.packages = optional (cfg.countryCode != null) [ pkgs.crda ];
systemd.services.hostapd5ghz = {
description = "hostapd wireless AP for 5 GHz band";
path = [ pkgs.hostapd ];
after = [ "sys-subsystem-net-devices-${escapedInterface}.device" ];
bindsTo = [ "sys-subsystem-net-devices-${escapedInterface}.device" ];
requiredBy = [ "network-link-${cfg.interface}.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${pkgs.hostapd}/bin/hostapd ${configFile}";
Restart = "always";
};
};
};
}