aboutsummaryrefslogtreecommitdiff
path: root/modules/wireguard/nixos/default.nix
blob: ada218a802ca5f8d3d5c7cd68a2c0396bc05f631 (plain)
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
{
  config,
  lib,
  ...
}: let
  cfg = config.collinux.wireguard;
in
  lib.mkIf cfg.enable {
    networking.useNetworkd = true;
    systemd.network = {
      enable = true;

      netdevs."10-wg" = {
        netdevConfig = {
          Kind = "wireguard";
          Name = "wg0";
        };

        wireguardConfig = {
          PrivateKeyFile = cfg.privateKeyFile;
          ListenPort = 51820;
        };

        wireguardPeers =
          cfg.peers
          |> builtins.mapAttrs (_: m: {
            PublicKey = m.publicKey;
            AllowedIPs = [m.ip];
            Endpoint = m.endpoint;
          })
          |> builtins.attrValues;
      };

      networks."12-wireguard" = {
        name = "wg0";

        networkConfig = {
          Address = cfg.ip;
          DHCP = "no";
          Gateway = cfg.gateway;

          IPv6AcceptRA = false;
        };
      };
    };
  }