liminix/pkgs/liminix-tools/networking/default.nix

60 lines
2 KiB
Nix
Raw Normal View History

{
callPackage
, liminix
, ifwait
, lib
}:
let
inherit (liminix.services) oneshot longrun;
inherit (lib) concatStringsSep optional;
in {
interface = { type ? "hardware", device, link ? null, primary ? null, id ? null, dependencies ? [] } @ args:
let ups =
[]
++ optional (type == "bridge")
"ip link add name ${device} type bridge"
++ optional (type == "vlan")
"ip link add link ${link} name ${device} type vlan id ${id}"
++ ["${ifwait}/bin/ifwait -v ${device} present"]
++ ["ip link set up dev ${device}"]
++ optional (primary != null)
"ip link set dev ${device} master ${primary.device}";
in oneshot {
name = "${device}.link";
up = lib.concatStringsSep "\n" ups;
down = "ip link set down dev ${device}";
dependencies = dependencies ++ lib.optional (primary != null) primary;
} // {
inherit device;
};
2023-03-01 23:24:58 +01:00
address = interface: { family, dependencies ? [], prefixLength, address } @ args:
let inherit (builtins) toString;
in oneshot {
2023-03-01 23:24:58 +01:00
dependencies = [ interface ] ++ dependencies;
name = "${interface.device}.addr.${address}";
up = "ip address add ${address}/${toString prefixLength} dev ${interface.device} ";
down = "ip address del ${address}/${toString prefixLength} dev ${interface.device} ";
} // {
inherit (interface) device;
};
udhcpc = callPackage ./udhcpc.nix {};
odhcpc = interface: { ... } @ args: longrun {
name = "${interface.device}.odhcp";
run = "odhcpcd ${interface.device}";
};
dnsmasq = callPackage ./dnsmasq.nix {};
hostapd = callPackage ./hostapd.nix {};
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 = ''
ip route add ${target} via ${via} ${with_dev}
2022-09-26 13:55:10 +02:00
'';
down = ''
ip route del ${target} via ${via} ${with_dev}
2022-09-26 13:55:10 +02:00
'';
inherit dependencies;
};
}