1 { pkgs, lib, config, inputs, ... }:
3 inherit (config.boot) initrd;
6 wg = config.networking.wireguard.interfaces.${wgIface};
7 wg-intra-peers = import (inputs.julm-nix + "/nixos/profiles/wireguard/wg-intra/peers.nix");
8 relay = wg-intra-peers.mermet;
12 (inputs.julm-nix + "/nixos/profiles/wireguard/wg-intra.nix")
14 networking.wireguard.${wgIface}.peers = {
18 carotte.enable = true;
19 aubergine.enable = true;
21 systemd.services."wireguard-${wgIface}" = {
22 unitConfig.Upholds = [ "upnpc-${toString wg.listenPort}.service" ];
24 networking.nftables.ruleset = ''
27 tcp dport ssh counter accept comment "SSH"
28 udp dport 60000-61000 counter accept comment "Mosh"
31 tcp dport { ssh, 2222 } counter accept comment "SSH"
32 udp dport 60001-60010 counter accept comment "Mosh"
33 tcp dport { http, https } counter accept comment "HTTP"
34 tcp dport git counter accept comment "Git"
39 iifname ${wgIface} oifname netIface masquerade
43 # Apparently required to get NAT reflection.
44 services.upnpc.redirections = [
45 { description = "WireGuard"; externalPort = wg.listenPort; protocol = "UDP"; duration = 30 * 60;
46 service.requiredBy = [ "wireguard-${wgIface}.service" ];
47 service.before = [ "wireguard-${wgIface}.service" ];
50 boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
52 # Open a wireguard tunnel to a relay
53 # in case the host is hosted behind a NAT and has no SSH port forwarding.
54 # This enables to send the disk password to the initrd, like that:
55 # ssh -J mermet.sourcephile.fr root@losurdo.wg -p 2222
56 boot.initrd.secrets."/root/initrd/${wgIface}.key" = "/root/initrd/${wgIface}.key";
57 boot.initrd.kernelModules = [ "wireguard" ];
58 boot.initrd.extraUtilsCommands = ''
59 #copy_bin_and_libs ${pkgs.wireguard-tools}/bin/wg
60 cp -fpdv ${pkgs.wireguard-tools}/bin/.wg-wrapped $out/bin/wg
62 boot.initrd.network.postCommands = ''
63 ip link add dev ${wgIface} type wireguard
64 ${lib.concatMapStringsSep "\n" (ip: ''
65 ip address add ${ip} dev ${wgIface}
67 wg set ${wgIface} private-key /root/initrd/${wgIface}.key \
68 listen-port ${toString wg.listenPort}
69 ip link set up dev ${wgIface} mtu 1280
70 wg set ${wgIface} peer ${relay.peer.publicKey} \
71 endpoint ${relay.ipv4}:${toString relay.listenPort} \
72 allowed-ips ${relay.ipv4}/32 \
73 persistent-keepalive 5
74 ip route replace ${relay.ipv4}/32 dev ${wgIface} table main
76 boot.initrd.postMountCommands = lib.mkIf initrd.network.flushBeforeStage2 ''
77 ip link del dev ${wgIface}