~aasg/nixexprs

ref: 880638840c289a9d561d636725e68b924bc4726c nixexprs/modules/services/networking/wireguard-systemd.nix -rw-r--r-- 5.9 KiB
88063884 — Aluísio Augusto Silva Gonçalves bird-lg: Fix import in config loader script 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
{ config, lib, pkgs, ... }:
with import ../../../lib/extension.nix { inherit lib; };
let
  cfg = config.networking.wireguard;
  kernel = config.boot.kernelPackages;

  generatePeerConfig = peer: {
    wireguardPeerConfig = pipe peer [
      (getAttrs [ "publicKey" "presharedKeyFile" "allowedIPs" "endpoint" "persistentKeepalive" ])
      (filterAttrs (name: value: value != null))
      capitalizeAttrNames
    ];
  };

  generateNetdev = interfaceName: interfaceCfg:
    nameValuePair "40-${interfaceName}" {
      netdevConfig = {
        Name = interfaceName;
        Kind = "wireguard";
      };
      wireguardConfig = capitalizeAttrNames (getAttrs [ "privateKeyFile" "listenPort" ] interfaceCfg);
      wireguardPeers = map generatePeerConfig interfaceCfg.peers;
    };

  generateRoute = table: ip: {
    routeConfig = {
      Destination = ip;
      Scope = "link";
      Table = [ table ];
    };
  };

  generateNetwork = interfaceName: interfaceCfg:
    nameValuePair "40-${interfaceName}" {
      name = interfaceName;
      address = interfaceCfg.ips;
      routes =
        let
          peerIPs = builtins.concatMap (peer: peer.allowedIPs) interfaceCfg.peers;
          peerRoutes = map (generateRoute interfaceCfg.table) peerIPs;
        in
        optionals interfaceCfg.allowedIPsAsRoutes peerRoutes;
    };

  generatePreSetup = interfaceName: interfaceCfg:
    nameValuePair "wireguard-${interfaceName}-prestart" {
      wantedBy = [ "sys-subsystem-net-devices-${interfaceName}.device" ];
      before = [ "sys-subsystem-net-devices-${interfaceName}.device" ];
      script = interfaceCfg.preSetup;
      serviceConfig.Type = "oneshot";
    };

  generatePostSetup = interfaceName: interfaceCfg:
    nameValuePair "wireguard-${interfaceName}-poststart" {
      wantedBy = [ "sys-subsystem-net-devices-${interfaceName}.device" ];
      after = [ "sys-subsystem-net-devices-${interfaceName}.device" ];
      script = interfaceCfg.postSetup;
      serviceConfig.Type = "oneshot";
    };

  generatePostShutdown = interfaceName: interfaceCfg:
    nameValuePair "wireguard-${interfaceName}-poststop" {
      wantedBy = [ "sys-subsystem-net-devices-${interfaceName}.device" ];
      partOf = [ "sys-subsystem-net-devices-${interfaceName}.device" ];
      before = [ "sys-subsystem-net-devices-${interfaceName}.device" ];
      preStop = interfaceCfg.postShutdown;
      serviceConfig = {
        Type = "oneshot";
        RemainAfterExit = true;
      };
    };

  generateKeyServiceUnit = interfaceName: interfaceCfg:
    assert interfaceCfg.generatePrivateKeyFile;
    nameValuePair "wireguard-${interfaceName}-key" {
      description = "WireGuard Tunnel - ${interfaceName} - Key Generator";
      group = "systemd-network";
      wantedBy = [ "systemd-networkd.service" ];
      requiredBy = [ "systemd-networkd.service" ];
      before = [ "systemd-networkd.service" ];
      serviceConfig = {
        Type = "oneshot";
        RemainAfterExit = true;
      };
      script = ''
        mkdir -m 0750 -p "${dirOf interfaceCfg.privateKeyFile}"
        if [[ ! -f "${interfaceCfg.privateKeyFile}" ]]; then
          touch "${interfaceCfg.privateKeyFile}"
          chmod 0640 "${interfaceCfg.privateKeyFile}"
          ${pkgs.wireguard}/bin/wg genkey > "${interfaceCfg.privateKeyFile}"
          chmod 0440 "${interfaceCfg.privateKeyFile}"
        fi
      '';
    };

  generateServices = interfaceName: interfaceCfg: [
    (optional interfaceCfg.generatePrivateKeyFile (generateKeyServiceUnit interfaceName interfaceCfg))
    (optional (interfaceCfg.preSetup != "") (generatePreSetup interfaceName interfaceCfg))
    (optional (interfaceCfg.postSetup != "") (generatePostSetup interfaceName interfaceCfg))
    (optional (interfaceCfg.postShutdown != "") (generatePostShutdown interfaceName interfaceCfg))
  ];
in
{
  options = {
    networking.wireguard.enableNetworkd = mkEnableOption "WireGuard via systemd-networkd";
  };

  config = mkIf cfg.enableNetworkd {
    assertions =
      let
        allPeers = flatten
          (mapAttrsToList
            (interfaceName: interfaceCfg:
              map (peer: { inherit interfaceName interfaceCfg peer; }) interfaceCfg.peers)
            cfg.interfaces);
      in
      (attrValues (
        mapAttrs
          (name: value: {
            assertion = value.privateKey == null;
            message = "networking.wireguard.interfaces.${name} has privateKey set, which is not supported by NixOS with systemd-network. Use privateKeyFile instead.";
          })
          cfg.interfaces))
      ++ (attrValues (
        mapAttrs
          (name: value: {
            assertion = value.privateKeyFile != null;
            message = "networking.wireguard.interfaces.${name}.privateKeyFile must be set.";
          })
          cfg.interfaces))
      ++ (map
        ({ interfaceName, peer, ... }: {
          assertion = peer.presharedKeyFile == null;
          message = "networking.wireguard.interfaces.${interfaceName} peer «${peer.publicKey}» has presharedKey set, which is not supported by NixOS with systemd-networkd. Use presharedKeyFile instead.";
        })
        allPeers)
      ++ (attrValues (
        mapAttrs
          (name: value: {
            assertion = (value.interfaceNamespace == null) && (value.socketNamespace == null);
            message = "networking.wireguard.interfaces.${name}.interfaceNamespace or networking.wireguard.interfaces.${name}.socketNamespace is set, but network namespaces are not yet supported by systemd (see https://github.com/systemd/systemd/issues/11103).";
          })
          cfg.interfaces));

    networking.wireguard.enable = mkForce false;
    boot.extraModulePackages = optional (versionOlder kernel.kernel.version "5.6") kernel.wireguard;
    environment.systemPackages = [ pkgs.wireguard-tools ];

    systemd.network.netdevs = mapAttrs' generateNetdev cfg.interfaces;
    systemd.network.networks = mapAttrs' generateNetwork cfg.interfaces;
    systemd.services = listToAttrs (flatten (mapAttrsToList generateServices cfg.interfaces));
  };
}