Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support configuring network locations #1048

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 128 additions & 27 deletions modules/networking/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,92 @@ let

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])$'';

emptyList = lst: if lst != [] then lst else ["empty"];
emptyList = lst: if lst != [ ] then lst else [ "empty" ];

onOff = cond: if cond then "on" else "off";

setNetworkServices = optionalString (cfg.knownNetworkServices != []) ''
networkservices=$(networksetup -listallnetworkservices)
${concatMapStringsSep "\n" (srv: ''
case "$networkservices" in
*${lib.escapeShellArg srv}*)
networksetup -setdnsservers ${lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.dns))}
networksetup -setsearchdomains ${lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.search))}
;;
esac
'') cfg.knownNetworkServices}
setLocations = optionalString (cfg.knownNetworkServices != [ ] && cfg.location != { }) ''
curr_location=$(networksetup -getcurrentlocation)

readarray -t curr_locations_array < <(networksetup -listlocations)

declare -A curr_locations
for location in "''${curr_locations_array[@]}"; do
curr_locations[$location]=1
done

declare -A goal_locations
for location in ${strings.escapeShellArgs (builtins.attrNames cfg.location)}; do
goal_locations[$location]=1
done

for location in "''${!goal_locations[@]}"; do
if [[ ! -v curr_locations[$location] ]]; then
networksetup -createlocation "$location" populate > /dev/null
fi
done

# switch to a location that surely does not need to be deleted
networksetup -switchtolocation ${strings.escapeShellArg (builtins.head (builtins.attrNames cfg.location))} > /dev/null

for location in "''${!curr_locations[@]}"; do
if [[ ! -v goal_locations[$location] ]]; then
networksetup -deletelocation "$location" > /dev/null
fi
done

${concatMapStringsSep "\n" (location: ''
networksetup -switchtolocation ${strings.escapeShellArg location} > /dev/null

networkservices=$(networksetup -listallnetworkservices)
${concatMapStringsSep "\n" (srv: ''
case "$networkservices" in
*${lib.escapeShellArg srv}*)
networksetup -setdnsservers ${
lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.location.${location}.dns))
}
networksetup -setsearchdomains ${
lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.location.${location}.search))
}
;;
esac
'') cfg.knownNetworkServices}
'') (builtins.attrNames cfg.location)}

if [[ -v goal_locations[$curr_location] ]]; then
networksetup -switchtolocation "$curr_location" > /dev/null
fi
'';
in

{
imports = [
(mkAliasOptionModule
[
"networking"
"dns"
]
[
"networking"
"location"
"Automatic"
"dns"
]
)
(mkAliasOptionModule
[
"networking"
"search"
]
[
"networking"
"location"
"Automatic"
"search"
]
)
];

options = {
networking.computerName = mkOption {
type = types.nullOr types.str;
Expand Down Expand Up @@ -73,27 +141,54 @@ in

networking.knownNetworkServices = mkOption {
type = types.listOf types.str;
default = [];
example = [ "Wi-Fi" "Ethernet Adaptor" "Thunderbolt Ethernet" ];
default = [ ];
example = [
"Wi-Fi"
"Ethernet Adaptor"
"Thunderbolt Ethernet"
];
description = ''
List of networkservices that should be configured.
List of network services that should be configured.

To display a list of all the network services on the server's
hardware ports, use {command}`networksetup -listallnetworkservices`.
'';
};

networking.dns = mkOption {
type = types.listOf types.str;
default = [];
example = [ "8.8.8.8" "8.8.4.4" "2001:4860:4860::8888" "2001:4860:4860::8844" ];
description = "The list of dns servers used when resolving domain names.";
};
networking.location = mkOption {
type = types.attrsOf (
types.submodule {
options = {
dns = mkOption {
type = types.listOf types.str;
default = [ ];
example = [
"8.8.8.8"
"8.8.4.4"
"2001:4860:4860::8888"
"2001:4860:4860::8844"
];
description = "The list of DNS servers used when resolving domain names.";
};

search = mkOption {
type = types.listOf types.str;
default = [ ];
description = "The list of search paths used when resolving domain names.";
};
};
}
);
default = { };
description = ''
Set of network locations to configure.

networking.search = mkOption {
type = types.listOf types.str;
default = [];
description = "The list of search paths used when resolving domain names.";
By default, a system comes with a single location called "Automatic", but you can
define additional locations to switch between different network configurations.

If you define any locations here, you must also explicitly define the "Automatic"
location if you want it to exist.
'';
};

networking.wakeOnLan.enable = mkOption {
Expand All @@ -110,8 +205,14 @@ in
config = {

warnings = [
(mkIf (cfg.knownNetworkServices == [] && cfg.dns != []) "networking.knownNetworkServices is empty, dns servers will not be configured.")
(mkIf (cfg.knownNetworkServices == [] && cfg.search != []) "networking.knownNetworkServices is empty, dns searchdomains will not be configured.")
(mkIf (
cfg.knownNetworkServices == [ ]
&& (builtins.any (l: l.dns != [ ]) (builtins.attrValues cfg.location))
) "networking.knownNetworkServices is empty, DNS servers will not be configured.")
(mkIf (
cfg.knownNetworkServices == [ ]
&& (builtins.any (l: l.search != [ ]) (builtins.attrValues cfg.location))
) "networking.knownNetworkServices is empty, DNS search domains will not be configured.")
];

system.activationScripts.networking.text = ''
Expand All @@ -128,7 +229,7 @@ in
scutil --set LocalHostName ${escapeShellArg cfg.localHostName}
''}

${setNetworkServices}
${setLocations}

${optionalString (cfg.wakeOnLan.enable != null) ''
systemsetup -setWakeOnNetworkAccess '${onOff cfg.wakeOnLan.enable}' &> /dev/null
Expand Down
26 changes: 23 additions & 3 deletions tests/networking-networkservices.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
{ config, lib, ... }:

{
networking.knownNetworkServices = [ "Wi-Fi" "Thunderbolt Ethernet" ];
networking.dns = [ "8.8.8.8" "8.8.4.4" ];
networking.knownNetworkServices = [
"Wi-Fi"
"Thunderbolt Ethernet"
];
networking.dns = [
"8.8.8.8"
"8.8.4.4"
];
networking.location."Home Lab" = {
search = [ "home.lab" ];
};

test = ''
echo checking dns settings in /activate >&2

grep "networksetup -switchtolocation ${lib.escapeShellArg "Automatic"}" ${config.out}/activate
grep "networksetup -setdnsservers ${lib.escapeShellArgs [ "Wi-Fi" "8.8.8.8" "8.8.4.4" ]}" ${config.out}/activate
grep "networksetup -setdnsservers ${lib.escapeShellArgs [ "Thunderbolt Ethernet" "8.8.8.8" "8.8.4.4" ]}" ${config.out}/activate
echo checking empty searchdomain settings in /activate >&2

grep "networksetup -switchtolocation ${lib.escapeShellArg "Home Lab"}" ${config.out}/activate
grep "networksetup -setdnsservers ${lib.escapeShellArgs [ "Wi-Fi" "empty" ]}" ${config.out}/activate
grep "networksetup -setdnsservers ${lib.escapeShellArgs [ "Thunderbolt Ethernet" "empty" ]}" ${config.out}/activate

echo checking searchdomain settings in /activate >&2

grep "networksetup -setsearchdomains ${lib.escapeShellArgs [ "Wi-Fi" "empty" ]}" ${config.out}/activate
grep "networksetup -setsearchdomains ${lib.escapeShellArgs [ "Thunderbolt Ethernet" "empty" ]}" ${config.out}/activate

grep "networksetup -setsearchdomains ${lib.escapeShellArgs [ "Wi-Fi" "home.lab" ]}" ${config.out}/activate
grep "networksetup -setsearchdomains ${lib.escapeShellArgs [ "Thunderbolt Ethernet" "home.lab" ]}" ${config.out}/activate
'';
}