summary refs log tree commit diff
path: root/nixos/modules/tasks/network-interfaces-systemd.nix
diff options
context:
space:
mode:
authorMajiir Paktu <majiir@nabaal.net>2023-10-02 19:19:48 -0400
committerMajiir Paktu <majiir@nabaal.net>2023-10-07 16:09:11 -0400
commitc11b788d1a6a2bed6187602a4cf2f3d1542098e7 (patch)
treef64051a5821a4066ee2cdb3b001ff775c34d2727 /nixos/modules/tasks/network-interfaces-systemd.nix
parent088da23f9ea4ed95da0f7842689774c0ab1837a3 (diff)
downloadnixpkgs-c11b788d1a6a2bed6187602a4cf2f3d1542098e7.tar
nixpkgs-c11b788d1a6a2bed6187602a4cf2f3d1542098e7.tar.gz
nixpkgs-c11b788d1a6a2bed6187602a4cf2f3d1542098e7.tar.bz2
nixpkgs-c11b788d1a6a2bed6187602a4cf2f3d1542098e7.tar.lz
nixpkgs-c11b788d1a6a2bed6187602a4cf2f3d1542098e7.tar.xz
nixpkgs-c11b788d1a6a2bed6187602a4cf2f3d1542098e7.tar.zst
nixpkgs-c11b788d1a6a2bed6187602a4cf2f3d1542098e7.zip
nixos/network-interfaces-systemd: support defaultGateway.interface
When interface and address are both specified, we can set Gateway= on
the named interface. The existing logic assumes interface is not set
(since it's guarded by assertion) so we now disable it when interface
has a value.

As a bonus, we now support the defaultGateway.metric option when
interface is set.
Diffstat (limited to 'nixos/modules/tasks/network-interfaces-systemd.nix')
-rw-r--r--nixos/modules/tasks/network-interfaces-systemd.nix27
1 files changed, 19 insertions, 8 deletions
diff --git a/nixos/modules/tasks/network-interfaces-systemd.nix b/nixos/modules/tasks/network-interfaces-systemd.nix
index 53097e21c64..8d1969dad06 100644
--- a/nixos/modules/tasks/network-interfaces-systemd.nix
+++ b/nixos/modules/tasks/network-interfaces-systemd.nix
@@ -28,9 +28,24 @@ 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);
 
+  defaultGateways = mkMerge (forEach [ cfg.defaultGateway cfg.defaultGateway6 ] (gateway:
+    optionalAttrs (gateway != null && gateway.interface != null) {
+      networks."40-${gateway.interface}" = {
+        matchConfig.Name = gateway.interface;
+        routes = [{
+          routeConfig = {
+            Gateway = gateway.address;
+          } // optionalAttrs (gateway.metric != null) {
+            Metric = gateway.metric;
+          };
+        }];
+      };
+    }
+  ));
+
   genericNetwork = override:
-    let gateway = optional (cfg.defaultGateway != null && (cfg.defaultGateway.address or "") != "") cfg.defaultGateway.address
-      ++ optional (cfg.defaultGateway6 != null && (cfg.defaultGateway6.address or "") != "") cfg.defaultGateway6.address;
+    let gateway = optional (cfg.defaultGateway != null && (cfg.defaultGateway.address or "") != "" && cfg.defaultGateway.interface == null) cfg.defaultGateway.address
+      ++ optional (cfg.defaultGateway6 != null && (cfg.defaultGateway6.address or "") != "" && cfg.defaultGateway6.interface == null) cfg.defaultGateway6.address;
         makeGateway = gateway: {
           routeConfig = {
             Gateway = gateway;
@@ -198,6 +213,7 @@ in
     # 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 [
+      defaultGateways
       (genericDhcpNetworks true)
       interfaceNetworks
       bridgeNetworks
@@ -213,12 +229,6 @@ in
     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.";
@@ -233,6 +243,7 @@ in
       mkMerge [ {
         enable = true;
       }
+      defaultGateways
       (genericDhcpNetworks false)
       interfaceNetworks
       bridgeNetworks