|
7 | 7 |
|
8 | 8 | hostnameRegEx = ''^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$'';
|
9 | 9 |
|
10 |
| - emptyList = lst: if lst != [] then lst else ["empty"]; |
| 10 | + emptyList = lst: if lst != [ ] then lst else [ "empty" ]; |
11 | 11 |
|
12 | 12 | onOff = cond: if cond then "on" else "off";
|
13 | 13 |
|
14 |
| - setNetworkServices = optionalString (cfg.knownNetworkServices != []) '' |
15 |
| - networkservices=$(networksetup -listallnetworkservices) |
16 |
| - ${concatMapStringsSep "\n" (srv: '' |
17 |
| - case "$networkservices" in |
18 |
| - *${lib.escapeShellArg srv}*) |
19 |
| - networksetup -setdnsservers ${lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.dns))} |
20 |
| - networksetup -setsearchdomains ${lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.search))} |
21 |
| - ;; |
22 |
| - esac |
23 |
| - '') cfg.knownNetworkServices} |
| 14 | + setLocations = optionalString (cfg.knownNetworkServices != [ ] && cfg.location != { }) '' |
| 15 | + curr_location=$(networksetup -getcurrentlocation) |
| 16 | +
|
| 17 | + readarray -t curr_locations_array < <(networksetup -listlocations) |
| 18 | +
|
| 19 | + declare -A curr_locations |
| 20 | + for location in "''${curr_locations_array[@]}"; do |
| 21 | + curr_locations[$location]=1 |
| 22 | + done |
| 23 | +
|
| 24 | + declare -A goal_locations |
| 25 | + for location in ${strings.escapeShellArgs (builtins.attrNames cfg.location)}; do |
| 26 | + goal_locations[$location]=1 |
| 27 | + done |
| 28 | +
|
| 29 | + for location in "''${!goal_locations[@]}"; do |
| 30 | + if [[ ! -v curr_locations[$location] ]]; then |
| 31 | + networksetup -createlocation "$location" populate > /dev/null |
| 32 | + fi |
| 33 | + done |
| 34 | +
|
| 35 | + # switch to a location that surely does not need to be deleted |
| 36 | + networksetup -switchtolocation ${strings.escapeShellArg (builtins.head (builtins.attrNames cfg.location))} > /dev/null |
| 37 | +
|
| 38 | + for location in "''${!curr_locations[@]}"; do |
| 39 | + if [[ ! -v goal_locations[$location] ]]; then |
| 40 | + networksetup -deletelocation "$location" > /dev/null |
| 41 | + fi |
| 42 | + done |
| 43 | +
|
| 44 | + ${concatMapStringsSep "\n" (location: '' |
| 45 | + networksetup -switchtolocation ${strings.escapeShellArg location} > /dev/null |
| 46 | +
|
| 47 | + networkservices=$(networksetup -listallnetworkservices) |
| 48 | + ${concatMapStringsSep "\n" (srv: '' |
| 49 | + case "$networkservices" in |
| 50 | + *${lib.escapeShellArg srv}*) |
| 51 | + networksetup -setdnsservers ${ |
| 52 | + lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.location.${location}.dns)) |
| 53 | + } |
| 54 | + networksetup -setsearchdomains ${ |
| 55 | + lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.location.${location}.search)) |
| 56 | + } |
| 57 | + ;; |
| 58 | + esac |
| 59 | + '') cfg.knownNetworkServices} |
| 60 | + '') (builtins.attrNames cfg.location)} |
| 61 | +
|
| 62 | + if [[ -v goal_locations[$curr_location] ]]; then |
| 63 | + networksetup -switchtolocation "$curr_location" > /dev/null |
| 64 | + fi |
24 | 65 | '';
|
25 | 66 | in
|
26 | 67 |
|
27 | 68 | {
|
| 69 | + imports = [ |
| 70 | + (mkAliasOptionModule |
| 71 | + [ |
| 72 | + "networking" |
| 73 | + "dns" |
| 74 | + ] |
| 75 | + [ |
| 76 | + "networking" |
| 77 | + "location" |
| 78 | + "Automatic" |
| 79 | + "dns" |
| 80 | + ] |
| 81 | + ) |
| 82 | + (mkAliasOptionModule |
| 83 | + [ |
| 84 | + "networking" |
| 85 | + "search" |
| 86 | + ] |
| 87 | + [ |
| 88 | + "networking" |
| 89 | + "location" |
| 90 | + "Automatic" |
| 91 | + "search" |
| 92 | + ] |
| 93 | + ) |
| 94 | + ]; |
| 95 | + |
28 | 96 | options = {
|
29 | 97 | networking.computerName = mkOption {
|
30 | 98 | type = types.nullOr types.str;
|
|
73 | 141 |
|
74 | 142 | networking.knownNetworkServices = mkOption {
|
75 | 143 | type = types.listOf types.str;
|
76 |
| - default = []; |
77 |
| - example = [ "Wi-Fi" "Ethernet Adaptor" "Thunderbolt Ethernet" ]; |
| 144 | + default = [ ]; |
| 145 | + example = [ |
| 146 | + "Wi-Fi" |
| 147 | + "Ethernet Adaptor" |
| 148 | + "Thunderbolt Ethernet" |
| 149 | + ]; |
78 | 150 | description = ''
|
79 |
| - List of networkservices that should be configured. |
| 151 | + List of network services that should be configured. |
80 | 152 |
|
81 | 153 | To display a list of all the network services on the server's
|
82 | 154 | hardware ports, use {command}`networksetup -listallnetworkservices`.
|
83 | 155 | '';
|
84 | 156 | };
|
85 | 157 |
|
86 |
| - networking.dns = mkOption { |
87 |
| - type = types.listOf types.str; |
88 |
| - default = []; |
89 |
| - example = [ "8.8.8.8" "8.8.4.4" "2001:4860:4860::8888" "2001:4860:4860::8844" ]; |
90 |
| - description = "The list of dns servers used when resolving domain names."; |
91 |
| - }; |
| 158 | + networking.location = mkOption { |
| 159 | + type = types.attrsOf ( |
| 160 | + types.submodule { |
| 161 | + options = { |
| 162 | + dns = mkOption { |
| 163 | + type = types.listOf types.str; |
| 164 | + default = [ ]; |
| 165 | + example = [ |
| 166 | + "8.8.8.8" |
| 167 | + "8.8.4.4" |
| 168 | + "2001:4860:4860::8888" |
| 169 | + "2001:4860:4860::8844" |
| 170 | + ]; |
| 171 | + description = "The list of DNS servers used when resolving domain names."; |
| 172 | + }; |
| 173 | + |
| 174 | + search = mkOption { |
| 175 | + type = types.listOf types.str; |
| 176 | + default = [ ]; |
| 177 | + description = "The list of search paths used when resolving domain names."; |
| 178 | + }; |
| 179 | + }; |
| 180 | + } |
| 181 | + ); |
| 182 | + default = { }; |
| 183 | + description = '' |
| 184 | + Set of network locations to configure. |
92 | 185 |
|
93 |
| - networking.search = mkOption { |
94 |
| - type = types.listOf types.str; |
95 |
| - default = []; |
96 |
| - description = "The list of search paths used when resolving domain names."; |
| 186 | + By default, a system comes with a single location called "Automatic", but you can |
| 187 | + define additional locations to switch between different network configurations. |
| 188 | +
|
| 189 | + If you define any locations here, you must also explicitly define the "Automatic" |
| 190 | + location if you want it to exist. |
| 191 | + ''; |
97 | 192 | };
|
98 | 193 |
|
99 | 194 | networking.wakeOnLan.enable = mkOption {
|
|
110 | 205 | config = {
|
111 | 206 |
|
112 | 207 | warnings = [
|
113 |
| - (mkIf (cfg.knownNetworkServices == [] && cfg.dns != []) "networking.knownNetworkServices is empty, dns servers will not be configured.") |
114 |
| - (mkIf (cfg.knownNetworkServices == [] && cfg.search != []) "networking.knownNetworkServices is empty, dns searchdomains will not be configured.") |
| 208 | + (mkIf ( |
| 209 | + cfg.knownNetworkServices == [ ] |
| 210 | + && (builtins.any (l: l.dns != [ ]) (builtins.attrValues cfg.location)) |
| 211 | + ) "networking.knownNetworkServices is empty, DNS servers will not be configured.") |
| 212 | + (mkIf ( |
| 213 | + cfg.knownNetworkServices == [ ] |
| 214 | + && (builtins.any (l: l.search != [ ]) (builtins.attrValues cfg.location)) |
| 215 | + ) "networking.knownNetworkServices is empty, DNS search domains will not be configured.") |
115 | 216 | ];
|
116 | 217 |
|
117 | 218 | system.activationScripts.networking.text = ''
|
|
128 | 229 | scutil --set LocalHostName ${escapeShellArg cfg.localHostName}
|
129 | 230 | ''}
|
130 | 231 |
|
131 |
| - ${setNetworkServices} |
| 232 | + ${setLocations} |
132 | 233 |
|
133 | 234 | ${optionalString (cfg.wakeOnLan.enable != null) ''
|
134 | 235 | systemsetup -setWakeOnNetworkAccess '${onOff cfg.wakeOnLan.enable}' &> /dev/null
|
|
0 commit comments