feat: sys-nix: integrate tailscale

nix-components
pegasust 2022-11-24 02:14:25 +00:00
parent 7d94ca3006
commit 2e66288ce0
1 changed files with 42 additions and 4 deletions

View File

@ -14,9 +14,7 @@ with lib;
] else [ ]) ++ [
"${modulesPath}/profiles/minimal.nix"
];
inherit networking;
inherit boot;
inherit services;
system.stateVersion = "22.05";
# users.users.<defaultUser>.uid = 1000;
@ -56,5 +54,45 @@ with lib;
pkgs.mosh # ssh-alt; parsec-like
pkgs.tailscale # VPC
];
# tailscale is mandatory : ^)
# inherit services;
services = services // {
tailscale.enable = true;
};
# create a oneshot job to authenticate to Tailscale
systemd.services.tailscale-autoconnect = {
description = "Automatic connection to Tailscale";
# make sure tailscale is running before trying to connect to tailscale
after = [ "network-pre.target" "tailscale.service" ];
wants = [ "network-pre.target" "tailscale.service" ];
wantedBy = [ "multi-user.target" ];
# set this service as a oneshot job
serviceConfig.Type = "oneshot";
# have the job run this shell script
script = ''
# wait for tailscaled to settle
sleep 5
# 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
${pkgs.tailscale}/bin/tailscale up -authkey tskey-examplekeyhere
'';
};
# Don't touch networking.firewall.enable, just configure everything else.
# inherit networking;
networking = networking // {
firewall = {
checkReversePath = "loose";
trustedInterfaces = [ "tailscale0" ];
allowedUDPPorts = [ config.services.tailscale.port ];
};
};
}