nixfiles/profiles/chaosInternalWireGuard/wireguard.nix

52 lines
1.4 KiB
Nix
Raw Normal View History

{
2023-09-18 03:56:58 +01:00
self,
lib,
config,
...
}: let
inherit (lib.modules) mkIf;
inherit (lib.lists) filter;
inherit (builtins) hasAttr attrNames;
# Assume this to be set
secrets = config.services.secrets.secrets;
2023-09-18 03:56:58 +01:00
wireguardData = import "${self}/data/chaosInternalWireGuard.nix";
wireguardHosts = wireguardData.hosts;
currentHostName = config.networking.hostName;
2023-09-18 03:56:58 +01:00
currentHostConfig = wireguardHosts.${currentHostName};
in {
2023-09-18 03:56:58 +01:00
networking.firewall.trustedInterfaces = ["wg0"];
networking.firewall.allowPing = true;
networking.firewall.allowedUDPPorts = mkIf (hasAttr "endpoint" currentHostConfig) [51820];
systemd.services.wireguard-debug = {
wantedBy = ["multi-user.target"];
partOf = ["wg-quick-wg0.service"];
script = ''
echo module wireguard +p > /sys/kernel/debug/dynamic_debug/control
'';
};
networking.wg-quick.interfaces = {
wg0 = {
address = ["${currentHostConfig.ip}/24"];
privateKeyFile = "${secrets.wg_private.path}";
listenPort = mkIf (hasAttr "endpoint" currentHostConfig) 51820;
peers = (map (
hostName: let
host = wireguardHosts.${hostName};
in {
allowedIPs = ["${host.ip}/32"];
publicKey = host.public;
endpoint = host.endpoint or null;
}
) (filter (
hostName: hostName != currentHostName
) (attrNames wireguardHosts)));
};
};
}