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 networking.wireguard.${wgIface}.peers = {
17 carotte.enable = true;
18 aubergine.enable = true;
20 systemd.services."wireguard-${wgIface}" = {
21 unitConfig.Upholds = [ "upnpc-${toString wg.listenPort}.service" ];
23 networking.nftables.ruleset = ''
26 tcp dport ssh counter accept comment "SSH"
27 udp dport 60000-61000 counter accept comment "Mosh"
30 tcp dport { ssh, 2222 } counter accept comment "SSH"
31 udp dport 60001-60010 counter accept comment "Mosh"
32 tcp dport { http, https } counter accept comment "HTTP"
33 tcp dport git counter accept comment "Git"
38 iifname ${wgIface} oifname netIface masquerade
42 # Apparently required to get NAT reflection.
43 services.upnpc.redirections = [
45 description = "WireGuard";
46 externalPort = wg.listenPort;
49 service.requiredBy = [ "wireguard-${wgIface}.service" ];
50 service.before = [ "wireguard-${wgIface}.service" ];
53 boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
55 # Open a wireguard tunnel to a relay
56 # in case the host is hosted behind a NAT and has no SSH port forwarding.
57 # This enables to send the disk password to the initrd, like that:
58 # ssh -J mermet.sourcephile.fr root@losurdo.wg -p 2222
59 # TODO: use a dedicated interface wg-initrd
60 boot.initrd.secrets."/root/initrd/${wgIface}.key" = "/run/credentials/wireguard-${wgIface}.service/privateKey";
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 ${wgIface} type wireguard
68 ${lib.concatMapStringsSep "\n" (ip: ''
69 ip address add ${ip} dev ${wgIface}
71 wg set ${wgIface} private-key /root/initrd/${wgIface}.key \
72 listen-port ${toString wg.listenPort}
73 ip link set up dev ${wgIface} mtu 1280
74 wg set ${wgIface} 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 ${wgIface} table main
80 boot.initrd.postMountCommands = lib.mkIf initrd.network.flushBeforeStage2 ''
81 ip link del dev ${wgIface}