1 { pkgs, lib, config, inputs, ... }:
3 inherit (config.boot) initrd;
5 wg = config.networking.wireguard.interfaces.${wgIface};
6 wg-intra-peers = import (inputs.julm-nix + "/nixos/profiles/wireguard/wg-intra/peers.nix");
7 relay = wg-intra-peers.mermet;
11 (inputs.julm-nix + "/nixos/profiles/wireguard/wg-intra.nix")
13 systemd.services."wireguard-${wgIface}" = {
15 LoadCredentialEncrypted = [ "privateKey:${./. + "/${wgIface}/privateKey.cred"}" ];
18 Upholds = [ "upnpc-${toString wg.listenPort}.service" ];
21 networking.wireguard.${wgIface}.peers = {
25 carotte.enable = true;
26 aubergine.enable = true;
28 networking.nftables.ruleset = ''
31 tcp dport ssh counter accept comment "SSH"
32 udp dport 60000-61000 counter accept comment "Mosh"
35 tcp dport { ssh, 2222 } counter accept comment "SSH"
36 udp dport 60001-60010 counter accept comment "Mosh"
37 tcp dport { http, https } counter accept comment "HTTP"
38 tcp dport git counter accept comment "Git"
43 iifname ${wgIface} oifname netIface masquerade
47 # Apparently required to get NAT reflection.
48 services.upnpc.enable = true;
49 services.upnpc.redirections = [
51 description = "WireGuard";
52 externalPort = wg.listenPort;
55 service.requiredBy = [ "wireguard-${wgIface}.service" ];
56 service.before = [ "wireguard-${wgIface}.service" ];
59 boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
61 # Open a wireguard tunnel to a relay
62 # in case the host is hosted behind a NAT and has no SSH port forwarding.
63 # This enables to send the disk password to the initrd, like that:
64 # ssh -J mermet.wg root@losurdo.wg -p 2222
65 # TODO: use a dedicated interface wg-initrd
66 boot.initrd.secrets."/root/initrd/${wgIface}.key" = "/run/credentials/wireguard-${wgIface}.service/privateKey";
67 boot.initrd.kernelModules = [ "wireguard" ];
77 PrivateKeyFile = "/root/initrd/${wgIface}.key";
78 ListenPort = wg.listenPort;
82 wireguardPeerConfig = {
83 PublicKey = relay.peer.publicKey;
84 AllowedIPs = [ "${relay.ipv4}/32" ];
85 PersistentKeepalive = 5;