os/modules/wireguard/private.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
3 KiB
Nix
Raw Normal View History

2023-10-19 18:55:56 +00:00
{
lib,
config,
pkgs,
...
2024-08-18 22:22:59 +00:00
}: let
2024-02-12 15:46:46 +00:00
cfg = config.pub-solar.wireguard.private;
2023-10-19 18:55:56 +00:00
in {
2024-02-12 15:46:46 +00:00
options.pub-solar.wireguard.private = {
ownIPs = lib.mkOption {
2023-10-19 18:55:56 +00:00
description = ''
Internal ips in wireguard used for cluster control-plane communication.
'';
type = lib.types.listOf lib.types.str;
2024-02-12 15:46:46 +00:00
default = [];
2023-10-19 18:55:56 +00:00
};
privateKeyFile = lib.mkOption {
2023-10-19 18:55:56 +00:00
description = ''
Location of private key file
'';
type = lib.types.path;
2023-10-19 18:55:56 +00:00
};
useDNS = lib.mkOption {
description = ''
Whether to use the wireguard DNS
'';
default = true;
type = lib.types.bool;
};
fullTunnel = lib.mkOption {
description = ''
Whether to tunnel all traffic through the wireguard VPN
'';
default = false;
type = lib.types.bool;
};
2023-10-19 18:55:56 +00:00
};
config = lib.mkIf (builtins.length cfg.ownIPs != 0) {
2023-10-19 18:55:56 +00:00
networking.firewall.allowedUDPPorts = [51899];
2024-08-18 22:22:59 +00:00
systemd.network.wait-online.ignoredInterfaces = ["wg-private"];
systemd.services.wireguard-wg-private = import ./service-override.nix lib;
networking.wireguard.interfaces = {
2024-02-12 15:46:46 +00:00
wg-private = {
listenPort = 51899;
mtu = 1300;
ips = cfg.ownIPs;
2024-02-12 15:46:46 +00:00
privateKeyFile = cfg.privateKeyFile;
2024-08-18 22:22:59 +00:00
postSetup =
""
+ (
if cfg.useDNS
then ''
printf "nameserver 10.13.12.7\nnameserver fd00:b12f:acab:1312:acab:7::" | resolvconf -a wg-private -m 0 -x
''
else ""
)
+ (
if cfg.fullTunnel
then ''
defaultRoute=$(${pkgs.iproute2}/bin/ip r | ${pkgs.gnugrep}/bin/grep "default via" | head -n 1 | ${pkgs.gawk}/bin/awk '{ print $3 " " $4 " " $5 }')
ipv4=$(${pkgs.dnsutils}/bin/dig +short A vpn.b12f.io)
${pkgs.iproute2}/bin/ip route add $ipv4 metric 256 via $defaultRoute
ipv6=$(${pkgs.dnsutils}/bin/dig +short AAAA vpn.b12f.io)
${pkgs.iproute2}/bin/ip route add $ipv6 metric 256 via $defaultRoute
ip -4 route delete default dev wg-private || true
ip -4 route replace default dev wg-private metric 512
ip -6 route delete default dev wg-private || true
ip -6 route replace default dev wg-private metric 512
''
else ""
);
2024-02-06 08:44:41 +00:00
postShutdown = lib.mkIf cfg.useDNS ''
2024-02-12 15:46:46 +00:00
resolvconf -d wg-private -f
'';
2023-10-19 18:55:56 +00:00
peers = [
{
2023-10-24 15:56:14 +00:00
# frikandel
publicKey = "p6YKNYBlySKfhTN+wbSsKdoNjzko/XSAiTAlCJzP1jA=";
2024-08-18 22:22:59 +00:00
allowedIPs =
[
"10.13.12.0/24"
"fd00:b12f:acab:1312::/64"
]
++ (
if cfg.fullTunnel
then [
"0.0.0.0/0"
"::/0"
]
else []
);
endpoint = "vpn.b12f.io:51899";
dynamicEndpointRefreshSeconds = 30;
2023-10-19 18:55:56 +00:00
}
];
};
};
};
}