aboutsummaryrefslogtreecommitdiff
path: root/infra/modules/base/tailscale.nix
blob: 4fdee80ea9db1456384d1794ff378dc71ee6b34a (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
47
48
49
50
51
52
{
  flake.modules.nixos.base =
    { config, lib, ... }:
    {
      options.infra.tailscale = {
        authenticate = lib.mkOption {
          type = lib.types.bool;
          default = false;
          description = "Whether to enable automatic authentication with the authkey.";
        };

        ssh = lib.mkOption {
          type = lib.types.bool;
          default = false;
          description = "Whether to enable Tailscale SSH.";
        };
      };

      config =
        let
          cfg = config.infra.tailscale;

          datadir = "/var/lib/tailscale";
        in
        {
          sops.secrets."services/tailscale/auth_key" = { };

          infra.persist.directories = [ datadir ];

          systemd.services.tailscaled = {
            serviceConfig.StateDirectory = "tailscale";
          };

          services.tailscale = {
            enable = true;
            authKeyFile =
              if cfg.authenticate then config.sops.secrets."services/tailscale/auth_key".path else null;
            extraUpFlags = lib.lists.optional cfg.ssh "--ssh";
            extraDaemonFlags = [
              "--no-logs-no-support"
              "--statedir=${datadir}"
            ];
          };

          virtualisation.vmVariant = {
            # Use an auth key with ephemeral nodes so they get automatically
            # deleted.
            sops.secrets."services/tailscale/auth_key".key = lib.mkForce "services/tailscale/nixos_vm_auth_key";
          };
        };
    };
}