Skip to content

Commit

Permalink
feat(nixos): Auto-re-auth tailscale on server reboot
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjones2014 committed Jul 4, 2024
1 parent 23aca5a commit 541e6f1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
3 changes: 1 addition & 2 deletions hosts/server/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
./content.nix
./nas.nix
./containers.nix
./tailscale.nix
];

powerManagement.cpuFreqGovernor = "performance";
Expand All @@ -36,8 +37,6 @@
};
};
services = {
tailscale.enable = true;
tailscale.authKeyFile = config.age.secrets.tailscale.path;
fail2ban.enable = true;
openssh = {
enable = true;
Expand Down
35 changes: 35 additions & 0 deletions hosts/server/tailscale.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{ config, pkgs, ... }: {
services = {
tailscale = {
enable = true;
authKeyFile = config.age.secrets.tailscale.path;
};
};

systemd.services.tailscale-autoconnect = {
description = "Automatic connection to Tailscale";

# make sure tailscale is running before trying to connect to tailscale
after = [ "network-pre.target" "tailscale.service" ];
wants = [ "network-pre.target" "tailscale.service" ];
wantedBy = [ "multi-user.target" ];

# set this service as a oneshot job
serviceConfig.Type = "oneshot";

# have the job run this shell script
script = ''
# wait for tailscaled to settle
sleep 2
# check if we are already authenticated to tailscale
status="$(${pkgs.tailscale}/bin/tailscale status -json | ${pkgs.jq}/bin/jq -r .BackendState)"
if [ $status = "Running" ]; then # if so, then do nothing
exit 0
fi
# otherwise authenticate with tailscale
${pkgs.tailscale}/bin/tailscale up --auth-key "file:${config.age.secrets.tailscale.path}"
'';
};
}

0 comments on commit 541e6f1

Please sign in to comment.