2023-06-18 00:46:31 +00:00
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}: let
|
|
|
|
cfg = config.mod.tailscale;
|
|
|
|
in {
|
|
|
|
options.mod.tailscale = {
|
2023-01-20 21:13:14 +00:00
|
|
|
enable = lib.mkEnableOption "tailscale";
|
|
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
2023-06-18 00:46:31 +00:00
|
|
|
environment.systemPackages = [pkgs.tailscale];
|
2023-01-20 21:13:14 +00:00
|
|
|
services.tailscale.enable = true;
|
2022-12-25 23:15:15 +00:00
|
|
|
|
2023-01-20 21:13:14 +00:00
|
|
|
systemd.services.tailscale-autoconnect = {
|
|
|
|
description = "Automatically connects to Tailscale";
|
2022-12-25 23:15:15 +00:00
|
|
|
|
2023-01-20 21:13:14 +00:00
|
|
|
# make sure tailscale is running before trying to connect to tailscale
|
2023-06-18 00:46:31 +00:00
|
|
|
after = ["network-pre.target" "tailscale.service"];
|
|
|
|
wants = ["network-pre.target" "tailscale.service"];
|
|
|
|
wantedBy = ["multi-user.target"];
|
2022-12-25 23:15:15 +00:00
|
|
|
|
2023-01-20 21:13:14 +00:00
|
|
|
# set this service as a oneshot job
|
|
|
|
serviceConfig.Type = "oneshot";
|
2022-12-25 23:15:15 +00:00
|
|
|
|
2023-01-20 21:13:14 +00:00
|
|
|
# have the job run this shell script
|
|
|
|
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
|
2022-12-25 23:15:15 +00:00
|
|
|
|
2023-01-20 21:13:14 +00:00
|
|
|
# ${pkgs.tailscale}/bin/tailscale up # blocks, doesn't give url
|
|
|
|
# This time, configure device auth so that we authenticate from portal
|
2023-06-18 00:46:31 +00:00
|
|
|
# https://tailscale.com/kb/1099/device-authorization/#enable-device-authorization-for-your-network
|
2023-01-20 21:13:14 +00:00
|
|
|
${pkgs.tailscale}/bin/tailscale up -authkey tskey-auth-kJcgTG5CNTRL-PUVFkk31z1bThHpfq3FC5b1jcMmkW2EYW
|
|
|
|
'';
|
|
|
|
};
|
2022-12-25 23:15:15 +00:00
|
|
|
|
2023-01-20 21:13:14 +00:00
|
|
|
networking.firewall = lib.mkIf config.networking.firewall.enable {
|
|
|
|
trustedInterfaces = [
|
|
|
|
"tailscale0"
|
|
|
|
];
|
|
|
|
allowedUDPPorts = [
|
|
|
|
config.services.tailscale.port
|
|
|
|
];
|
|
|
|
allowedTCPPorts = [
|
|
|
|
22
|
|
|
|
];
|
|
|
|
checkReversePath = "loose";
|
|
|
|
};
|
2022-12-25 23:15:15 +00:00
|
|
|
};
|
|
|
|
}
|