2023-03-04 08:21:35 +00:00
|
|
|
{ config, pkgs, inputs, ... }:
|
|
|
|
let
|
|
|
|
inherit (inputs.nixpkgs-nixos-unstable.legacyPackages."x86_64-linux")
|
|
|
|
tailscale;
|
2022-08-19 18:51:44 +01:00
|
|
|
in {
|
2023-03-04 08:21:35 +00:00
|
|
|
environment.systemPackages = [ tailscale ];
|
2022-08-19 18:51:44 +01:00
|
|
|
services.tailscale = {
|
|
|
|
enable = true;
|
|
|
|
package = tailscale;
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.firewall = {
|
2023-03-04 08:21:35 +00:00
|
|
|
trustedInterfaces = [ "tailscale0" ];
|
|
|
|
allowedUDPPorts = [ config.services.tailscale.port ];
|
2022-08-19 18:51:44 +01:00
|
|
|
};
|
|
|
|
sops.secrets."tailscale-key-${config.networking.hostName}" = {
|
|
|
|
sopsFile = ./keys.sops.yaml;
|
|
|
|
};
|
|
|
|
systemd.services.tailscale-auth = {
|
|
|
|
description = "Auth with tailscale";
|
|
|
|
|
2023-03-04 08:21:35 +00:00
|
|
|
after = [ "network-pre.target" "tailscale.service" ];
|
|
|
|
wants = [ "network-pre.target" "tailscale.service" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-08-19 18:51:44 +01:00
|
|
|
|
|
|
|
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
|
2023-03-05 11:12:50 +00:00
|
|
|
echo "already connected"
|
2022-08-19 18:51:44 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2023-03-05 11:12:50 +00:00
|
|
|
echo "$status, reauthing"
|
2023-03-04 08:21:35 +00:00
|
|
|
${tailscale}/bin/tailscale up --force-reauth --authkey `cat /run/secrets/tailscale-key-${config.networking.hostName}`
|
2022-08-19 18:51:44 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|