This repository has no description
2.8 kB
72 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
26 clanCore.facts.services.wiregrill = {
27 secret."wiregrill.key" = { };
28 public."wiregrill.pub" = { };
29 generator.path = with pkgs; [
30 coreutils
31 wireguard-tools
32 ];
33 generator.script = ''
34 wg genkey > "$secrets"/wiregrill.key
35 cat "$secrets"/wiregrill.key | wg pubkey > "$facts"/wiregrill.pub
36 '';
37 };
38
39 systemd.network.networks.wiregrill = {
40 matchConfig.Name = "wiregrill";
41 address =
42 (optional (!isNull self.ip4) "${self.ip4.addr}/16") ++
43 (optional (!isNull self.ip6) "${self.ip6.addr}/48")
44 ;
45 networkConfig = {
46 IgnoreCarrierLoss = "10s";
47 };
48 };
49
50 networking.wireguard.interfaces.wiregrill = {
51 ips =
52 (optional (!isNull self.ip4 && !config.systemd.network.enable) self.ip4.addr) ++
53 (optional (!isNull self.ip6 && !config.systemd.network.enable) self.ip6.addr);
54 listenPort = 51820;
55 privateKeyFile = "${config.krebs.secret.directory}/wiregrill.key";
56 allowedIPsAsRoutes = true;
57 peers = mapAttrsToList
58 (name: host: {
59 # inherit name;
60 allowedIPs = if isRouter then
61 (optional (!isNull host.nets.wiregrill.ip4) host.nets.wiregrill.ip4.addr) ++
62 (optional (!isNull host.nets.wiregrill.ip6) host.nets.wiregrill.ip6.addr)
63 else
64 host.nets.wiregrill.wireguard.subnets
65 ;
66 endpoint = mkIf (!isNull host.nets.wiregrill.via) (host.nets.wiregrill.via.ip4.addr + ":${toString host.nets.wiregrill.wireguard.port}");
67 persistentKeepalive = mkIf (!isNull host.nets.wiregrill.via) 61;
68 publicKey = (replaceStrings ["\n"] [""] host.nets.wiregrill.wireguard.pubkey);
69 })
70 (filterAttrs (_: h: hasAttr "wiregrill" h.nets) config.krebs.hosts);
71 };
72}