dotfiles/nixos/tailscale/default.nix

43 lines
1.2 KiB
Nix

{ config, pkgs, inputs, ... }:
let
inherit (inputs.nixpkgs-nixos-unstable.legacyPackages."x86_64-linux")
tailscale;
in {
environment.systemPackages = [ tailscale ];
services.tailscale = {
enable = true;
package = tailscale;
};
networking.firewall = {
trustedInterfaces = [ "tailscale0" ];
allowedUDPPorts = [ config.services.tailscale.port ];
};
sops.secrets."tailscale-key-${config.networking.hostName}" = {
sopsFile = ./keys.sops.yaml;
restartUnits = [ "tailscale-auth.service" ];
};
systemd.services.tailscale-auth = {
description = "Auth with tailscale";
after = [ "network-pre.target" "tailscale.service" ];
wants = [ "network-pre.target" "tailscale.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig.Type = "oneshot";
script = ''
sleep 2
status="$(${tailscale}/bin/tailscale status -json | ${pkgs.jq}/bin/jq -r .BackendState)"
if [ $status = "Running" ]; then # if so, then do nothing
echo "already connected"
exit 0
fi
echo "$status, reauthing"
${tailscale}/bin/tailscale up --authkey `cat /run/secrets/tailscale-key-${config.networking.hostName}`
'';
};
}