{ config, options, lib, pkgs, utils, ... }:
with lib;
with utils;
let
cfg = config.networking;
interfaces = attrValues cfg.interfaces;
hasVirtuals = any (i: i.virtual) interfaces;
hasSits = cfg.sits != { };
hasBonds = cfg.bonds != { };
slaves = concatMap (i: i.interfaces) (attrValues cfg.bonds)
++ concatMap (i: i.interfaces) (attrValues cfg.bridges)
++ concatMap (i: attrNames (filterAttrs (name: config: ! (config.type == "internal" || hasAttr name cfg.interfaces)) i.interfaces)) (attrValues cfg.vswitches);
slaveIfs = map (i: cfg.interfaces.${i}) (filter (i: cfg.interfaces ? ${i}) slaves);
rstpBridges = flip filterAttrs cfg.bridges (_: { rstp, ... }: rstp);
needsMstpd = rstpBridges != { };
bridgeStp = optional needsMstpd (pkgs.writeTextFile {
name = "bridge-stp";
executable = true;
destination = "/bin/bridge-stp";
text = ''
#!${pkgs.runtimeShell} -e
export PATH="${pkgs.mstpd}/bin"
BRIDGES=(${concatStringsSep " " (attrNames rstpBridges)})
for BRIDGE in $BRIDGES; do
if [ "$BRIDGE" = "$1" ]; then
if [ "$2" = "start" ]; then
mstpctl addbridge "$BRIDGE"
exit 0
elif [ "$2" = "stop" ]; then
mstpctl delbridge "$BRIDGE"
exit 0
fi
exit 1
fi
done
exit 1
'';
});
# 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;
{ options = {
address = mkOption {
type = types.str;
description = ''
IPv${toString v} address of the interface. Leave empty to configure the
interface using DHCP.
'';
};
prefixLength = mkOption {
type = types.addCheck types.int (n: n >= 0 && n <= (if v == 4 then 32 else 128));
description = ''
Subnet mask of the interface, specified as the number of
bits in the prefix (<literal>${if v == 4 then "24" else "64"}</literal>).
'';
};
};
};
routeOpts = v:
{ options = {
address = mkOption {
type = types.str;
description = "IPv${toString v} address of the network.";
};
prefixLength = mkOption {
type = types.addCheck types.int (n: n >= 0 && n <= (if v == 4 then 32 else 128));
description =