1 { pkgs, lib, config, inputs, ... }:
3 inherit (config.boot) initrd;
5 wg = config.networking.wireguard.interfaces.${iface};
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 networking.wireguard.${iface}.peers = {
17 carotte.enable = true;
19 systemd.services."wireguard-${iface}" = {
20 unitConfig.Upholds = [ "upnpc-${toString wg.listenPort}.service" ];
22 networking.nftables.ruleset = ''
23 # Allow initiating connection to and from other peers
24 add rule inet filter fw2net udp sport ${toString wg.listenPort} counter accept comment "WireGuard ${iface} output to peers"
25 add rule inet filter net2fw udp dport ${toString wg.listenPort} counter accept comment "WireGuard ${iface} input from peers"
27 # Hook ${iface} into relevant chains
28 add chain inet filter intra2fw
29 #add rule inet filter intra2fw counter accept
30 add rule inet filter input iifname "${iface}" jump intra2fw
31 add rule inet filter input iifname "${iface}" log level warn prefix "intra2fw: " counter drop
32 add chain inet filter fw2intra
33 add rule inet filter output oifname "${iface}" jump fw2intra
34 add rule inet filter output oifname "${iface}" log level warn prefix "fw2intra: " counter drop
36 # ${iface} firewalling
37 #add rule inet filter fw2intra counter accept
38 add rule inet filter fw2intra counter accept
39 ${lib.concatMapStringsSep "\n" (ip: ''
40 add rule inet filter intra2fw ip saddr ${ip} counter accept comment "relay"
43 add chain inet filter fwd-intra
44 #add rule inet filter fwd-intra counter accept
45 add rule inet filter forward iifname "${iface}" jump fwd-intra
47 # Apparently required to get NAT reflection.
48 services.upnpc.redirections = [
49 { description = "WireGuard"; externalPort = wg.listenPort; protocol = "UDP"; duration = 30 * 60;
50 service.requiredBy = [ "wireguard-${iface}.service" ];
51 service.before = [ "wireguard-${iface}.service" ];
54 boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
56 # Open a wireguard tunnel to a relay
57 # in case the host is hosted behind a NAT and has no SSH port forwarding.
58 # This enables to send the disk password to the initrd, like that:
59 # ssh -J mermet.sourcephile.fr root@losurdo.wg -p 2222
60 boot.initrd.secrets."/root/initrd/${iface}.key" = "/root/initrd/${iface}.key";
61 boot.initrd.kernelModules = [ "wireguard" ];
62 boot.initrd.extraUtilsCommands = ''
63 #copy_bin_and_libs ${pkgs.wireguard-tools}/bin/wg
64 cp -fpdv ${pkgs.wireguard-tools}/bin/.wg-wrapped $out/bin/wg
66 boot.initrd.network.postCommands = ''
67 ip link add dev ${iface} type wireguard
68 ${lib.concatMapStringsSep "\n" (ip: ''
69 ip address add ${ip} dev ${iface}
71 wg set ${iface} private-key /root/initrd/${iface}.key \
72 listen-port ${toString wg.listenPort}
73 ip link set up dev ${iface} mtu 1280
74 wg set ${iface} peer ${relay.peer.publicKey} \
75 endpoint ${relay.ipv4}:${toString relay.listenPort} \
76 allowed-ips ${relay.ipv4}/32 \
77 persistent-keepalive 5
78 ip route replace ${relay.ipv4}/32 dev ${iface} table main
80 boot.initrd.postMountCommands = lib.mkIf initrd.network.flushBeforeStage2 ''
81 ip link del dev ${iface}