dotfiles/nixos/systems/hela/patches.nix
magic_rb c234160635
Border router, hela
Signed-off-by: magic_rb <magic_rb@redalder.org>
2024-09-04 22:39:43 +02:00

53 lines
1.4 KiB
Nix

{
config,
lib,
pkgs,
...
}: {
systemd.services.ifstate-patch-pre = {
partOf = ["ifstate.service"];
wantedBy = ["ifstate.service"];
before = ["ifstate.service"];
path = [
pkgs.jq
pkgs.iproute2
];
script = ''
# create all network namespaces beforehand
${lib.concatMapStringsSep "\n" (
name: ''
if ! [ -f "/var/run/netns/${name}" ] ; then
ip netns add ${name}
fi
''
) (lib.attrNames config.services.ifstate.settings.namespaces)}
# move all dsa interfaces into their respective namespaces
${lib.concatMapStringsSep "\n" (
interface: ''
ifname="$(ip -json link | jq -r '.[] | select(.address == "${interface.link.address}") | .ifname')"
if ! [ -z "$ifname" ] ; then
ip link set "$ifname" netns ${interface.namespace}
fi
''
) (lib.pipe config.services.ifstate.settings.namespaces [
(lib.mapAttrs (_: settings: settings.interfaces or []))
lib.attrsToList
(lib.concatMap ({
name,
value,
}:
lib.pipe value [
(lib.filter (interface: interface.link.kind == "dsa" && interface.link ? "address"))
(map (interface: interface // {namespace = name;}))
]))
])}
'';
serviceConfig.Type = "oneshot";
};
}