]> Git — Sourcephile - sourcephile-nix.git/blob - hosts/losurdo/networking/wireguard/intranet.nix
nftables: add comment
[sourcephile-nix.git] / hosts / losurdo / networking / wireguard / intranet.nix
1 { pkgs, lib, config, inputs, ... }:
2 let
3 inherit (config.security) gnupg;
4 inherit (config.boot) initrd;
5 iface = "wg-intra";
6 wg = config.networking.wireguard.interfaces.${iface};
7 wg-intra-peers = import (inputs.julm-nix + "/nixos/profiles/wireguard/wg-intra/peers.nix");
8 relay = wg-intra-peers.mermet;
9 in
10 {
11 imports = [
12 (inputs.julm-nix + "/nixos/profiles/wireguard/wg-intra.nix")
13 ];
14 networking.wireguard.${iface}.peers = {
15 mermet.enable = true;
16 oignon.enable = true;
17 patate.enable = true;
18 };
19 networking.wireguard.interfaces.${iface} = {
20 privateKeyFile = gnupg.secrets."wireguard/${iface}/privateKey".path;
21 };
22 security.gnupg.secrets."wireguard/${iface}/privateKey" = {
23 /*
24 systemdConfig.serviceConfig = {
25 before = [ "wireguard-${iface}.service" ];
26 wantedBy = [ "wireguard-${iface}.service" ];
27 requiredBy = [ "wireguard-${iface}.service" ];
28 };
29 */
30 };
31 systemd.services."wireguard-${iface}" = {
32 after = [ gnupg.secrets."wireguard/${iface}/privateKey".service ];
33 requires = [ gnupg.secrets."wireguard/${iface}/privateKey".service ];
34 unitConfig.Upholds = [ "upnpc-${toString wg.listenPort}.service" ];
35 };
36 networking.nftables.ruleset = ''
37 # Allow initiating connection to and from other peers
38 add rule inet filter fw2net udp sport ${toString wg.listenPort} counter accept comment "WireGuard ${iface} output to peers"
39 add rule inet filter net2fw udp dport ${toString wg.listenPort} counter accept comment "WireGuard ${iface} input from peers"
40
41 # Hook ${iface} into relevant chains
42 add rule inet filter input iifname "${iface}" jump intra2fw
43 add rule inet filter input iifname "${iface}" log level warn prefix "intra2fw: " counter drop
44 add rule inet filter output oifname "${iface}" jump fw2intra
45 add rule inet filter output oifname "${iface}" log level warn prefix "fw2intra: " counter drop
46
47 # ${iface} firewalling
48 add rule inet filter fw2intra counter accept
49 ${lib.concatMapStringsSep "\n" (ip: ''
50 add rule inet filter intra2fw ip saddr ${ip} counter accept comment "relay"
51 '') relay.ips}
52 add rule inet filter forward iifname "${iface}" jump fwd-intra
53 '';
54 # Apparently required to get NAT reflection.
55 services.upnpc.redirections = [
56 { description = "WireGuard"; externalPort = wg.listenPort; protocol = "UDP"; duration = 30 * 60;
57 service.requiredBy = [ "wireguard-${iface}.service" ];
58 service.before = [ "wireguard-${iface}.service" ];
59 }
60 ];
61 boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
62
63 # Open a wireguard tunnel to a relay
64 # in case the host is hosted behind a NAT and has no SSH port forwarding.
65 # This enables to send the disk password to the initrd, like that:
66 # ssh -J mermet.sourcephile.fr root@losurdo.wg -p 2222
67 boot.initrd.secrets."/root/initrd/${iface}.key" = "/root/initrd/${iface}.key";
68 boot.initrd.kernelModules = [ "wireguard" ];
69 boot.initrd.extraUtilsCommands = ''
70 #copy_bin_and_libs ${pkgs.wireguard-tools}/bin/wg
71 cp -fpdv ${pkgs.wireguard-tools}/bin/.wg-wrapped $out/bin/wg
72 '';
73 boot.initrd.network.postCommands = ''
74 ip link add dev ${iface} type wireguard
75 ${lib.concatMapStringsSep "\n" (ip: ''
76 ip address add ${ip} dev ${iface}
77 '') wg.ips}
78 wg set ${iface} private-key /root/initrd/${iface}.key \
79 listen-port ${toString wg.listenPort}
80 ip link set up dev ${iface} mtu 1280
81 wg set ${iface} peer ${relay.peer.publicKey} \
82 endpoint ${relay.ipv4}:${toString relay.listenPort} \
83 allowed-ips ${relay.ipv4}/32 \
84 persistent-keepalive 5
85 ip route replace ${relay.ipv4}/32 dev ${iface} table main
86 '';
87 boot.initrd.postMountCommands = lib.mkIf initrd.network.flushBeforeStage2 ''
88 ip link del dev ${iface}
89 '';
90 }