Skip to content

Commit

Permalink
feat: simplify tailscale auth
Browse files Browse the repository at this point in the history
  • Loading branch information
konradmalik committed Jun 5, 2024
1 parent bffec6c commit 6f73a53
Showing 1 changed file with 3 additions and 38 deletions.
41 changes: 3 additions & 38 deletions hosts/common/global/tailscale.nix
Original file line number Diff line number Diff line change
@@ -1,47 +1,12 @@
{ config, pkgs, ... }:
{ config, ... }:
{
services.tailscale = {
enable = true;
useRoutingFeatures = "both";
openFirewall = true;
authKeyFile = config.sops.secrets.tailscale-auth-key.path;
};

# setup a key secret
sops.secrets.tailscale-auth-key = { };

# create a oneshot job to authenticate to Tailscale
systemd.services.tailscale-autoauth = {
description = "Automatic authentication 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 = with pkgs; ''
# wait for tailscaled to settle
echo "waiting 5 sec..."
sleep 5
# check if we are already authenticated to tailscale
status="$(${tailscale}/bin/tailscale status -json | ${jq}/bin/jq -r .BackendState)"
if [ $status = "Running" ]; then
# if so, then do nothing
echo "already authenticated"
exit 0
fi
# otherwise authenticate with tailscale
${tailscale}/bin/tailscale up -authkey $(cat ${config.sops.secrets.tailscale-auth-key.path})
'';
};
}

0 comments on commit 6f73a53

Please sign in to comment.