From 05a5979906ef181644ee4db71bcbbd90ca7c8cca Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Sun, 10 Aug 2025 21:19:51 -0400 Subject: [PATCH] networking: add dhcpClientId option to set DHCP Client ID --- modules/networking/default.nix | 19 +++++++++++++++++++ release.nix | 1 + ...king-networkservices-no-dhcp-client-id.nix | 14 ++++++++++++++ tests/networking-networkservices.nix | 3 +++ 4 files changed, 37 insertions(+) create mode 100644 tests/networking-networkservices-no-dhcp-client-id.nix diff --git a/modules/networking/default.nix b/modules/networking/default.nix index e75e234..44af42e 100644 --- a/modules/networking/default.nix +++ b/modules/networking/default.nix @@ -18,6 +18,9 @@ let *${lib.escapeShellArg srv}*) networksetup -setdnsservers ${lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.dns))} networksetup -setsearchdomains ${lib.escapeShellArgs ([ srv ] ++ (emptyList cfg.search))} + ${optionalString (cfg.dhcpClientId != null) '' + networksetup -setdhcp ${lib.escapeShellArgs [ srv cfg.dhcpClientId ]} + ''} ;; esac '') cfg.knownNetworkServices} @@ -133,6 +136,21 @@ in ''; }; + networking.dhcpClientId = mkOption { + type = types.nullOr types.str; + default = null; + example = "my-client-id"; + description = '' + The DHCP client identifier to use when requesting an IP address from a DHCP server. + + If this option is set, it will be used by the system when requesting an IP address. + If not set, no changes will be made. + + Set to the string "empty" to clear any previously configured client ID + and restore the system default behavior. + ''; + }; + networking.dns = mkOption { type = types.listOf types.str; default = []; @@ -162,6 +180,7 @@ in 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 == [] && cfg.dhcpClientId != null) "networking.knownNetworkServices is empty, dhcp client ID will not be configured.") ]; system.activationScripts.networking.text = '' diff --git a/release.nix b/release.nix index 160c642..b8a1d6b 100644 --- a/release.nix +++ b/release.nix @@ -88,6 +88,7 @@ in { tests.networking-firewall = makeTest ./tests/networking-firewall.nix; tests.networking-hostname = makeTest ./tests/networking-hostname.nix; tests.networking-networkservices = makeTest ./tests/networking-networkservices.nix; + tests.networking-networkservices-no-dhcp-client-id = makeTest ./tests/networking-networkservices-no-dhcp-client-id.nix; tests.nix-enable = makeTest ./tests/nix-enable.nix; tests.nixpkgs-overlays = makeTest ./tests/nixpkgs-overlays.nix; tests.programs-gnupg = makeTest ./tests/programs-gnupg.nix; diff --git a/tests/networking-networkservices-no-dhcp-client-id.nix b/tests/networking-networkservices-no-dhcp-client-id.nix new file mode 100644 index 0000000..2a37425 --- /dev/null +++ b/tests/networking-networkservices-no-dhcp-client-id.nix @@ -0,0 +1,14 @@ +{ config, lib, ... }: + +{ + networking.knownNetworkServices = [ "Wi-Fi" "Thunderbolt Ethernet" ]; + networking.dns = [ "8.8.8.8" "8.8.4.4" ]; + + test = '' + echo checking dhcp client ID is not configured in /activate >&2 + if grep -q "networksetup -setdhcp" ${config.out}/activate; then + echo "unexpected dhcp client ID configuration in /activate" >&2 + exit 1 + fi + ''; +} diff --git a/tests/networking-networkservices.nix b/tests/networking-networkservices.nix index a657342..7e6f744 100644 --- a/tests/networking-networkservices.nix +++ b/tests/networking-networkservices.nix @@ -3,11 +3,14 @@ { networking.knownNetworkServices = [ "Wi-Fi" "Thunderbolt Ethernet" ]; networking.dns = [ "8.8.8.8" "8.8.4.4" ]; + networking.dhcpClientId = "test-client-id"; test = '' echo checking dns settings in /activate >&2 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 + grep "networksetup -setdhcp ${lib.escapeShellArgs [ "Wi-Fi" "test-client-id" ]}" ${config.out}/activate + grep "networksetup -setdhcp ${lib.escapeShellArgs [ "Thunderbolt Ethernet" "test-client-id" ]}" ${config.out}/activate echo checking empty 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