2023-01-17 08:05:11 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
# Take note this service is heavily based on the hardware networking setup of
|
|
|
|
# this host so better stay focused on the hardware configuration on this host.
|
|
|
|
let
|
|
|
|
acmeName = "wireguard.${config.networking.domain}";
|
2023-01-19 12:16:01 +00:00
|
|
|
inherit (builtins) toString;
|
2023-02-08 11:05:23 +00:00
|
|
|
inherit (import ../hardware/networks.nix) interfaces wireguardPort wireguardPeers;
|
2023-01-17 08:05:11 +00:00
|
|
|
|
2023-06-11 06:11:35 +00:00
|
|
|
wireguardIFName = interfaces.wireguard0.ifname;
|
2023-06-22 10:01:19 +00:00
|
|
|
lanIFName = interfaces.lan.ifname;
|
2023-01-23 05:29:42 +00:00
|
|
|
|
2023-02-10 15:45:11 +00:00
|
|
|
desktopPeerAddresses = with wireguardPeers.desktop; [ "${IPv4}/32" "${IPv6}/128" ];
|
|
|
|
phonePeerAddresses = with wireguardPeers.phone; [ "${IPv4}/32" "${IPv6}/128" ];
|
2023-02-06 11:32:55 +00:00
|
|
|
|
2023-01-17 08:05:11 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
environment.systemPackages = [ pkgs.wireguard-tools ];
|
|
|
|
|
2023-06-08 11:52:29 +00:00
|
|
|
networking.firewall = {
|
|
|
|
# Allow the UDP traffic for the Wireguard service.
|
|
|
|
allowedUDPPorts = [ wireguardPort ];
|
|
|
|
|
|
|
|
# IP forwarding for specific interfaces.
|
|
|
|
filterForward = true;
|
2023-06-11 06:11:35 +00:00
|
|
|
extraForwardRules = ''
|
2023-06-13 05:20:00 +00:00
|
|
|
iifname ${wireguardIFName} accept comment "IP forward from Wireguard interface to LAN"
|
2023-06-11 06:11:35 +00:00
|
|
|
'';
|
2023-06-08 11:52:29 +00:00
|
|
|
};
|
2023-01-17 08:05:11 +00:00
|
|
|
|
2023-06-11 06:11:35 +00:00
|
|
|
networking.nftables.ruleset = ''
|
|
|
|
table ip wireguard-${wireguardIFName} {
|
2023-06-13 05:20:00 +00:00
|
|
|
chain prerouting {
|
|
|
|
type nat hook prerouting priority filter; policy accept;
|
|
|
|
}
|
|
|
|
|
2023-06-11 06:11:35 +00:00
|
|
|
chain postrouting {
|
|
|
|
type nat hook postrouting priority srcnat; policy accept;
|
2023-06-22 15:17:28 +00:00
|
|
|
iifname ${wireguardIFName} snat to ${interfaces.lan.IPv4.address} comment "Make packets from Wireguard interface appear as coming from the LAN interface"
|
2023-06-11 06:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
2023-06-08 11:53:20 +00:00
|
|
|
# Since we're using systemd-networkd to configure interfaces, we can control
|
|
|
|
# how each interface can handle things such as IP masquerading so no need for
|
|
|
|
# modifying sysctl settings like 'ipv4.ip_forward' or similar.
|
2023-01-17 08:05:11 +00:00
|
|
|
systemd.network = {
|
2023-02-13 01:51:30 +00:00
|
|
|
wait-online.ignoredInterfaces = [ wireguardIFName ];
|
|
|
|
|
2023-01-17 08:05:11 +00:00
|
|
|
netdevs."99-${wireguardIFName}" = {
|
|
|
|
netdevConfig = {
|
|
|
|
Name = wireguardIFName;
|
|
|
|
Kind = "wireguard";
|
|
|
|
};
|
|
|
|
|
|
|
|
wireguardConfig = {
|
|
|
|
PrivateKeyFile = config.sops.secrets."plover/wireguard/private-key".path;
|
|
|
|
ListenPort = wireguardPort;
|
|
|
|
};
|
|
|
|
|
|
|
|
wireguardPeers = [
|
|
|
|
# Desktop workstation.
|
|
|
|
{
|
|
|
|
wireguardPeerConfig = {
|
|
|
|
PublicKey = lib.readFile ../../../ni/files/wireguard/wireguard-public-key-ni;
|
|
|
|
PresharedKeyFile = config.sops.secrets."plover/wireguard/preshared-keys/ni".path;
|
2023-01-23 09:46:32 +00:00
|
|
|
AllowedIPs = lib.concatStringsSep "," desktopPeerAddresses;
|
2023-01-17 08:05:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
# Phone.
|
|
|
|
{
|
|
|
|
wireguardPeerConfig = {
|
|
|
|
PublicKey = lib.readFile ../../files/wireguard/wireguard-public-key-phone;
|
|
|
|
PresharedKeyFile = config.sops.secrets."plover/wireguard/preshared-keys/phone".path;
|
2023-01-23 09:46:32 +00:00
|
|
|
AllowedIPs = lib.concatStringsSep "," phonePeerAddresses;
|
2023-01-17 08:05:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2023-06-27 04:54:29 +00:00
|
|
|
networks."99-${wireguardIFName}" = with interfaces.wireguard0; {
|
|
|
|
matchConfig.Name = ifname;
|
2023-02-06 11:32:55 +00:00
|
|
|
|
2023-06-27 04:54:29 +00:00
|
|
|
address = [
|
2023-02-10 15:45:11 +00:00
|
|
|
"${IPv4.address}/14"
|
|
|
|
"${IPv6.address}/64"
|
2023-01-23 09:46:32 +00:00
|
|
|
];
|
2023-06-27 04:54:29 +00:00
|
|
|
|
|
|
|
routes = [
|
|
|
|
{ routeConfig.Gateway = IPv4.gateway; }
|
|
|
|
];
|
2023-01-17 08:05:11 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|