2023-12-16 15:59:33 +00:00
|
|
|
index: { lib, pkgs, config, ... }:
|
2023-11-25 17:18:34 +00:00
|
|
|
let
|
2023-12-16 15:59:33 +00:00
|
|
|
inherit (builtins) attrNames concatMap;
|
|
|
|
inherit (lib) optional;
|
2023-11-25 17:18:34 +00:00
|
|
|
inherit (lib.my) net;
|
|
|
|
inherit (lib.my.c.home) prefixes vips;
|
|
|
|
|
2023-11-26 01:29:44 +00:00
|
|
|
vlanIface = vlan: if vlan == "as211024" then vlan else "lan-${vlan}";
|
2023-12-16 15:59:33 +00:00
|
|
|
vrrpIPs = family: concatMap (vlan: [
|
|
|
|
{
|
|
|
|
addr = "${vips.${vlan}.${family}}/${toString (net.cidr.length prefixes.${vlan}.${family})}";
|
|
|
|
dev = vlanIface vlan;
|
|
|
|
}
|
|
|
|
] ++ (optional (family == "v6") {
|
|
|
|
addr = "fe80::1/64";
|
2023-11-26 01:29:44 +00:00
|
|
|
dev = vlanIface vlan;
|
2023-12-16 15:59:33 +00:00
|
|
|
})) (attrNames vips);
|
2023-11-25 17:18:34 +00:00
|
|
|
mkVRRP = family: routerId: {
|
|
|
|
state = if index == 0 then "MASTER" else "BACKUP";
|
|
|
|
interface = "lan-core";
|
|
|
|
priority = 255 - index;
|
|
|
|
virtualRouterId = routerId;
|
|
|
|
virtualIps = vrrpIPs family;
|
2023-12-16 15:59:33 +00:00
|
|
|
extraConfig = ''
|
|
|
|
notify_master "${config.systemd.package}/bin/systemctl start radvd.service"
|
|
|
|
notify_backup "${config.systemd.package}/bin/systemctl stop radvd.service"
|
|
|
|
'';
|
2023-11-25 17:18:34 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
services = {
|
|
|
|
keepalived = {
|
|
|
|
enable = true;
|
|
|
|
extraGlobalDefs = ''
|
|
|
|
vrrp_version 3
|
|
|
|
nftables keepalived
|
|
|
|
'';
|
|
|
|
vrrpInstances = {
|
|
|
|
v4 = mkVRRP "v4" 51;
|
|
|
|
v6 = mkVRRP "v6" 52;
|
|
|
|
};
|
|
|
|
extraConfig = ''
|
|
|
|
vrrp_sync_group main {
|
|
|
|
group {
|
|
|
|
v4
|
|
|
|
v6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|