{ config, lib, pkgs, ... }: { environment.systemPackages = [ pkgs.tailscale ]; services.tailscale.enable = true; systemd.services.tailscale-autoconnect = { description = "Automatic oneshot connection to tailscale"; after = [ "network-pre.target" "tailscale.service" ]; wants = [ "network-pre.target" "tailscale.service" ]; wantedBy = [ "multi-user.target" ]; serviceConfig.Type = "oneshot"; script = with pkgs; '' sleep 2 status="$(${tailscale}/bin/tailscale status -json | ${jq}/bin/jq -r .BackendState)" if [ $status = "Running" ]; then # if so, then do nothing exit 0 fi ${tailscale}/bin/tailscale up -authkey ${config.age.secrets.tailscale.path} ''; }; networking.firewall = { trustedInterfaces = [ "tailscale0" ]; allowedUDPPorts = [ config.services.tailscale.port ]; allowedTCPPorts = [ 22 ]; }; }