nixos/network-interfaces: add option to set source address
This commit is contained in:
@@ -42,6 +42,14 @@ let
|
||||
ip link del dev "${i}" 2>/dev/null || true
|
||||
'';
|
||||
|
||||
formatIpArgs =
|
||||
args:
|
||||
lib.pipe args [
|
||||
(lib.filterAttrs (n: v: v != null))
|
||||
(lib.mapAttrsToList (n: v: "${n} ${toString v}"))
|
||||
(lib.concatStringsSep " ")
|
||||
];
|
||||
|
||||
# warn that these attributes are deprecated (2017-2-2)
|
||||
# Should be removed in the release after next
|
||||
bondDeprecation = rec {
|
||||
@@ -160,39 +168,41 @@ let
|
||||
EOF
|
||||
''}
|
||||
|
||||
# Set the default gateway.
|
||||
${optionalString (cfg.defaultGateway != null && cfg.defaultGateway.address != "") ''
|
||||
${optionalString (cfg.defaultGateway.interface != null) ''
|
||||
ip route replace ${cfg.defaultGateway.address} dev ${cfg.defaultGateway.interface} ${
|
||||
optionalString (cfg.defaultGateway.metric != null) "metric ${toString cfg.defaultGateway.metric}"
|
||||
} proto static
|
||||
''}
|
||||
ip route replace default ${
|
||||
optionalString (cfg.defaultGateway.metric != null) "metric ${toString cfg.defaultGateway.metric}"
|
||||
} via "${cfg.defaultGateway.address}" ${
|
||||
optionalString (
|
||||
cfg.defaultGatewayWindowSize != null
|
||||
) "window ${toString cfg.defaultGatewayWindowSize}"
|
||||
} ${
|
||||
optionalString (cfg.defaultGateway.interface != null) "dev ${cfg.defaultGateway.interface}"
|
||||
} proto static
|
||||
''}
|
||||
${optionalString (cfg.defaultGateway6 != null && cfg.defaultGateway6.address != "") ''
|
||||
${optionalString (cfg.defaultGateway6.interface != null) ''
|
||||
ip -6 route replace ${cfg.defaultGateway6.address} dev ${cfg.defaultGateway6.interface} ${
|
||||
optionalString (cfg.defaultGateway6.metric != null) "metric ${toString cfg.defaultGateway6.metric}"
|
||||
} proto static
|
||||
''}
|
||||
ip -6 route replace default ${
|
||||
optionalString (cfg.defaultGateway6.metric != null) "metric ${toString cfg.defaultGateway6.metric}"
|
||||
} via "${cfg.defaultGateway6.address}" ${
|
||||
optionalString (
|
||||
cfg.defaultGatewayWindowSize != null
|
||||
) "window ${toString cfg.defaultGatewayWindowSize}"
|
||||
} ${
|
||||
optionalString (cfg.defaultGateway6.interface != null) "dev ${cfg.defaultGateway6.interface}"
|
||||
} proto static
|
||||
# Set the default gateway
|
||||
${flip concatMapStrings
|
||||
[
|
||||
{
|
||||
version = "-4";
|
||||
gateway = cfg.defaultGateway;
|
||||
}
|
||||
{
|
||||
version = "-6";
|
||||
gateway = cfg.defaultGateway6;
|
||||
}
|
||||
]
|
||||
(
|
||||
{ version, gateway }:
|
||||
optionalString (gateway != null && gateway.address != "") ''
|
||||
${optionalString (gateway.interface != null) ''
|
||||
ip ${version} route replace ${gateway.address} proto static ${
|
||||
formatIpArgs {
|
||||
metric = gateway.metric;
|
||||
dev = gateway.interface;
|
||||
}
|
||||
}
|
||||
''}
|
||||
ip ${version} route replace default proto static ${
|
||||
formatIpArgs {
|
||||
metric = gateway.metric;
|
||||
via = gateway.address;
|
||||
window = cfg.defaultGatewayWindowSize;
|
||||
dev = gateway.interface;
|
||||
src = gateway.source;
|
||||
}
|
||||
}
|
||||
''
|
||||
)
|
||||
}
|
||||
'';
|
||||
};
|
||||
|
||||
|
||||
@@ -46,6 +46,9 @@ let
|
||||
// optionalAttrs (gateway.metric != null) {
|
||||
Metric = gateway.metric;
|
||||
}
|
||||
// optionalAttrs (gateway.source != null) {
|
||||
PreferredSource = gateway.source;
|
||||
}
|
||||
)
|
||||
];
|
||||
};
|
||||
|
||||
@@ -180,6 +180,12 @@ let
|
||||
description = "The default gateway metric/preference.";
|
||||
};
|
||||
|
||||
source = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
description = "The default source address.";
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
@@ -656,6 +662,7 @@ in
|
||||
example = {
|
||||
address = "131.211.84.1";
|
||||
interface = "enp3s0";
|
||||
source = "131.211.84.2";
|
||||
};
|
||||
type = types.nullOr (types.coercedTo types.str gatewayCoerce (types.submodule gatewayOpts));
|
||||
description = ''
|
||||
@@ -669,6 +676,7 @@ in
|
||||
example = {
|
||||
address = "2001:4d0:1e04:895::1";
|
||||
interface = "enp3s0";
|
||||
source = "2001:4d0:1e04:895::2";
|
||||
};
|
||||
type = types.nullOr (types.coercedTo types.str gatewayCoerce (types.submodule gatewayOpts));
|
||||
description = ''
|
||||
|
||||
@@ -39,11 +39,23 @@ let
|
||||
defaultGateway = {
|
||||
address = "192.168.1.1";
|
||||
interface = "enp1s0";
|
||||
source = "192.168.1.3";
|
||||
};
|
||||
defaultGateway6 = {
|
||||
address = "fd00:1234:5678:1::1";
|
||||
interface = "enp1s0";
|
||||
source = "fd00:1234:5678:1::3";
|
||||
};
|
||||
interfaces.enp1s0.ipv6.addresses = [
|
||||
{
|
||||
address = "fd00:1234:5678:1::2";
|
||||
prefixLength = 64;
|
||||
}
|
||||
{
|
||||
address = "fd00:1234:5678:1::3";
|
||||
prefixLength = 128;
|
||||
}
|
||||
];
|
||||
interfaces.enp1s0.ipv4.addresses = [
|
||||
{
|
||||
address = "192.168.1.2";
|
||||
@@ -89,7 +101,11 @@ let
|
||||
|
||||
with subtest("Test default gateway"):
|
||||
client.wait_until_succeeds("ping -c 1 192.168.3.1")
|
||||
client.wait_until_succeeds("ping -c 1 fd00:1234:5678:3::1")
|
||||
client.wait_until_succeeds("ping -c 1 fd00:1234:5678:1::1")
|
||||
|
||||
with subtest("Test default addresses"):
|
||||
client.succeed("ip -4 route show default | grep -q 'src 192.168.1.3'")
|
||||
client.succeed("ip -6 route show default | grep -q 'src fd00:1234:5678:1::3'")
|
||||
'';
|
||||
};
|
||||
routeType = {
|
||||
|
||||
Reference in New Issue
Block a user