nix/shared/bastion/wireguard.nix
2025-07-22 00:05:56 +02:00

47 lines
No EOL
1.3 KiB
Nix

{ config, lib, pkgs, ... }:
let
# Import users wireguard sessions
users-wg = import ./../users-wg.nix;
# Import nodes
nodes = import ./../../nodes.nix;
myPeer = nodes."${config.hostName}";
myZone = myPeer.zone;
buildSecret = zone: {
"wg-private-zone-${toString zone}" = {
file = ./../../secrets/bastion + ( "/wg-private-zone-" + toString zone + ".age" );
owner = "root";
group = "root";
};
};
peers = map (peerConfig: {
name = "${peerConfig.name}";
publicKey = peerConfig.publicKey;
allowedIPs = [
"172.19.${toString (myZone + 127)}.${toString peerConfig.id}/32"
"fc00:f::${toString (myZone + 127)}:${toString peerConfig.id}/128"
];
persistentKeepalive = 25;
}) users-wg;
interface = {
"mgmt" = {
ips = [
"172.19.${toString (myZone + 127)}.254/24"
"fc00:f::${toString (myZone + 127)}:254/96"
];
privateKeyFile = config.age.secrets."wg-private-zone-${toString myZone}".path;
listenPort = 51920;
peers = peers;
};
};
in
{
age.secrets = buildSecret myZone;
# Return all WireGuard interfaces for each node
networking.wireguard.interfaces = interface;
}