1
0
Fork 0
dotfiles/system/nixos/tailscale.nix

33 lines
917 B
Nix
Raw Normal View History

2023-04-04 13:05:39 +00:00
{ pkgs, config, ... }:
2022-07-31 12:03:27 +00:00
{
environment.systemPackages = [ pkgs.tailscale ];
services.tailscale.enable = true;
systemd.services.tailscale-autoconnect = {
description = "Automatic connection to Tailscale";
after = [ "network-pre.target" "tailscale.service" ];
wants = [ "network-pre.target" "tailscale.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig.Type = "oneshot";
script = ''
# 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
2023-04-04 13:05:39 +00:00
authkey="$(cat ${config.age.secrets.tailscale-authkey.path})"
${pkgs.tailscale}/bin/tailscale up -authkey "$authkey"
2022-07-31 12:03:27 +00:00
'';
};
}