From b8e61fe76a3f1dcceab0bc2bd3bb71407b839453 Mon Sep 17 00:00:00 2001 From: TheGeneralist <180094941+thegeneralist01@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:34:50 +0100 Subject: [PATCH] thegeneralist: start nginx and coredns only once tailscale0 has 100.108.125.63 --- hosts/thegeneralist/configuration.nix | 1 + hosts/thegeneralist/tailscale-ip.nix | 51 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 hosts/thegeneralist/tailscale-ip.nix diff --git a/hosts/thegeneralist/configuration.nix b/hosts/thegeneralist/configuration.nix index 2693263..21fc53c 100644 --- a/hosts/thegeneralist/configuration.nix +++ b/hosts/thegeneralist/configuration.nix @@ -14,6 +14,7 @@ imports = [ ./hardware-configuration.nix ./site.nix + ./tailscale-ip.nix ./cache ./archive ./forgejo diff --git a/hosts/thegeneralist/tailscale-ip.nix b/hosts/thegeneralist/tailscale-ip.nix new file mode 100644 index 0000000..28c44d6 --- /dev/null +++ b/hosts/thegeneralist/tailscale-ip.nix @@ -0,0 +1,51 @@ +# Boot ordering for services that bind this host's Tailscale address directly. +# +# nginx (every vhost: archive, archivr, jellyfin, plex) and coredns listen on +# 100.108.125.63, not on 0.0.0.0. That address only appears on tailscale0 once +# tailscaled has logged in, which happens some seconds AFTER tailscaled.service +# reports "started". Ordering after tailscaled.service is therefore not enough: +# before this unit existed nginx failed 2-3 times per boot with "Cannot assign +# requested address" and only came up through its Restart=always retry loop. +# +# tailscale-ip-ready is a oneshot that blocks until the address is really +# present; nginx and coredns are ordered after it. `wants` (not `requires`): +# if Tailscale never connects the wait times out, the unit fails, and nginx and +# coredns still start and fall back to their own restart loops instead of +# staying down. +# +# Deliberately NOT net.ipv4.ip_nonlocal_bind = 1: that lets every service bind +# any address, so a typo in a listen address would silently stop failing. +{ pkgs, ... }: +let + # Must match the `listen.addr` entries of the nginx vhosts and the coredns `bind` lines. + tailscaleAddress = "100.108.125.63"; +in +{ + systemd.services.tailscale-ip-ready = { + description = "Wait until tailscale0 has ${tailscaleAddress}"; + after = [ "tailscaled.service" ]; + wants = [ "tailscaled.service" ]; + serviceConfig = { + Type = "oneshot"; + # Stays "active" after the check so later start jobs don't re-run the wait. + RemainAfterExit = true; + TimeoutStartSec = "2min"; + }; + script = '' + until ${pkgs.iproute2}/bin/ip -4 -o addr show dev tailscale0 2>/dev/null \ + | ${pkgs.gnugrep}/bin/grep -qF ' ${tailscaleAddress}/'; do + sleep 1 + done + ''; + }; + + systemd.services.nginx = { + after = [ "tailscale-ip-ready.service" ]; + wants = [ "tailscale-ip-ready.service" ]; + }; + + systemd.services.coredns = { + after = [ "tailscale-ip-ready.service" ]; + wants = [ "tailscale-ip-ready.service" ]; + }; +}