1
1
Fork 1
mirror of https://github.com/oddlama/nix-config.git synced 2025-10-11 07:10:39 +02:00

feat: new hostapd module v0.5 done (stitching, categorization missing)

This commit is contained in:
oddlama 2023-03-19 02:01:50 +01:00
parent d6ade5eb5c
commit 9c5aaef805
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A
2 changed files with 25 additions and 27 deletions

View file

@ -5,6 +5,7 @@
... ...
}: { }: {
imports = [../../modules/hostapd.nix]; imports = [../../modules/hostapd.nix];
disabledModules = ["services/networking/hostapd.nix"];
# Associates each known client to a unique password # Associates each known client to a unique password
rekey.secrets.wifi-clients.file = ./secrets/wifi-clients.age; rekey.secrets.wifi-clients.file = ./secrets/wifi-clients.age;

View file

@ -8,38 +8,32 @@
with lib; let with lib; let
# TODO: add multi AP support (aka EasyMesh(TM)) # TODO: add multi AP support (aka EasyMesh(TM))
# TODO DFS as separate setting ? # TODO DFS as separate setting ?
disabledModules = ["services/networking/hostapd.nix"];
cfg = config.services.hostapd; cfg = config.services.hostapd;
# Escapes a string as hex (hello -> 68656c6c6f)
escapeHex = s: toLower (stringAsChars (x: toHexString (strings.charToInt x)) s);
# Maps the specified acl mode to values understood by hostapd # Maps the specified acl mode to values understood by hostapd
macaddrAclModes = { macaddrAclModes = {
"allow" = 0; "allow" = "0";
"deny" = 1; "deny" = "1";
"radius" = 2; "radius" = "2";
}; };
# Maps the specified ignore broadcast ssid mode to values understood by hostapd # Maps the specified ignore broadcast ssid mode to values understood by hostapd
ignoreBroadcastSsidModes = { ignoreBroadcastSsidModes = {
"disabled" = 0; "disabled" = "0";
"empty" = 1; "empty" = "1";
"clear" = 2; "clear" = "2";
}; };
# Maps the specified vht and he channel widths to values understood by hostapd # Maps the specified vht and he channel widths to values understood by hostapd
operatingChannelWidth = { operatingChannelWidth = {
"20or40" = 0; "20or40" = "0";
"80" = 1; "80" = "1";
"160" = 2; "160" = "2";
"80+80" = 3; "80+80" = "3";
}; };
configFileForInterface = interface: let configFileForInterface = interface: ifcfg: let
ifcfg = cfg.interfaces.${interface};
escapedInterface = utils.escapeSystemdPath interface; escapedInterface = utils.escapeSystemdPath interface;
hasMacAllowList = count ifcfg.macAllow > 0 || ifcfg.macAllowFile != null; hasMacAllowList = length ifcfg.macAllow > 0 || ifcfg.macAllowFile != null;
hasMacDenyList = count ifcfg.macDeny > 0 || ifcfg.macDenyFile != null; hasMacDenyList = length ifcfg.macDeny > 0 || ifcfg.macDenyFile != null;
bool01 = b: bool01 = b:
if b if b
then "1" then "1"
@ -58,7 +52,7 @@ with lib; let
##### IEEE 802.11 related configuration ####################################### ##### IEEE 802.11 related configuration #######################################
ssid2=${escapeHex ifcfg.ssid} ssid=${ifcfg.ssid}
utf8_ssid=${ifcfg.hwMode} utf8_ssid=${ifcfg.hwMode}
${optionalString (ifcfg.countryCode != null) '' ${optionalString (ifcfg.countryCode != null) ''
country_code=${ifcfg.countryCode} country_code=${ifcfg.countryCode}
@ -136,6 +130,8 @@ with lib; let
${ifcfg.extraConfig} ${ifcfg.extraConfig}
''; '';
configFiles = mapAttrsToList configFileForInterface cfg.interfaces;
in { in {
options = { options = {
services.hostapd = { services.hostapd = {
@ -152,6 +148,7 @@ in {
interfaces = mkOption { interfaces = mkOption {
default = {}; default = {};
# TODO
example = literalExpression '' example = literalExpression ''
{ {
# WiFi 4 - 2.4GHz # WiFi 4 - 2.4GHz
@ -376,7 +373,7 @@ in {
multi_ap=1 multi_ap=1
''; '';
type = types.lines; type = types.lines;
description = mdDoc "Extra configuration options to put in hostapd.conf."; description = mdDoc "Extra configuration options to put at the end of this interface's hostapd.conf.";
}; };
#### IEEE 802.11n (WiFi 4) related configuration #### IEEE 802.11n (WiFi 4) related configuration
@ -558,22 +555,22 @@ in {
config = mkIf cfg.enable { config = mkIf cfg.enable {
assertions = [ assertions = [
{ {
assertion = count cfg.interfaces > 0; assertion = length (attrNames cfg.interfaces) > 0;
message = "At least one interface must be configured with hostapd!"; message = "At least one interface must be configured with hostapd!";
} }
]; ];
environment.systemPackages = [pkgs.hostapd]; environment.systemPackages = [pkgs.hostapd];
services.udev.packages = optionals (cfg.countryCode != null) [pkgs.crda]; services.udev.packages = optionals (any (i: i.countryCode != null) (attrValues cfg.interfaces)) [pkgs.crda];
systemd.services.hostapd = { systemd.services.hostapd = {
description = "hostapd wireless AP"; description = "hostapd wireless AP";
path = [pkgs.hostapd]; path = [pkgs.hostapd];
after = ["sys-subsystem-net-devices-${escapedInterface}.device"]; after = mapAttrsToList (interface: _: "sys-subsystem-net-devices-${utils.escapeSystemdPath interface}.device") cfg.interfaces;
bindsTo = ["sys-subsystem-net-devices-${escapedInterface}.device"]; bindsTo = mapAttrsToList (interface: _: "sys-subsystem-net-devices-${utils.escapeSystemdPath interface}.device") cfg.interfaces;
requiredBy = ["network-link-${cfg.interface}.service"]; requiredBy = mapAttrsToList (interface: _: "network-link-${interface}.service") cfg.interfaces;
wantedBy = ["multi-user.target"]; wantedBy = ["multi-user.target"];
preStart = mkBefore '' preStart = mkBefore ''
@ -585,7 +582,7 @@ in {
''; '';
serviceConfig = { serviceConfig = {
ExecStart = "${pkgs.hostapd}/bin/hostapd ${configFile}"; ExecStart = "${pkgs.hostapd}/bin/hostapd ${concatStringsSep " " configFiles}";
Restart = "always"; Restart = "always";
ExecReload = "/bin/kill -HUP $MAINPID"; ExecReload = "/bin/kill -HUP $MAINPID";
RuntimeDirectory = "hostapd"; RuntimeDirectory = "hostapd";