Commit 748f1329 authored by Will Fancher's avatar Will Fancher
Browse files

systemd-initrd: Automatically configure networking.interfaces

parent dd392d7c
Loading
Loading
Loading
Loading
+159 −143
Original line number Diff line number Diff line
@@ -28,33 +28,6 @@ let
    # TODO: warn the user that any address configured on those interfaces will be useless
    ++ concatMap (i: attrNames (filterAttrs (_: config: config.type != "internal") i.interfaces)) (attrValues cfg.vswitches);

in

{

  config = mkIf cfg.useNetworkd {

    assertions = [ {
      assertion = cfg.defaultGatewayWindowSize == null;
      message = "networking.defaultGatewayWindowSize is not supported by networkd.";
    } {
      assertion = cfg.defaultGateway == null || cfg.defaultGateway.interface == null;
      message = "networking.defaultGateway.interface is not supported by networkd.";
    } {
      assertion = cfg.defaultGateway6 == null || cfg.defaultGateway6.interface == null;
      message = "networking.defaultGateway6.interface is not supported by networkd.";
    } ] ++ flip mapAttrsToList cfg.bridges (n: { rstp, ... }: {
      assertion = !rstp;
      message = "networking.bridges.${n}.rstp is not supported by networkd.";
    }) ++ flip mapAttrsToList cfg.fooOverUDP (n: { local, ... }: {
      assertion = local == null;
      message = "networking.fooOverUDP.${n}.local is not supported by networkd.";
    });

    networking.dhcpcd.enable = mkDefault false;

    systemd.network =
      let
  domains = cfg.search ++ (optional (cfg.domain != null) cfg.domain);
  genericNetwork = override:
    let gateway = optional (cfg.defaultGateway != null && (cfg.defaultGateway.address or "") != "") cfg.defaultGateway.address
@@ -70,11 +43,9 @@ in
    } // optionalAttrs (domains != [ ]) {
      domains = override domains;
    };
      in mkMerge [ {
        enable = true;
      }
      (mkIf cfg.useDHCP {
        networks."99-ethernet-default-dhcp" = lib.mkIf cfg.useDHCP {

  genericDhcpNetworks = initrd: mkIf cfg.useDHCP {
    networks."99-ethernet-default-dhcp" = {
      # 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
@@ -94,10 +65,12 @@ in
      matchConfig.Name = ["en*" "eth*"];
      DHCP = "yes";
      linkConfig.RequiredForOnline =
            lib.mkDefault config.systemd.network.wait-online.anyInterface;
        lib.mkDefault (if initrd
        then config.boot.initrd.systemd.network.wait-online.anyInterface
        else config.systemd.network.wait-online.anyInterface);
      networkConfig.IPv6PrivacyExtensions = "kernel";
    };
        networks."99-wireless-client-dhcp" = lib.mkIf cfg.useDHCP {
    networks."99-wireless-client-dhcp" = {
      # Like above, but this is much more likely to be correct.
      matchConfig.WLANInterfaceType = "station";
      DHCP = "yes";
@@ -110,8 +83,10 @@ in
      dhcpV4Config.RouteMetric = 1025;
      ipv6AcceptRAConfig.RouteMetric = 1025;
    };
      })
      (mkMerge (forEach interfaces (i: {
  };


  interfaceNetworks = mkMerge (forEach interfaces (i: {
    netdevs = mkIf i.virtual ({
      "40-${i.name}" = {
        netdevConfig = {
@@ -196,7 +171,47 @@ in
        MTUBytes = toString i.mtu;
      };
    }];
      })))
  }));

in

{
  config = mkMerge [

  (mkIf config.boot.initrd.network.enable {
    # Note this is if initrd.network.enable, not if
    # initrd.systemd.network.enable. By setting the latter and not the
    # former, the user retains full control over the configuration.
    boot.initrd.systemd.network = mkMerge [(genericDhcpNetworks true) interfaceNetworks];
  })

  (mkIf cfg.useNetworkd {

    assertions = [ {
      assertion = cfg.defaultGatewayWindowSize == null;
      message = "networking.defaultGatewayWindowSize is not supported by networkd.";
    } {
      assertion = cfg.defaultGateway == null || cfg.defaultGateway.interface == null;
      message = "networking.defaultGateway.interface is not supported by networkd.";
    } {
      assertion = cfg.defaultGateway6 == null || cfg.defaultGateway6.interface == null;
      message = "networking.defaultGateway6.interface is not supported by networkd.";
    } ] ++ flip mapAttrsToList cfg.bridges (n: { rstp, ... }: {
      assertion = !rstp;
      message = "networking.bridges.${n}.rstp is not supported by networkd.";
    }) ++ flip mapAttrsToList cfg.fooOverUDP (n: { local, ... }: {
      assertion = local == null;
      message = "networking.fooOverUDP.${n}.local is not supported by networkd.";
    });

    networking.dhcpcd.enable = mkDefault false;

    systemd.network =
      mkMerge [ {
        enable = true;
      }
      (genericDhcpNetworks false)
      interfaceNetworks
      (mkMerge (flip mapAttrsToList cfg.bridges (name: bridge: {
        netdevs."40-${name}" = {
          netdevConfig = {
@@ -437,6 +452,7 @@ in
              bindsTo = [ "systemd-networkd.service" ];
          };
      };
  };
  })

  ];
}
+2 −5
Original line number Diff line number Diff line
@@ -8,12 +8,9 @@ import ./make-test-python.nix ({ pkgs, lib, ... }: {

      boot.initrd.systemd = {
        enable = true;
        network.networks."99-eth0" = {
          matchConfig.Name = "eth0";
          DHCP = "yes";
        };
        # Enable network-online to fail the test in case of timeout
        network.wait-online.timeout = 10;
        # Drop the boot into emergency mode if we timeout
        network.wait-online.anyInterface = true;
        targets.network-online.requiredBy = [ "initrd.target" ];
        services.systemd-networkd-wait-online.requiredBy =
          [ "network-online.target" ];