-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nixos): Auto-re-auth tailscale on server reboot
- Loading branch information
1 parent
23aca5a
commit 541e6f1
Showing
2 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
''; | ||
}; | ||
} |