summaryrefslogtreecommitdiff
path: root/modules/nixos/traits/tailscale.nix
blob: ea38e5ca0a152433e8d51f6df7e1148405f6eca8 (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
{
  config,
  lib,
  secretsDir,
  ...
}:
let
  cfg = config.traits.tailscale;
in
{
  options.traits.tailscale = {
    enable = lib.mkEnableOption "Tailscale";
    ssh.enable = lib.mkEnableOption "Tailscale SSH";
    manageSecrets = lib.mkEnableOption "automatic management of secrets";
  };

  config = lib.mkIf cfg.enable (
    lib.mkMerge [
      {
        networking.firewall = {
          # all connections from tailscale are safe...or should be
          trustedInterfaces = [ config.services.tailscale.interfaceName ];
        };

        services.tailscale = {
          enable = true;
          openFirewall = true;
        };
      }

      (lib.mkIf cfg.ssh.enable {
        networking.firewall = {
          allowedTCPPorts = [ 22 ];
        };

        services.tailscale = {
          extraUpFlags = [ "--ssh" ];
        };
      })

      (lib.mkIf cfg.manageSecrets {
        age.secrets = lib.mkIf cfg.manageSecrets {
          tailscaleAuthKey.file = "${secretsDir}/tailscaleAuthKey.age";
        };

        services.tailscale = {
          authKeyFile = config.age.secrets.tailscaleAuthKey.path;
        };
      })
    ]
  );
}