Kieran's opinionated (and probably slightly dumb) nix config
1# simple network manager
2#
3# This module provides a simpler way to declare wifi profiles with network manager.
4# - you can pass the PSK via environment variable, direct value, or file.
5# - profiles are defined in `atelier.network.wifi.profiles`.
6#
7# Example usage:
8# atelier.network.wifi = {
9# enable = true;
10# profiles = {
11# "MySSID" = { psk = "supersecret"; };
12# };
13# };
14
15{
16 lib,
17 config,
18 pkgs,
19 ...
20}:
21let
22 cfg = config.atelier.network.wifi;
23 mkProfile =
24 name:
25 {
26 pskVar ? null,
27 psk ? null,
28 pskFile ? null,
29 }:
30 let
31 base = {
32 connection = {
33 id = name;
34 type = "wifi";
35 };
36 ipv4.method = "auto";
37 ipv6 = {
38 addr-gen-mode = "stable-privacy";
39 method = "auto";
40 };
41 wifi = {
42 mode = "infrastructure";
43 ssid = name;
44 };
45 };
46 sec =
47 if pskVar != null then
48 {
49 wifi-security = {
50 key-mgmt = "wpa-psk";
51 psk = "$" + pskVar;
52 };
53 }
54 else if psk != null then
55 {
56 wifi-security = {
57 key-mgmt = "wpa-psk";
58 psk = psk;
59 };
60 }
61 else if pskFile != null then
62 {
63 wifi-security = {
64 key-mgmt = "wpa-psk";
65 psk = "$(" + pkgs.coreutils + "/bin/cat " + pskFile + ")";
66 };
67 }
68 else
69 { };
70 in
71 base // sec;
72in
73{
74 options.atelier.network.wifi = {
75 enable = lib.mkEnableOption "Enable NetworkManager with simplified Wi-Fi profiles";
76 hostName = lib.mkOption {
77 type = lib.types.str;
78 default = lib.mkDefault (config.networking.hostName or "nixos");
79 };
80 nameservers = lib.mkOption {
81 type = lib.types.listOf lib.types.str;
82 default = lib.mkDefault [ ];
83 };
84 envFile = lib.mkOption {
85 type = lib.types.nullOr lib.types.path;
86 default = null;
87 description = "Single environment file with PSK variables (used once).";
88 };
89
90 profiles = lib.mkOption {
91 type = lib.types.attrsOf (
92 lib.types.submodule (
93 { name, ... }:
94 {
95 options = {
96 pskVar = lib.mkOption {
97 type = lib.types.nullOr lib.types.str;
98 default = null;
99 description = "Variable name in envFile providing PSK";
100 };
101 psk = lib.mkOption {
102 type = lib.types.nullOr lib.types.str;
103 default = null;
104 };
105 pskFile = lib.mkOption {
106 type = lib.types.nullOr lib.types.path;
107 default = null;
108 };
109 };
110 }
111 )
112 );
113 default = { };
114 description = "Map of SSID -> { pskVar | psk | pskFile }.";
115 };
116 };
117
118 config = lib.mkIf cfg.enable {
119 networking = {
120 hostName = lib.mkIf (cfg.hostName != "") cfg.hostName;
121 nameservers = lib.mkIf (cfg.nameservers != [ ]) cfg.nameservers;
122 useDHCP = false;
123 dhcpcd.enable = false;
124 networkmanager = {
125 enable = true;
126 dns = "none";
127 ensureProfiles = {
128 environmentFiles = lib.optional (cfg.envFile != null) cfg.envFile;
129 profiles = lib.mapAttrs mkProfile cfg.profiles;
130 };
131 };
132 };
133 };
134}