@jaspermayone.com's dotfiles
1# Simple NetworkManager WiFi module (NixOS only)
2#
3# Provides a simpler way to declare wifi profiles with NetworkManager.
4# - Pass PSK via environment variable, direct value, or file
5# - Supports eduroam networks with the `eduroam = true` flag
6#
7# Example usage:
8# jsp.network.wifi = {
9# enable = true;
10# profiles = {
11# "MySSID" = { psk = "supersecret"; };
12# "eduroam" = {
13# eduroam = true;
14# identity = "user@university.edu";
15# psk = "password";
16# };
17# };
18# };
19
20{
21 lib,
22 config,
23 pkgs,
24 ...
25}:
26let
27 cfg = config.jsp.network.wifi;
28 mkProfile =
29 name:
30 {
31 pskVar ? null,
32 psk ? null,
33 pskFile ? null,
34 eduroam ? false,
35 identity ? null,
36 }:
37 let
38 base = {
39 connection = {
40 id = name;
41 type = "wifi";
42 };
43 ipv4.method = "auto";
44 ipv6 = {
45 addr-gen-mode = "stable-privacy";
46 method = "auto";
47 };
48 wifi = {
49 mode = "infrastructure";
50 ssid = name;
51 };
52 };
53 sec =
54 if eduroam then
55 if pskVar != null then
56 {
57 wifi-security = {
58 key-mgmt = "wpa-eap";
59 password = "$" + pskVar;
60 identity = identity;
61 phase2-auth = "mschapv2";
62 };
63 }
64 else if psk != null then
65 {
66 wifi-security = {
67 key-mgmt = "wpa-eap";
68 password = psk;
69 identity = identity;
70 phase2-auth = "mschapv2";
71 };
72 }
73 else if pskFile != null then
74 {
75 wifi-security = {
76 key-mgmt = "wpa-eap";
77 password = "$(" + pkgs.coreutils + "/bin/cat " + pskFile + ")";
78 identity = identity;
79 phase2-auth = "mschapv2";
80 };
81 }
82 else
83 { }
84 else if pskVar != null then
85 {
86 wifi-security = {
87 key-mgmt = "wpa-psk";
88 psk = "$" + pskVar;
89 };
90 }
91 else if psk != null then
92 {
93 wifi-security = {
94 key-mgmt = "wpa-psk";
95 psk = psk;
96 };
97 }
98 else if pskFile != null then
99 {
100 wifi-security = {
101 key-mgmt = "wpa-psk";
102 psk = "$(" + pkgs.coreutils + "/bin/cat " + pskFile + ")";
103 };
104 }
105 else
106 { };
107 in
108 base // sec;
109in
110{
111 options.jsp.network.wifi = {
112 enable = lib.mkEnableOption "NetworkManager with simplified Wi-Fi profiles";
113
114 hostName = lib.mkOption {
115 type = lib.types.str;
116 default = config.networking.hostName or "nixos";
117 description = "Hostname for the machine";
118 };
119
120 nameservers = lib.mkOption {
121 type = lib.types.listOf lib.types.str;
122 default = [ ];
123 description = "List of DNS nameservers";
124 };
125
126 envFile = lib.mkOption {
127 type = lib.types.nullOr lib.types.path;
128 default = null;
129 description = "Environment file with PSK variables";
130 };
131
132 profiles = lib.mkOption {
133 type = lib.types.attrsOf (
134 lib.types.submodule (
135 { name, ... }:
136 {
137 options = {
138 pskVar = lib.mkOption {
139 type = lib.types.nullOr lib.types.str;
140 default = null;
141 description = "Variable name in envFile providing PSK";
142 };
143 psk = lib.mkOption {
144 type = lib.types.nullOr lib.types.str;
145 default = null;
146 description = "WiFi password (plaintext - prefer pskVar or pskFile)";
147 };
148 pskFile = lib.mkOption {
149 type = lib.types.nullOr lib.types.path;
150 default = null;
151 description = "File containing the PSK";
152 };
153 eduroam = lib.mkOption {
154 type = lib.types.bool;
155 default = false;
156 description = "Enable eduroam configuration";
157 };
158 identity = lib.mkOption {
159 type = lib.types.nullOr lib.types.str;
160 default = null;
161 description = "Identity for eduroam authentication";
162 };
163 };
164 }
165 )
166 );
167 default = { };
168 description = "Map of SSID -> WiFi configuration";
169 };
170 };
171
172 config = lib.mkIf cfg.enable {
173 networking = {
174 hostName = lib.mkIf (cfg.hostName != "") cfg.hostName;
175 nameservers = lib.mkIf (cfg.nameservers != [ ]) cfg.nameservers;
176 useDHCP = false;
177 dhcpcd.enable = false;
178 networkmanager = {
179 enable = true;
180 dns = "none";
181 ensureProfiles = {
182 environmentFiles = lib.optional (cfg.envFile != null) cfg.envFile;
183 profiles = lib.mapAttrs mkProfile cfg.profiles;
184 };
185 };
186 };
187 };
188}