aboutsummaryrefslogtreecommitdiff
path: root/hosts/enode/headscale.nix
blob: f457e28b8bf393024b7d1bcd26cbaa8499d594db (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
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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  inherit (lib)
    mkIf
    flip
    ;

  cfg = config.services.headscale;
in
{
  services.headscale = {
    enable = true;
    address = "127.0.0.1";
    port = 8080;
    settings = {
      server_url = "https://headscale.sorre.xyz";
      database.sqlite = {
        path = "/var/lib/headscale/db.sqlite";
        write_ahead_log = true;
      };
      trusted_proxies = [
        "127.0.0.1/32"
        "::1/128"
      ];
      dns = {
        base_domain = "sorre.internal";
        override_local_dns = true;
        nameservers.global = [
          "1.1.1.1"
          "1.0.0.1"
        ];
        magic_dns = true;
        extra_records =
          let
            towerAddr = "100.64.0.6";
            towerSubdomains = [
              "music"
              "vaultwarden"
              "git"
              "kanboard"
            ];
          in
          flip map towerSubdomains (subdomain: {
            type = "A";
            name = "${subdomain}.${cfg.settings.dns.base_domain}";
            value = towerAddr;
          });
      };
      policy = {
        mode = "file";
        path = ./headscale-policy.json;
      };
    };
  };

  systemd.services.backup-headscale = mkIf cfg.enable {
    serviceConfig = {
      Type = "exec";
      User = cfg.user;
      Group = cfg.group;
      StateDirectory = "backups/headscale";
      StateDirectoryMode = "0700";
      Umask = "077";
    };
    description = "Back up headscale data";
    conflicts = [ "headscale.service" ];
    path = [ pkgs.gnutar ];
    enableStrictShellChecks = true;
    script = ''
      #!/usr/bin/env bash
      set -euo pipefail

      TIMESTAMP="$(date +%Y%m%d%H%M%S)"
      BACKUP_PATH="$STATE_DIRECTORY/$TIMESTAMP.backup.tar"
      echo "Backing up headscale to $BACKUP_PATH" >&2

      tar cf "$BACKUP_PATH" /var/lib/headscale
      chmod u=r,go-rwx "$BACKUP_PATH"

      find "$STATE_DIRECTORY" -type f -name '*.backup.tar' -print0 |
        sort --zero-terminated --reverse |
        tail --zero-terminated --lines=+6 |
        xargs --null --no-run-if-empty rm --
    '';
  };

  # See: https://unix.stackexchange.com/a/804263
  systemd.services.backup-headscale-restart-service = mkIf cfg.enable {
    unitConfig = {
      StopPropagatedFrom = "backup-headscale.service";
      Before = "backup-headscale.service";
      User = "root";
    };
    path = [ pkgs.systemdMinimal ];
    serviceConfig = {
      Type = "oneshot";
      User = "root";
      RemainAfterExit = true;
      ExecStop = "systemctl start headscale.service";
    };
    wantedBy = [ "backup-headscale.service" ];
  };

  systemd.timers.backup-headscale = mkIf cfg.enable {
    description = "Back up headscale every morning";
    timerConfig.OnCalendar = "*-*-* 05:00:00";
    wantedBy = [ "timers.target" ];
  };
}