diff options
| author | seth <[email protected]> | 2023-10-30 04:22:32 -0400 |
|---|---|---|
| committer | seth <[email protected]> | 2023-10-30 09:46:15 +0000 |
| commit | 10b0df38b4286237b56ff9177f8d4c5676bfb5c1 (patch) | |
| tree | ab298c74339bf9bc41571fa88746ecd9c522fbdf /modules/nixos/features/tailscale.nix | |
| parent | 4c2c60a4f2b14c1e6ffaffe5e301dc31ac4fed0f (diff) | |
tree-wide: refactor
i went overboard on modules. this is much comfier
Diffstat (limited to 'modules/nixos/features/tailscale.nix')
| -rw-r--r-- | modules/nixos/features/tailscale.nix | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/modules/nixos/features/tailscale.nix b/modules/nixos/features/tailscale.nix new file mode 100644 index 0000000..cbbe2e5 --- /dev/null +++ b/modules/nixos/features/tailscale.nix @@ -0,0 +1,63 @@ +{ + config, + lib, + pkgs, + ... +}: let + cfg = config.features.tailscale; + inherit (lib) mkDefault mkEnableOption mkIf optionalAttrs; + + baseDir = ../../../secrets/systems/${config.networking.hostName}; +in { + options.features.tailscale = { + enable = mkEnableOption "enable support for tailscale"; + ssh.enable = mkEnableOption "enable support for tailscale ssh"; + }; + + config = mkIf cfg.enable { + age.secrets = mkIf cfg.ssh.enable { + tailscaleAuthKey.file = "${baseDir}/tailscaleAuthKey.age"; + }; + + networking.firewall = + { + allowedUDPPorts = [config.services.tailscale.port]; + trustedInterfaces = ["tailscale0"]; + } + // optionalAttrs cfg.ssh.enable { + allowedTCPPorts = [22]; + }; + + services = { + tailscale.enable = mkDefault true; + }; + + # https://tailscale.com/kb/1096/nixos-minecraft/ + systemd.services = mkIf cfg.ssh.enable { + tailscale-autoconnect = { + description = "Automatic connection to Tailscale"; + + after = ["network-pre.target" "tailscale.service"]; + wants = ["network-pre.target" "tailscale.service"]; + wantedBy = ["multi-user.target"]; + + serviceConfig.Type = "oneshot"; + + script = '' + # wait for tailscaled to settle + sleep 2 + + # check if we are already authenticated to tailscale + status="$(${lib.getExe pkgs.tailscale} status -json | ${lib.getExe pkgs.jq}/bin/jq -r .BackendState)" + if [ $status = "Running" ]; then # if so, then do nothing + exit 0 + fi + + # otherwise authenticate with tailscale + ${lib.getExe pkgs.tailscale}/bin/tailscale up --ssh \ + --auth-key "file:${config.age.secrets.tailscaleAuthKey.path}" + ''; + }; + }; + }; +} |
