|
1 |
| -{ config, lib, pkgs, ... }: |
| 1 | +{ config, lib, ... }: |
2 | 2 |
|
3 | 3 | with lib;
|
4 | 4 |
|
|
10 | 10 | emptyList = lst: if lst != [] then lst else ["empty"];
|
11 | 11 | quoteStrings = concatMapStringsSep " " (str: "'${str}'");
|
12 | 12 |
|
13 |
| - setNetworkServices = optionalString (cfg.knownNetworkServices != []) '' |
14 |
| - networkservices=$(networksetup -listallnetworkservices) |
15 |
| - ${concatMapStringsSep "\n" (srv: '' |
16 |
| - case "$networkservices" in |
17 |
| - *'${srv}'*) |
18 |
| - networksetup -setdnsservers '${srv}' ${quoteStrings (emptyList cfg.dns)} |
19 |
| - networksetup -setsearchdomains '${srv}' ${quoteStrings (emptyList cfg.search)} |
20 |
| - ;; |
21 |
| - esac |
22 |
| - '') cfg.knownNetworkServices} |
| 13 | + setLocations = optionalString (cfg.knownNetworkServices != [] && cfg.location != {}) '' |
| 14 | + curr_location=$(networksetup -getcurrentlocation) |
| 15 | +
|
| 16 | + readarray -t curr_locations_array < <(networksetup -listlocations) |
| 17 | +
|
| 18 | + declare -A curr_locations |
| 19 | + for location in "''${curr_locations_array[@]}"; do |
| 20 | + curr_locations[$location]=1 |
| 21 | + done |
| 22 | +
|
| 23 | + declare -A goal_locations |
| 24 | + for location in ${strings.escapeShellArgs (builtins.attrNames cfg.location)}; do |
| 25 | + goal_locations[$location]=1 |
| 26 | + done |
| 27 | +
|
| 28 | + for location in "''${!goal_locations[@]}"; do |
| 29 | + if [[ ! -v curr_locations[$location] ]]; then |
| 30 | + networksetup -createlocation "$location" populate > /dev/null |
| 31 | + fi |
| 32 | + done |
| 33 | +
|
| 34 | + # switch to a location that surely does not need to be deleted |
| 35 | + networksetup -switchtolocation ${strings.escapeShellArg ( |
| 36 | + builtins.head (builtins.attrNames cfg.location) |
| 37 | + )} > /dev/null |
| 38 | +
|
| 39 | + for location in "''${!curr_locations[@]}"; do |
| 40 | + if [[ ! -v goal_locations[$location] ]]; then |
| 41 | + networksetup -deletelocation "$location" > /dev/null |
| 42 | + fi |
| 43 | + done |
| 44 | +
|
| 45 | + ${concatMapStringsSep "\n" (location: '' |
| 46 | + networksetup -switchtolocation ${strings.escapeShellArg location} > /dev/null |
| 47 | +
|
| 48 | + networkservices=$(networksetup -listallnetworkservices) |
| 49 | + ${concatMapStringsSep "\n" (srv: '' |
| 50 | + case "$networkservices" in |
| 51 | + *'${srv}'*) |
| 52 | + networksetup -setdnsservers '${srv}' ${quoteStrings (emptyList cfg.location.${location}.dns)} |
| 53 | + networksetup -setsearchdomains '${srv}' ${quoteStrings (emptyList cfg.location.${location}.search)} |
| 54 | + ;; |
| 55 | + esac |
| 56 | + '') cfg.knownNetworkServices} |
| 57 | + '') (builtins.attrNames cfg.location)} |
| 58 | +
|
| 59 | + if [[ -v goal_locations[$curr_location] ]]; then |
| 60 | + networksetup -switchtolocation "$curr_location" > /dev/null |
| 61 | + fi |
23 | 62 | '';
|
24 | 63 | in
|
25 | 64 |
|
26 | 65 | {
|
| 66 | + imports = [ |
| 67 | + (mkAliasOptionModule ["networking" "dns"] ["networking" "location" "Automatic" "dns"]) |
| 68 | + (mkAliasOptionModule ["networking" "search"] ["networking" "location" "Automatic" "search"]) |
| 69 | + ]; |
| 70 | + |
27 | 71 | options = {
|
28 | 72 | networking.computerName = mkOption {
|
29 | 73 | type = types.nullOr types.str;
|
|
75 | 119 | default = [];
|
76 | 120 | example = [ "Wi-Fi" "Ethernet Adaptor" "Thunderbolt Ethernet" ];
|
77 | 121 | description = ''
|
78 |
| - List of networkservices that should be configured. |
| 122 | + List of network services that should be configured. |
79 | 123 |
|
80 | 124 | To display a list of all the network services on the server's
|
81 | 125 | hardware ports, use {command}`networksetup -listallnetworkservices`.
|
82 | 126 | '';
|
83 | 127 | };
|
84 | 128 |
|
85 |
| - networking.dns = mkOption { |
86 |
| - type = types.listOf types.str; |
87 |
| - default = []; |
88 |
| - example = [ "8.8.8.8" "8.8.4.4" "2001:4860:4860::8888" "2001:4860:4860::8844" ]; |
89 |
| - description = "The list of dns servers used when resolving domain names."; |
90 |
| - }; |
| 129 | + networking.location = mkOption { |
| 130 | + type = types.attrsOf (types.submodule { |
| 131 | + options = { |
| 132 | + dns = mkOption { |
| 133 | + type = types.listOf types.str; |
| 134 | + default = []; |
| 135 | + example = [ "8.8.8.8" "8.8.4.4" "2001:4860:4860::8888" "2001:4860:4860::8844" ]; |
| 136 | + description = "The list of DNS servers used when resolving domain names."; |
| 137 | + }; |
| 138 | + |
| 139 | + search = mkOption { |
| 140 | + type = types.listOf types.str; |
| 141 | + default = []; |
| 142 | + description = "The list of search paths used when resolving domain names."; |
| 143 | + }; |
| 144 | + }; |
| 145 | + }); |
| 146 | + default = {}; |
| 147 | + description = '' |
| 148 | + Set of network locations to configure. |
91 | 149 |
|
92 |
| - networking.search = mkOption { |
93 |
| - type = types.listOf types.str; |
94 |
| - default = []; |
95 |
| - description = "The list of search paths used when resolving domain names."; |
| 150 | + By default, a system comes with a single location called "Automatic", but you can |
| 151 | + define additional locations to switch between different network configurations. |
| 152 | +
|
| 153 | + If you define any locations here, you must also explicitly define the "Automatic" |
| 154 | + location if you want it to exist. |
| 155 | + ''; |
96 | 156 | };
|
97 | 157 | };
|
98 | 158 |
|
99 | 159 | config = {
|
100 | 160 |
|
101 | 161 | warnings = [
|
102 |
| - (mkIf (cfg.knownNetworkServices == [] && cfg.dns != []) "networking.knownNetworkServices is empty, dns servers will not be configured.") |
103 |
| - (mkIf (cfg.knownNetworkServices == [] && cfg.search != []) "networking.knownNetworkServices is empty, dns searchdomains will not be configured.") |
| 162 | + ( |
| 163 | + mkIf (cfg.knownNetworkServices == [] && ( |
| 164 | + builtins.any (l: l.dns != []) (builtins.attrValues cfg.location) |
| 165 | + )) "networking.knownNetworkServices is empty, DNS servers will not be configured." |
| 166 | + ) |
| 167 | + ( |
| 168 | + mkIf (cfg.knownNetworkServices == [] && ( |
| 169 | + builtins.any (l: l.search != []) (builtins.attrValues cfg.location) |
| 170 | + )) "networking.knownNetworkServices is empty, DNS search domains will not be configured." |
| 171 | + ) |
104 | 172 | ];
|
105 | 173 |
|
106 | 174 | system.activationScripts.networking.text = ''
|
|
116 | 184 | scutil --set LocalHostName ${escapeShellArg cfg.localHostName}
|
117 | 185 | ''}
|
118 | 186 |
|
119 |
| - ${setNetworkServices} |
| 187 | + ${setLocations} |
120 | 188 | '';
|
121 | 189 |
|
122 | 190 | };
|
|
0 commit comments