Files
nix-flakes/modules/tailscale.nix
2025-07-23 16:47:48 +03:00

54 lines
1.5 KiB
Nix

{ config, lib, pkgs, ...}:
{
config = {
environment.systemPackages = [
pkgs.tailscale
pkgs.jq
];
systemd.services.tailscaled = {
enable = true;
description = "Tailscale node agent";
after = [ "network-pre.target" ];
wants = [ "network-pre.target" ];
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.tailscale}/bin/tailscaled --statedir /var/lib/tailscale";
Restart = "on-failure";
RestartSec = 5;
LiminNOFILE = "infinity";
};
wantedBy = [ "multi-user.target" ];
};
systemd.services.tailscale-autoconnect = {
enable = false;
description = "Automatic connection to Tailscale";
# make sure tailscale is running before trying to connect to tailscale
after = [ "network-pre.target" "tailscaled.service" ];
wants = [ "network-pre.target" "tailscaled.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
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 -authkey tskey-examplekeyhere
'';
};
};
}