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