From 185948bd01dd80e856a27788b2b6cf787410279f Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Fri, 30 Aug 2024 01:15:37 +1000 Subject: [PATCH 1/2] tailscale: only autoconnect after backend is up Previously, if this service started before the backend is up, `StatusText` would be empty leading to the service trying to run `tailscale up` even if this device is already logged in. --- nixos/modules/services/networking/tailscale.nix | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/nixos/modules/services/networking/tailscale.nix b/nixos/modules/services/networking/tailscale.nix index a690dc610e82..b00af075d02f 100644 --- a/nixos/modules/services/networking/tailscale.nix +++ b/nixos/modules/services/networking/tailscale.nix @@ -121,10 +121,16 @@ in { serviceConfig = { Type = "oneshot"; }; - script = '' - status=$(${config.systemd.package}/bin/systemctl show -P StatusText tailscaled.service) - if [[ $status != Connected* ]]; then - ${cfg.package}/bin/tailscale up --auth-key 'file:${cfg.authKeyFile}' ${escapeShellArgs cfg.extraUpFlags} + # https://github.com/tailscale/tailscale/blob/v1.72.1/ipn/backend.go#L24-L32 + script = let + statusCommand = "${lib.getExe cfg.package} status --json | ${lib.getExe pkgs.jq} -r '.BackendState'"; + in '' + while [[ "$(${statusCommand})" == "NoState" ]]; do + sleep 0.5 + done + status=$(${statusCommand}) + if [[ "$status" == "NeedsLogin" || "$status" == "NeedsMachineAuth" ]]; then + ${lib.getExe cfg.package} up --auth-key 'file:${cfg.authKeyFile}' ${escapeShellArgs cfg.extraUpFlags} fi ''; }; @@ -137,7 +143,7 @@ in { Type = "oneshot"; }; script = '' - ${cfg.package}/bin/tailscale set ${escapeShellArgs cfg.extraSetFlags} + ${lib.getExe cfg.package} set ${escapeShellArgs cfg.extraSetFlags} ''; }; From d25d241e3819fbeded149f6e2cd4d89d0887cdb0 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Thu, 12 Sep 2024 23:06:23 +1000 Subject: [PATCH 2/2] Update nixos/modules/services/networking/tailscale.nix Co-authored-by: Sandro --- nixos/modules/services/networking/tailscale.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/modules/services/networking/tailscale.nix b/nixos/modules/services/networking/tailscale.nix index b00af075d02f..2acae677390c 100644 --- a/nixos/modules/services/networking/tailscale.nix +++ b/nixos/modules/services/networking/tailscale.nix @@ -123,7 +123,7 @@ in { }; # https://github.com/tailscale/tailscale/blob/v1.72.1/ipn/backend.go#L24-L32 script = let - statusCommand = "${lib.getExe cfg.package} status --json | ${lib.getExe pkgs.jq} -r '.BackendState'"; + statusCommand = "${lib.getExe cfg.package} status --json --peers=false | ${lib.getExe pkgs.jq} -r '.BackendState'"; in '' while [[ "$(${statusCommand})" == "NoState" ]]; do sleep 0.5