nixos/networking: network is online if default gw set

Previously services depending on network-online.target would wait until
dhcpcd times out if it was enabled and a static network address
configuration was used. Setting the default gateway statically is enough
for the networking to be considered online.

This also adjusts the relevant networking tests to wait for
network-online.target instead of just network.target.

Changed files
+12 -5
nixos
modules
services
networking
tasks
tests
+6 -2
nixos/modules/services/networking/dhcpcd.nix
···
config = mkIf enableDHCP {
-
systemd.services.dhcpcd =
{ description = "DHCP Client";
-
wantedBy = [ "network-online.target" ];
after = [ "network.target" ];
wants = [ "network.target" ];
···
config = mkIf enableDHCP {
+
systemd.services.dhcpcd = let
+
cfgN = config.networking;
+
hasDefaultGatewaySet = (cfgN.defaultGateway != null && cfgN.defaultGateway.address != "")
+
|| (cfgN.defaultGateway6 != null && cfgN.defaultGateway6.address != "");
+
in
{ description = "DHCP Client";
+
wantedBy = optional (!hasDefaultGatewaySet) "network-online.target";
after = [ "network.target" ];
wants = [ "network.target" ];
+4 -1
nixos/modules/tasks/network-interfaces-scripted.nix
···
then [ "${dev}-netdev.service" ]
else optional (dev != null && dev != "lo" && !config.boot.isContainer) (subsystemDevice dev);
networkLocalCommands = {
after = [ "network-setup.service" ];
bindsTo = [ "network-setup.service" ];
···
before = [ "network.target" "shutdown.target" ];
wants = [ "network.target" ];
conflicts = [ "shutdown.target" ];
-
wantedBy = [ "multi-user.target" ];
unitConfig.ConditionCapability = "CAP_NET_ADMIN";
···
then [ "${dev}-netdev.service" ]
else optional (dev != null && dev != "lo" && !config.boot.isContainer) (subsystemDevice dev);
+
hasDefaultGatewaySet = (cfg.defaultGateway != null && cfg.defaultGateway.address != "")
+
|| (cfg.defaultGateway6 != null && cfg.defaultGateway6.address != "");
+
networkLocalCommands = {
after = [ "network-setup.service" ];
bindsTo = [ "network-setup.service" ];
···
before = [ "network.target" "shutdown.target" ];
wants = [ "network.target" ];
conflicts = [ "shutdown.target" ];
+
wantedBy = [ "multi-user.target" ] ++ optional hasDefaultGatewaySet "network-online.target";
unitConfig.ConditionCapability = "CAP_NET_ADMIN";
+2 -2
nixos/tests/networking.nix
···
startAll;
$client->waitForUnit("network.target");
-
$router->waitForUnit("network.target");
# Make sure dhcpcd is not started
$client->fail("systemctl status dhcpcd.service");
···
startAll;
$client->waitForUnit("network.target");
-
$router->waitForUnit("network.target");
# Wait until we have an ip address on each interface
$client->waitUntilSucceeds("ip addr show dev eth1 | grep -q '192.168.1'");
···
startAll;
$client->waitForUnit("network.target");
+
$router->waitForUnit("network-online.target");
# Make sure dhcpcd is not started
$client->fail("systemctl status dhcpcd.service");
···
startAll;
$client->waitForUnit("network.target");
+
$router->waitForUnit("network-online.target");
# Wait until we have an ip address on each interface
$client->waitUntilSucceeds("ip addr show dev eth1 | grep -q '192.168.1'");