This repository has no description
2.4 kB
58 lines
1{ config, lib, pkgs, ... }: with lib; let
2
3 self = config.krebs.build.host.nets.wiregrill;
4 isRouter = !isNull self.via;
5
6in mkIf (hasAttr "wiregrill" config.krebs.build.host.nets) {
7 #hack for modprobe inside containers
8 systemd.services."wireguard-wiregrill".path = mkIf config.boot.isContainer (mkBefore [
9 (pkgs.writeDashBin "modprobe" ":")
10 ]);
11
12 boot.kernel.sysctl = mkIf isRouter {
13 "net.ipv6.conf.all.forwarding" = 1;
14 };
15 krebs.iptables.tables.filter.INPUT.rules = [
16 { predicate = "-p udp --dport ${toString self.wireguard.port}"; target = "ACCEPT"; }
17 ];
18 krebs.iptables.tables.filter.FORWARD.rules = mkIf isRouter (mkBefore [
19 { predicate = "-i wiregrill -o wiregrill"; target = "ACCEPT"; }
20 { predicate = "-i wiregrill -o retiolum"; target = "ACCEPT"; }
21 { predicate = "-i retiolum -o wiregrill"; target = "ACCEPT"; }
22 { predicate = "-i wiregrill -o eth0"; target = "ACCEPT"; }
23 { predicate = "-o wiregrill -m conntrack --ctstate RELATED,ESTABLISHED"; target = "ACCEPT"; }
24 ]);
25 systemd.network.networks.wiregrill = {
26 matchConfig.Name = "wiregrill";
27 address =
28 (optional (!isNull self.ip4) "${self.ip4.addr}/16") ++
29 (optional (!isNull self.ip6) "${self.ip6.addr}/48")
30 ;
31 networkConfig = {
32 IgnoreCarrierLoss = "10s";
33 };
34 };
35
36 networking.wireguard.interfaces.wiregrill = {
37 ips =
38 (optional (!isNull self.ip4 && !config.systemd.network.enable) self.ip4.addr) ++
39 (optional (!isNull self.ip6 && !config.systemd.network.enable) self.ip6.addr);
40 listenPort = 51820;
41 privateKeyFile = "${config.krebs.secret.directory}/wiregrill.key";
42 allowedIPsAsRoutes = true;
43 peers = mapAttrsToList
44 (name: host: {
45 # inherit name;
46 allowedIPs = if isRouter then
47 (optional (!isNull host.nets.wiregrill.ip4) host.nets.wiregrill.ip4.addr) ++
48 (optional (!isNull host.nets.wiregrill.ip6) host.nets.wiregrill.ip6.addr)
49 else
50 host.nets.wiregrill.wireguard.subnets
51 ;
52 endpoint = mkIf (!isNull host.nets.wiregrill.via) (host.nets.wiregrill.via.ip4.addr + ":${toString host.nets.wiregrill.wireguard.port}");
53 persistentKeepalive = mkIf (!isNull host.nets.wiregrill.via) 61;
54 publicKey = (replaceStrings ["\n"] [""] host.nets.wiregrill.wireguard.pubkey);
55 })
56 (filterAttrs (_: h: hasAttr "wiregrill" h.nets) config.krebs.hosts);
57 };
58}