summary refs log tree commit diff
path: root/nixos/modules/tasks
diff options
context:
space:
mode:
authorWilliam A. Kennington III <william@wkennington.com>2014-11-24 12:06:07 -0800
committerWilliam A. Kennington III <william@wkennington.com>2014-11-26 11:22:03 -0800
commitc7d46687c5ed499bc558718eb04e90842b3eb0fd (patch)
tree968158abd7b5abeb2126af4d8a40e7d85fb9c35f /nixos/modules/tasks
parent2057d9087fa27413b2802c5ad54e5be5c2088f82 (diff)
downloadnixlib-c7d46687c5ed499bc558718eb04e90842b3eb0fd.tar
nixlib-c7d46687c5ed499bc558718eb04e90842b3eb0fd.tar.gz
nixlib-c7d46687c5ed499bc558718eb04e90842b3eb0fd.tar.bz2
nixlib-c7d46687c5ed499bc558718eb04e90842b3eb0fd.tar.lz
nixlib-c7d46687c5ed499bc558718eb04e90842b3eb0fd.tar.xz
nixlib-c7d46687c5ed499bc558718eb04e90842b3eb0fd.tar.zst
nixlib-c7d46687c5ed499bc558718eb04e90842b3eb0fd.zip
nixos/network-interfaces: Support mac address and mtu universally
Diffstat (limited to 'nixos/modules/tasks')
-rw-r--r--nixos/modules/tasks/network-interfaces-scripted.nix54
-rw-r--r--nixos/modules/tasks/network-interfaces-systemd.nix9
-rw-r--r--nixos/modules/tasks/network-interfaces.nix53
3 files changed, 56 insertions, 60 deletions
diff --git a/nixos/modules/tasks/network-interfaces-scripted.nix b/nixos/modules/tasks/network-interfaces-scripted.nix
index 1ac3310797c6..4910a391523f 100644
--- a/nixos/modules/tasks/network-interfaces-scripted.nix
+++ b/nixos/modules/tasks/network-interfaces-scripted.nix
@@ -80,65 +80,44 @@ in
               '';
           };
 
-        # For each interface <foo>, create a job ‘<foo>-cfg.service"
-        # that performs static configuration.  It has a "wants"
+        # For each interface <foo>, create a job ‘network-addresses-<foo>.service"
+        # that performs static address configuration.  It has a "wants"
         # dependency on ‘<foo>.service’, which is supposed to create
         # the interface and need not exist (i.e. for hardware
         # interfaces).  It has a binds-to dependency on the actual
         # network device, so it only gets started after the interface
         # has appeared, and it's stopped when the interface
         # disappears.
-        configureInterface = i:
+        configureAddrs = i:
           let
             ips = interfaceIps i;
           in
-          nameValuePair "${i.name}-cfg"
-          { description = "Configuration of ${i.name}";
+          nameValuePair "network-addresses-${i.name}"
+          { description = "Addresss configuration of ${i.name}";
             wantedBy = [ "network-interfaces.target" ];
+            before = [ "network-interfaces.target" ];
             bindsTo = [ (subsystemDevice i.name) ];
             after = [ (subsystemDevice i.name) ];
             serviceConfig.Type = "oneshot";
             serviceConfig.RemainAfterExit = true;
-            path = [ pkgs.iproute pkgs.gawk ];
             script =
               ''
                 echo "bringing up interface..."
                 ip link set "${i.name}" up
-              ''
-              + optionalString (i.macAddress != null)
-                ''
-                  echo "setting MAC address to ${i.macAddress}..."
-                  ip link set "${i.name}" address "${i.macAddress}"
-                ''
-              + optionalString (i.mtu != null)
-                ''
-                  echo "setting MTU to ${toString i.mtu}..."
-                  ip link set "${i.name}" mtu "${toString i.mtu}"
-                ''
 
-              # Ip Setup
-              +
-                ''
-                  curIps=$(ip -o a show dev "${i.name}" | awk '{print $4}')
-                  # Only do an add if it's necessary.  This is
-                  # useful when the Nix store is accessed via this
-                  # interface (e.g. in a QEMU VM test).
-                  restart_network_interfaces=false
-                ''
-              + flip concatMapStrings (ips) (ip:
+                restart_network_interfaces=false
+              '' + flip concatMapStrings (ips) (ip:
                 let
                   address = "${ip.address}/${toString ip.prefixLength}";
                 in
                 ''
                   echo "checking ip ${address}..."
-                  if ! echo "$curIps" | grep "${address}" >/dev/null 2>&1; then
-                    if out=$(ip addr add "${address}" dev "${i.name}" 2>&1); then
-                      echo "added ip ${address}..."
-                      restart_network_setup=true
-                    elif ! echo "$out" | grep "File exists" >/dev/null 2>&1; then
-                      echo "failed to add ${address}"
-                      exit 1
-                    fi
+                  if out=$(ip addr add "${address}" dev "${i.name}" 2>&1); then
+                    echo "added ip ${address}..."
+                    restart_network_setup=true
+                  elif ! echo "$out" | grep "File exists" >/dev/null 2>&1; then
+                    echo "failed to add ${address}"
+                    exit 1
                   fi
                 '')
               + optionalString (ips != [ ])
@@ -154,8 +133,7 @@ in
             preStop =
               ''
                 echo "releasing configured ip's..."
-              ''
-              + flip concatMapStrings (ips) (ip:
+              '' + flip concatMapStrings (ips) (ip:
                 let
                   address = "${ip.address}/${toString ip.prefixLength}";
                 in
@@ -321,7 +299,7 @@ in
           });
 
       in listToAttrs (
-           map configureInterface interfaces ++
+           map configureAddrs interfaces ++
            map createTunDevice (filter (i: i.virtual) interfaces))
          // mapAttrs' createBridgeDevice cfg.bridges
          // mapAttrs' createBondDevice cfg.bonds
diff --git a/nixos/modules/tasks/network-interfaces-systemd.nix b/nixos/modules/tasks/network-interfaces-systemd.nix
index 134e800a26f6..690522a0fe7f 100644
--- a/nixos/modules/tasks/network-interfaces-systemd.nix
+++ b/nixos/modules/tasks/network-interfaces-systemd.nix
@@ -59,15 +59,6 @@ in
         networks."99-main" = genericNetwork mkDefault;
       }
       (mkMerge (flip map interfaces (i: {
-        links."40-${i.name}" = {
-          matchConfig.Name = i.name;
-          linkConfig =
-            (optionalAttrs (i.macAddress != null) {
-              MACAddress = i.macAddress;
-            }) // (optionalAttrs (i.mtu != null) {
-              MTUBytes = toString i.mtu;
-            });
-        };
         netdevs = mkIf i.virtual (
           let
             devType = if i.virtualType != null then i.virtualType
diff --git a/nixos/modules/tasks/network-interfaces.nix b/nixos/modules/tasks/network-interfaces.nix
index 11b641d40eba..6fb6b77adb91 100644
--- a/nixos/modules/tasks/network-interfaces.nix
+++ b/nixos/modules/tasks/network-interfaces.nix
@@ -11,6 +11,10 @@ let
   hasSits = cfg.sits != { };
   hasBonds = cfg.bonds != { };
 
+  # We must escape interfaces due to the systemd interpretation
+  subsystemDevice = interface:
+    "sys-subsystem-net-devices-${escapeSystemdPath interface}.device";
+
   addrOpts = v:
     assert v == 4 || v == 6;
     {
@@ -623,19 +627,42 @@ in
       ++ optional hasVirtuals pkgs.tunctl
       ++ optional cfg.enableIPv6 pkgs.ndisc6;
 
-    systemd.services.network-local-commands = {
-      description = "Extra networking commands.";
-      before = [ "network.target" "network-online.target" ];
-      wantedBy = [ "network.target" "network-online.target" ];
-      unitConfig.ConditionCapability = "CAP_NET_ADMIN";
-      path = [ pkgs.iproute ];
-      serviceConfig.Type = "oneshot";
-      serviceConfig.RemainAfterExit = true;
-      script = ''
-        # Run any user-specified commands.
-        ${cfg.localCommands}
-      '';
-    };
+    systemd.services = {
+      network-local-commands = {
+        description = "Extra networking commands.";
+        before = [ "network.target" "network-online.target" ];
+        wantedBy = [ "network.target" "network-online.target" ];
+        unitConfig.ConditionCapability = "CAP_NET_ADMIN";
+        path = [ pkgs.iproute ];
+        serviceConfig.Type = "oneshot";
+        serviceConfig.RemainAfterExit = true;
+        script = ''
+          # Run any user-specified commands.
+          ${cfg.localCommands}
+        '';
+      };
+    } // (listToAttrs (flip map interfaces (i:
+      nameValuePair "network-link-${i.name}"
+      { description = "Link configuration of ${i.name}";
+        wantedBy = [ "network-interfaces.target" ];
+        before = [ "network-interfaces.target" ];
+        bindsTo = [ (subsystemDevice i.name) ];
+        after = [ (subsystemDevice i.name) ];
+        serviceConfig = {
+          Type = "oneshot";
+          RemainAfterExit = true;
+        };
+        script =
+          ''
+            echo "Configuring link..."
+          '' + optionalString (i.macAddress != null) ''
+            echo "setting MAC address to ${i.macAddress}..."
+            ip link set "${i.name}" address "${i.macAddress}"
+          '' + optionalString (i.mtu != null) ''
+            echo "setting MTU to ${toString i.mtu}..."
+            ip link set "${i.name}" mtu "${toString i.mtu}"
+          '';
+      })));
   };
 
 }