]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/oignon/wireguard.nix
wireguard: restart peer on failure (eg. DNS)
[julm/julm-nix.git] / hosts / oignon / wireguard.nix
1 { pkgs, lib, config, hostName, private, ... }:
2 let
3 inherit (config.services) wireguard;
4 domain = "sourcephile.fr";
5 iface = "wg-intra";
6 peer = {
7 publicKey = "tE4fzqDrr7BgfOo9tWgGnpu3v0JRDEUZbJnm9e2F/GA=";
8 allowedIPs = [ "192.168.42.3/32" ];
9 };
10 keyToUnitName = lib.replaceChars
11 [ "/" "-" " " "+" "=" ]
12 [ "-" "\\x2d" "\\x20" "\\x2b" "\\x3d" ];
13 in
14 {
15 services.openssh.listenAddresses = map (ip: {addr=lib.removeSuffix "/32" ip;}) peer.allowedIPs;
16 networking.firewall.extraCommands = ''
17 ip46tables -A nixos-fw -i ${iface} -p tcp -m tcp --dport 22 -j ACCEPT
18 '';
19 networking.wireguard.interfaces.${iface} = {
20 ips = peer.allowedIPs;
21 privateKeyFile = "${private}/${hostName}/wireguard/${iface}/privateKey";
22 peers = [
23 {
24 publicKey = "xsFFep3k8z0pXgUOz4aryOF8l/KPBSOd4WQA26BkXy0=";
25 endpoint = "losurdo.${domain}:43642";
26 allowedIPs = [ "192.168.42.2/32" ];
27 persistentKeepalive = 25;
28 }
29 ];
30 };
31 networking.hosts = builtins.listToAttrs (lib.concatMap (peer:
32 let host = lib.removeSuffix ".${domain}:43642" peer.endpoint + ".wg"; in
33 map (ip: lib.nameValuePair (lib.removeSuffix "/32" ip) [host]) peer.allowedIPs
34 ) config.networking.wireguard.interfaces.${iface}.peers);
35 systemd.services = builtins.listToAttrs (map (peer:
36 lib.nameValuePair "wireguard-${iface}-peer-${keyToUnitName peer.publicKey}"
37 {
38 serviceConfig.Restart = "on-failure";
39 serviceConfig.RestartSec = 15;
40 }
41 ) config.networking.wireguard.interfaces.${iface}.peers);
42 }