From fff7544eb65a7d83e0c790517d01ad076e52e6b0 Mon Sep 17 00:00:00 2001 From: "quentin@aristote.fr" Date: Thu, 4 Apr 2024 19:55:31 +0200 Subject: nixos: wifi: refactor --- modules/nixos/personal/networking/wifi.nix | 51 ++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 13 deletions(-) (limited to 'modules/nixos/personal/networking') diff --git a/modules/nixos/personal/networking/wifi.nix b/modules/nixos/personal/networking/wifi.nix index 22f41dd..ffe6b21 100644 --- a/modules/nixos/personal/networking/wifi.nix +++ b/modules/nixos/personal/networking/wifi.nix @@ -8,7 +8,7 @@ id, ssid, }: { - "${id}" = { + "${id}_template" = { connection = { id = "${id} (template)"; type = "wifi"; @@ -20,6 +20,9 @@ wifi-security = { key-mgmt = "wpa-psk"; # fill-in password on first connection + # this will create a new connection + # disable the personal.networking.wifi.enable option + # to keep it for next rebuild }; ipv4 = { method = "auto"; @@ -32,27 +35,49 @@ }; }; }; + knownSSIDs = { + home = "Quentintranet"; + home-iot = "Quentinternet of Things"; + home-guest = "Quentinvités"; + aristote = "ARISTOTE"; + aristote-4g = "ARISTOTE 4G"; + stvictoret = "ORBIWAN"; + montlaur = "Nordnet_E080"; + montlaur-5g = "Nordnet_E080_5G"; + }; in { options.personal.networking.wifi = { enable = lib.mkEnableOption "personal WiFi networks"; networks = lib.mkOption { + type = with lib.types; listOf str; + default = ["home-private" "hotspot"]; + }; + extraNetworks = lib.mkOption { type = with lib.types; listOf (attrsOf str); - default = [ - { - id = "home-private"; - ssid = "Quentintranet"; - } - { - id = "hotspot"; - ssid = "Quentinternational"; - } + default = []; + example = [ { - id = "home-cercier"; - ssid = "ARISTOTE"; + id = "my-wifi"; + ssid = "WiFi SSID"; } ]; }; }; - config.networking.networkmanager.ensureProfiles.profiles = lib.mkIf cfg.enable (lib.mergeAttrsList (builtins.map mkWifiProfile cfg.networks)); + config.networking.networkmanager.ensureProfiles.profiles = let + networks = + builtins.map (id: { + inherit id; + ssid = + if lib.hasAttr id knownSSIDs + then lib.getAttr id knownSSIDs + else throw "Unknown WiFi ID: ${id}"; + }) + cfg.networks + ++ cfg.extraNetworks; + profiles = lib.mergeAttrsList (builtins.map mkWifiProfile networks); + in + lib.mkIf + cfg.enable + profiles; } -- cgit v1.2.3