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