summary refs log tree commit diff
path: root/nixos/modules/services/networking/wireguard.nix
blob: 35918e42b402a996f2e4f0ecd3c161f150a053b4 (plain) (blame)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.networking.wireguard;

  kernel = config.boot.kernelPackages;

  # interface options

  interfaceOpts = { name, ... }: {

    options = {

      ips = mkOption {
        example = [ "192.168.2.1/24" ];
        default = [];
        type = with types; listOf str;
        description = "The IP addresses of the interface.";
      };

      privateKey = mkOption {
        example = "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=";
        type = types.str;
        description = "Base64 private key generated by wg genkey.";
      };

      presharedKey = mkOption {
        default = null;
        example = "rVXs/Ni9tu3oDBLS4hOyAUAa1qTWVA3loR8eL20os3I=";
        type = with types; nullOr str;
        description = ''base64 preshared key generated by wg genpsk. Optional,
        and may be omitted. This option adds an additional layer of
        symmetric-key cryptography to be mixed into the already existing
        public-key  cryptography, for post-quantum resistance.'';
      };

      listenPort = mkOption {
        default = null;
        type = with types; nullOr int;
        example = 51820;
        description = ''16-bit port for listening. Optional; if not specified,
        automatically generated based on interface name.'';
      };

      preSetup = mkOption {
        example = literalExample [''
          ${pkgs.iproute}/bin/ip netns add foo
        ''];
        default = [];
        type = with types; listOf str;
        description = ''A list of commands called at the start of the interface
        setup.'';
      };

      postSetup = mkOption {
        example = literalExample [''
          ${pkgs.bash} -c 'printf "nameserver 10.200.100.1" | ${pkgs.openresolv}/bin/resolvconf -a wg0 -m 0'
        ''];
        default = [];
        type = with types; listOf str;
        description = "A list of commands called at the end of the interface setup.";
      };

      postShutdown = mkOption {
        example = literalExample ["${pkgs.openresolv}/bin/resolvconf -d wg0"];
        default = [];
        type = with types; listOf str;
        description = "A list of commands called after shutting down the interface.";
      };

      peers = mkOption {
        default = [];
        description = "Peers linked to the interface.";
        type = with types; listOf (submodule peerOpts);
      };

    };

  };

  # peer options

  peerOpts = {

    options = {

      publicKey = mkOption {
        example = "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=";
        type = types.str;
        description = "The base64 public key the peer.";
      };

      allowedIPs = mkOption {
        example = [ "10.192.122.3/32" "10.192.124.1/24" ];
        type = with types; listOf str;
        description = ''List of IP (v4 or v6) addresses with CIDR masks from
        which this peer is allowed to send incoming traffic and to which
        outgoing traffic for this peer is directed. The catch-all 0.0.0.0/0 may
        be specified for matching all IPv4 addresses, and ::/0 may be specified
        for matching all IPv6 addresses.'';
      };

      endpoint = mkOption {
        default = null;
        example = "demo.wireguard.io:12913";
        type = with types; nullOr str;
        description = ''Endpoint IP or hostname of the peer, followed by a colon,
        and then a port number of the peer.'';
      };

      persistentKeepalive = mkOption {
        default = null;
        type = with types; nullOr int;
        example = 25;
        description = ''This is optional and is by default off, because most
        users will not need it. It represents, in seconds, between 1 and 65535
        inclusive, how often to send an authenticated empty packet to the peer,
        for the purpose of keeping a stateful firewall or NAT mapping valid
        persistently. For example, if the interface very rarely sends traffic,
        but it might at anytime receive traffic from a peer, and it is behind
        NAT, the interface might benefit from having a persistent keepalive
        interval of 25 seconds; however, most users will not need this.'';
      };

    };

  };

  generateConf = name: values: pkgs.writeText "wireguard-${name}.conf" ''
    [Interface]
    PrivateKey = ${values.privateKey}
    ${optionalString (values.presharedKey != null) "PresharedKey = ${values.presharedKey}"}
    ${optionalString (values.listenPort != null)   "ListenPort = ${toString values.listenPort}"}

    ${concatStringsSep "\n\n" (map (peer: ''
    [Peer]
    PublicKey = ${peer.publicKey}
    ${optionalString (peer.allowedIPs != []) "AllowedIPs = ${concatStringsSep ", " peer.allowedIPs}"}
    ${optionalString (peer.endpoint != null) "Endpoint = ${peer.endpoint}"}
    ${optionalString (peer.persistentKeepalive != null) "PersistentKeepalive = ${toString peer.persistentKeepalive}"}
    '') values.peers)}
  '';

  ipCommand = "${pkgs.iproute}/bin/ip";
  wgCommand = "${pkgs.wireguard}/bin/wg";

  generateUnit = name: values:
    nameValuePair "wireguard-${name}"
      {
        description = "WireGuard Tunnel - ${name}";
        wantedBy = [ "ip-up.target" ];
        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
          ExecStart = lib.flatten([
            values.preSetup

            "-${ipCommand} link del dev ${name}"
            "${ipCommand} link add dev ${name} type wireguard"
            "${wgCommand} setconf ${name} ${generateConf name values}"

            (map (ip:
            ''${ipCommand} address add ${ip} dev ${name}''
            ) values.ips)

            "${ipCommand} link set up dev ${name}"

            (flatten (map (peer: (map (ip:
            "${ipCommand} route add ${ip} dev ${name}"
            ) peer.allowedIPs)) values.peers))

            values.postSetup
          ]);

          ExecStop = [ ''${ipCommand} link del dev "${name}"'' ] ++ values.postShutdown;
        };
      };

in

{

  ###### interface

  options = {

    networking.wireguard = {

      interfaces = mkOption {
        description = "Wireguard interfaces.";
        default = {};
        example = {
          wg0 = {
            ips = [ "192.168.20.4/24" ];
            privateKey = "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=";
            peers = [
              { allowedIPs = [ "192.168.20.1/32" ];
                publicKey  = "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=";
                endpoint   = "demo.wireguard.io:12913"; }
            ];
          };
        };
        type = with types; attrsOf (submodule interfaceOpts);
      };

    };

  };


  ###### implementation

  config = mkIf (cfg.interfaces != {}) {

    boot.extraModulePackages = [ kernel.wireguard ];
    environment.systemPackages = [ pkgs.wireguard ];

    systemd.services = mapAttrs' generateUnit cfg.interfaces;

  };

}