diff --git a/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml b/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml
index fcda25bf0f9..ab0edcebc67 100644
--- a/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml
+++ b/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml
@@ -2471,6 +2471,21 @@
hosts.
+
+
+ The option
+ networking.useDHCP
+ isn’t deprecated anymore. When using
+ systemd-networkd,
+ a generic .network-unit is added which
+ enables DHCP for each interface matching
+ en*, eth* or
+ wl* with priority 99 (which means that it
+ doesn’t have any effect if such an interface is matched by a
+ .network-unit with a lower priority). In
+ case of scripted networking, no behavior was changed.
+
+
diff --git a/nixos/doc/manual/release-notes/rl-2205.section.md b/nixos/doc/manual/release-notes/rl-2205.section.md
index 24fbc537dca..af65ae46131 100644
--- a/nixos/doc/manual/release-notes/rl-2205.section.md
+++ b/nixos/doc/manual/release-notes/rl-2205.section.md
@@ -877,4 +877,11 @@ In addition to numerous new and upgraded packages, this release has the followin
`true` starting with NixOS 22.11. Enable it explicitly if you need to control
Snapserver remotely or connect streamig clients from other hosts.
+- The option [networking.useDHCP](options.html#opt-networking.useDHCP) isn't deprecated anymore.
+ When using [`systemd-networkd`](options.html#opt-networking.useNetworkd), a generic
+ `.network`-unit is added which enables DHCP for each interface matching `en*`, `eth*`
+ or `wl*` with priority 99 (which means that it doesn't have any effect if such an interface is matched
+ by a `.network-`unit with a lower priority). In case of scripted networking, no behavior
+ was changed.
+
diff --git a/nixos/modules/installer/tools/nixos-generate-config.pl b/nixos/modules/installer/tools/nixos-generate-config.pl
index fb5d3ba4732..b74ec838df4 100644
--- a/nixos/modules/installer/tools/nixos-generate-config.pl
+++ b/nixos/modules/installer/tools/nixos-generate-config.pl
@@ -581,17 +581,19 @@ ${\join "", (map { " $_\n" } (uniq @attrs))}}
EOF
sub generateNetworkingDhcpConfig {
+ # FIXME disable networking.useDHCP by default when switching to networkd.
my $config = <.useDHCP`.
+ networking.useDHCP = lib.mkDefault true;
EOF
foreach my $path (glob "/sys/class/net/*") {
my $dev = basename($path);
if ($dev ne "lo") {
- $config .= " networking.interfaces.$dev.useDHCP = lib.mkDefault true;\n";
+ $config .= " # networking.interfaces.$dev.useDHCP = lib.mkDefault true;\n";
}
}
diff --git a/nixos/modules/system/boot/networkd.nix b/nixos/modules/system/boot/networkd.nix
index d1a6f46bfc4..0336930b3ab 100644
--- a/nixos/modules/system/boot/networkd.nix
+++ b/nixos/modules/system/boot/networkd.nix
@@ -779,6 +779,7 @@ let
"RouteDenyList"
"RouteAllowList"
"DHCPv6Client"
+ "RouteMetric"
])
(assertValueOneOf "UseDNS" boolValues)
(assertValueOneOf "UseDomains" (boolValues ++ ["route"]))
diff --git a/nixos/modules/tasks/network-interfaces-systemd.nix b/nixos/modules/tasks/network-interfaces-systemd.nix
index 8654539b662..110e84494a3 100644
--- a/nixos/modules/tasks/network-interfaces-systemd.nix
+++ b/nixos/modules/tasks/network-interfaces-systemd.nix
@@ -43,12 +43,6 @@ in
} {
assertion = cfg.defaultGateway6 == null || cfg.defaultGateway6.interface == null;
message = "networking.defaultGateway6.interface is not supported by networkd.";
- } {
- assertion = cfg.useDHCP == false;
- message = ''
- networking.useDHCP is not supported by networkd.
- Please use per interface configuration and set the global option to false.
- '';
} ] ++ flip mapAttrsToList cfg.bridges (n: { rstp, ... }: {
assertion = !rstp;
message = "networking.bridges.${n}.rstp is not supported by networkd.";
@@ -80,6 +74,42 @@ in
in mkMerge [ {
enable = true;
}
+ (mkIf cfg.useDHCP {
+ networks."99-ethernet-default-dhcp" = lib.mkIf cfg.useDHCP {
+ # We want to match physical ethernet interfaces as commonly
+ # found on laptops, desktops and servers, to provide an
+ # "out-of-the-box" setup that works for common cases. This
+ # heuristic isn't perfect (it could match interfaces with
+ # custom names that _happen_ to start with en or eth), but
+ # should be good enough to make the common case easy and can
+ # be overridden on a case-by-case basis using
+ # higher-priority networks or by disabling useDHCP.
+
+ # Type=ether matches veth interfaces as well, and this is
+ # more likely to result in interfaces being configured to
+ # use DHCP when they shouldn't.
+
+ # We set RequiredForOnline to false, because it's fairly
+ # common for such devices to have multiple interfaces and
+ # only one of them to be connected (e.g. a laptop with
+ # ethernet and WiFi interfaces). Maybe one day networkd will
+ # support "any"-style RequiredForOnline...
+ matchConfig.Name = ["en*" "eth*"];
+ DHCP = "yes";
+ linkConfig.RequiredForOnline = lib.mkDefault false;
+ };
+ networks."99-wireless-client-dhcp" = lib.mkIf cfg.useDHCP {
+ # Like above, but this is much more likely to be correct.
+ matchConfig.WLANInterfaceType = "station";
+ DHCP = "yes";
+ linkConfig.RequiredForOnline = lib.mkDefault false;
+ # We also set the route metric to one more than the default
+ # of 1024, so that Ethernet is preferred if both are
+ # available.
+ dhcpV4Config.RouteMetric = 1025;
+ ipv6AcceptRAConfig.RouteMetric = 1025;
+ };
+ })
(mkMerge (forEach interfaces (i: {
netdevs = mkIf i.virtual ({
"40-${i.name}" = {
diff --git a/nixos/modules/tasks/network-interfaces.nix b/nixos/modules/tasks/network-interfaces.nix
index d09e9b99248..d56159f1596 100644
--- a/nixos/modules/tasks/network-interfaces.nix
+++ b/nixos/modules/tasks/network-interfaces.nix
@@ -1254,11 +1254,6 @@ in
Whether to use DHCP to obtain an IP address and other
configuration for all network interfaces that are not manually
configured.
-
- Using this option is highly discouraged and also incompatible with
- . Please use
- instead
- and set this to false.
'';
};
diff --git a/nixos/tests/networking.nix b/nixos/tests/networking.nix
index a1150097a09..2cc1e9b0942 100644
--- a/nixos/tests/networking.nix
+++ b/nixos/tests/networking.nix
@@ -139,6 +139,26 @@ let
client.wait_until_succeeds("ping -c 1 192.168.3.1")
'';
};
+ dhcpDefault = {
+ name = "useDHCP-by-default";
+ nodes.router = router;
+ nodes.client = { lib, ... }: {
+ # Disable test driver default config
+ networking.interfaces = lib.mkForce {};
+ networking.useNetworkd = networkd;
+ virtualisation.vlans = [ 1 ];
+ };
+ testScript = ''
+ start_all()
+ client.wait_for_unit("multi-user.target")
+ client.wait_until_succeeds("ip addr show dev eth1 | grep '192.168.1'")
+ client.shell_interact()
+ client.succeed("ping -c 1 192.168.1.1")
+ router.succeed("ping -c 1 192.168.1.1")
+ router.succeed("ping -c 1 192.168.1.2")
+ client.succeed("ping -c 1 192.168.1.2")
+ '';
+ };
dhcpSimple = {
name = "SimpleDHCP";
nodes.router = router;