1
0
liminix/modules/network/route.nix

31 lines
773 B
Nix
Raw Normal View History

{
liminix,
lib,
}:
{
target,
via,
interface ? null,
metric,
}:
let
inherit (liminix.services) oneshot;
2023-09-13 17:01:50 +00:00
with_dev = if interface != null then "dev $(output ${interface} ifname)" else "";
2024-06-20 09:15:54 +00:00
target_hash = builtins.substring 0 12 (builtins.hashString "sha256" target);
via_hash = builtins.substring 0 12 (builtins.hashString "sha256" via);
in
oneshot {
name = "route-${target_hash}-${
builtins.substring 0 12 (
builtins.hashString "sha256" "${via_hash}-${if interface != null then interface.name else ""}"
)
}";
up = ''
ip route add ${target} via ${via} metric ${toString metric} ${with_dev}
'';
down = ''
ip route del ${target} via ${via} ${with_dev}
'';
dependencies = [ ] ++ lib.optional (interface != null) interface;
}