2022-09-26 13:26:54 +02:00
|
|
|
{
|
|
|
|
callPackage
|
|
|
|
, liminix
|
2023-03-01 18:55:52 +01:00
|
|
|
, ifwait
|
2023-02-25 00:49:05 +01:00
|
|
|
, lib
|
2023-08-28 00:38:59 +02:00
|
|
|
, serviceFns
|
2022-09-26 13:26:54 +02:00
|
|
|
}:
|
2023-02-25 00:49:05 +01:00
|
|
|
let
|
|
|
|
inherit (liminix.services) oneshot longrun;
|
|
|
|
inherit (lib) concatStringsSep optional;
|
2023-08-28 00:38:59 +02:00
|
|
|
ifup = name : ifname : ''
|
|
|
|
. ${serviceFns}
|
|
|
|
${ifwait}/bin/ifwait -v ${ifname} present
|
|
|
|
ip link set up dev ${ifname}
|
|
|
|
(in_outputs ${name}
|
|
|
|
echo ${ifname} > ifname
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
|
2022-09-26 13:26:54 +02:00
|
|
|
in {
|
2023-03-07 20:10:12 +01:00
|
|
|
interface = { type ? "hardware", device, link ? null, primary ? null, id ? null, dependencies ? [] } @ args:
|
2023-08-27 23:34:00 +02:00
|
|
|
let name = "${device}.link";
|
|
|
|
ups =
|
2023-02-25 00:49:05 +01:00
|
|
|
[]
|
|
|
|
++ optional (type == "bridge")
|
|
|
|
"ip link add name ${device} type bridge"
|
2023-03-07 20:10:12 +01:00
|
|
|
++ optional (type == "vlan")
|
|
|
|
"ip link add link ${link} name ${device} type vlan id ${id}"
|
2023-08-28 00:38:59 +02:00
|
|
|
++ [(ifup name device)]
|
2023-02-25 00:49:05 +01:00
|
|
|
++ optional (primary != null)
|
|
|
|
"ip link set dev ${device} master ${primary.device}";
|
|
|
|
in oneshot {
|
2023-08-27 23:34:00 +02:00
|
|
|
inherit name;
|
2023-02-25 00:49:05 +01:00
|
|
|
up = lib.concatStringsSep "\n" ups;
|
|
|
|
down = "ip link set down dev ${device}";
|
|
|
|
dependencies = dependencies ++ lib.optional (primary != null) primary;
|
|
|
|
};
|
2023-08-28 00:38:59 +02:00
|
|
|
inherit ifup;
|
2023-03-01 23:24:58 +01:00
|
|
|
address = interface: { family, dependencies ? [], prefixLength, address } @ args:
|
2022-09-26 13:26:54 +02:00
|
|
|
let inherit (builtins) toString;
|
|
|
|
in oneshot {
|
2023-03-01 23:24:58 +01:00
|
|
|
dependencies = [ interface ] ++ dependencies;
|
2023-08-28 00:20:58 +02:00
|
|
|
name = "${interface.name}.addr.${address}";
|
|
|
|
up = "ip address add ${address}/${toString prefixLength} dev $(output ${interface} ifname)";
|
|
|
|
down = "ip address del ${address}/${toString prefixLength} dev $(output ${interface} ifname)";
|
2022-09-26 13:26:54 +02:00
|
|
|
};
|
2023-06-30 11:17:33 +02:00
|
|
|
route = { name, target, via, dependencies, dev ? null }:
|
|
|
|
let with_dev = if dev != null then "dev ${dev}" else "";
|
|
|
|
in oneshot {
|
2022-09-26 13:55:10 +02:00
|
|
|
inherit name;
|
|
|
|
up = ''
|
2023-06-30 11:17:33 +02:00
|
|
|
ip route add ${target} via ${via} ${with_dev}
|
2022-09-26 13:55:10 +02:00
|
|
|
'';
|
|
|
|
down = ''
|
2023-06-30 11:17:33 +02:00
|
|
|
ip route del ${target} via ${via} ${with_dev}
|
2022-09-26 13:55:10 +02:00
|
|
|
'';
|
|
|
|
inherit dependencies;
|
|
|
|
};
|
2022-09-26 13:26:54 +02:00
|
|
|
}
|