2024-10-21 22:34:25 +00:00
|
|
|
|
{ config, lib, ... }:
|
2017-07-05 09:32:36 +00:00
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
cfg = config.networking;
|
|
|
|
|
|
2020-09-01 01:01:41 +00:00
|
|
|
|
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])$'';
|
|
|
|
|
|
2018-01-07 23:00:11 +00:00
|
|
|
|
emptyList = lst: if lst != [] then lst else ["empty"];
|
|
|
|
|
|
2023-11-18 13:18:06 +00:00
|
|
|
|
onOff = cond: if cond then "on" else "off";
|
|
|
|
|
|
2018-01-13 01:12:39 +00:00
|
|
|
|
setNetworkServices = optionalString (cfg.knownNetworkServices != []) ''
|
2018-01-07 23:00:11 +00:00
|
|
|
|
networkservices=$(networksetup -listallnetworkservices)
|
|
|
|
|
${concatMapStringsSep "\n" (srv: ''
|
|
|
|
|
case "$networkservices" in
|
2024-10-21 22:34:25 +00:00
|
|
|
|
*${lib.escapeShellArg srv}*)
|
|
|
|
|
networksetup -setdnsservers ${lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.dns))}
|
|
|
|
|
networksetup -setsearchdomains ${lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.search))}
|
2018-01-07 23:00:11 +00:00
|
|
|
|
;;
|
|
|
|
|
esac
|
2018-01-13 01:12:39 +00:00
|
|
|
|
'') cfg.knownNetworkServices}
|
2018-01-07 23:00:11 +00:00
|
|
|
|
'';
|
2017-07-05 09:32:36 +00:00
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
options = {
|
2020-09-01 01:01:41 +00:00
|
|
|
|
networking.computerName = mkOption {
|
2017-07-05 09:32:36 +00:00
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
default = null;
|
2020-09-01 01:01:41 +00:00
|
|
|
|
example = "John’s MacBook Pro";
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2020-09-01 01:01:41 +00:00
|
|
|
|
The user-friendly name for the system, set in System Preferences > Sharing > Computer Name.
|
|
|
|
|
|
|
|
|
|
Setting this option is equivalent to running `scutil --set ComputerName`.
|
|
|
|
|
|
|
|
|
|
This name can contain spaces and Unicode characters.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.hostName = mkOption {
|
|
|
|
|
type = types.nullOr (types.strMatching hostnameRegEx);
|
|
|
|
|
default = null;
|
|
|
|
|
example = "Johns-MacBook-Pro";
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2020-09-01 01:01:41 +00:00
|
|
|
|
The hostname of your system, as visible from the command line and used by local and remote
|
|
|
|
|
networks when connecting through SSH and Remote Login.
|
|
|
|
|
|
|
|
|
|
Setting this option is equivalent to running the command `scutil --set HostName`.
|
|
|
|
|
|
|
|
|
|
(Note that networking.localHostName defaults to the value of this option.)
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.localHostName = mkOption {
|
|
|
|
|
type = types.nullOr (types.strMatching hostnameRegEx);
|
|
|
|
|
default = cfg.hostName;
|
|
|
|
|
example = "Johns-MacBook-Pro";
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2020-09-01 01:01:41 +00:00
|
|
|
|
The local hostname, or local network name, is displayed beneath the computer's name at the
|
|
|
|
|
top of the Sharing preferences pane. It identifies your Mac to Bonjour-compatible services.
|
|
|
|
|
|
|
|
|
|
Setting this option is equivalent to running the command `scutil --set LocalHostName`, where
|
|
|
|
|
running, e.g., `scutil --set LocalHostName 'Johns-MacBook-Pro'`, would set
|
|
|
|
|
the systems local hostname to "Johns-MacBook-Pro.local". The value of this option defaults
|
|
|
|
|
to the value of the networking.hostName option.
|
|
|
|
|
|
|
|
|
|
By default on macOS the local hostname is your computer's name with ".local" appended, with
|
|
|
|
|
any spaces replaced with hyphens, and invalid characters omitted.
|
|
|
|
|
'';
|
2017-07-05 09:32:36 +00:00
|
|
|
|
};
|
2018-01-07 23:00:11 +00:00
|
|
|
|
|
2018-01-13 01:12:39 +00:00
|
|
|
|
networking.knownNetworkServices = mkOption {
|
2018-01-07 23:00:11 +00:00
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [];
|
|
|
|
|
example = [ "Wi-Fi" "Ethernet Adaptor" "Thunderbolt Ethernet" ];
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = ''
|
2018-01-07 23:00:11 +00:00
|
|
|
|
List of networkservices that should be configured.
|
|
|
|
|
|
|
|
|
|
To display a list of all the network services on the server's
|
2023-06-22 11:21:32 +00:00
|
|
|
|
hardware ports, use {command}`networksetup -listallnetworkservices`.
|
2018-01-07 23:00:11 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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" ];
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = "The list of dns servers used when resolving domain names.";
|
2018-01-07 23:00:11 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.search = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [];
|
2024-04-14 21:02:32 +00:00
|
|
|
|
description = "The list of search paths used when resolving domain names.";
|
2018-01-07 23:00:11 +00:00
|
|
|
|
};
|
2023-11-18 13:18:06 +00:00
|
|
|
|
|
|
|
|
|
networking.wakeOnLan.enable = mkOption {
|
|
|
|
|
type = types.nullOr types.bool;
|
|
|
|
|
default = null;
|
|
|
|
|
description = ''
|
|
|
|
|
Enable Wake-on-LAN for the device.
|
|
|
|
|
|
|
|
|
|
Battery powered devices may require being connected to power.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2017-07-05 09:32:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
2018-01-08 17:45:12 +00:00
|
|
|
|
warnings = [
|
2018-01-13 01:12:39 +00:00
|
|
|
|
(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.")
|
2018-01-08 17:45:12 +00:00
|
|
|
|
];
|
|
|
|
|
|
2017-07-05 09:32:36 +00:00
|
|
|
|
system.activationScripts.networking.text = ''
|
|
|
|
|
echo "configuring networking..." >&2
|
|
|
|
|
|
2020-09-01 01:01:41 +00:00
|
|
|
|
${optionalString (cfg.computerName != null) ''
|
2024-10-28 13:09:37 +00:00
|
|
|
|
# shellcheck disable=SC1112
|
2024-01-19 17:16:32 +00:00
|
|
|
|
scutil --set ComputerName ${escapeShellArg cfg.computerName}
|
2020-09-01 01:01:41 +00:00
|
|
|
|
''}
|
|
|
|
|
${optionalString (cfg.hostName != null) ''
|
2024-01-19 17:16:32 +00:00
|
|
|
|
scutil --set HostName ${escapeShellArg cfg.hostName}
|
2020-09-01 01:01:41 +00:00
|
|
|
|
''}
|
|
|
|
|
${optionalString (cfg.localHostName != null) ''
|
2024-01-19 17:16:32 +00:00
|
|
|
|
scutil --set LocalHostName ${escapeShellArg cfg.localHostName}
|
2020-09-01 01:01:41 +00:00
|
|
|
|
''}
|
|
|
|
|
|
2018-01-07 23:00:11 +00:00
|
|
|
|
${setNetworkServices}
|
2023-11-18 13:18:06 +00:00
|
|
|
|
|
|
|
|
|
${optionalString (cfg.wakeOnLan.enable != null) ''
|
|
|
|
|
systemsetup -setWakeOnNetworkAccess '${onOff cfg.wakeOnLan.enable}' &> /dev/null
|
|
|
|
|
''}
|
2017-07-05 09:32:36 +00:00
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|