2022-09-09 17:26:49 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.tailscale;
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
2023-06-26 02:14:34 +00:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "tailscale" "domain" ] "Tailscale no longer requires setting the search domain manually.")
|
|
|
|
(mkRemovedOptionModule [ "services" "tailscale" "magicDNS" ] "MagicDNS no longer requires overriding the DNS servers, if this is necessary you can use `services.tailscale.overrideLocalDns`.")
|
|
|
|
];
|
2022-09-09 17:26:49 +00:00
|
|
|
|
2023-06-26 02:14:34 +00:00
|
|
|
options.services.tailscale = {
|
2024-04-14 21:02:32 +00:00
|
|
|
enable = mkEnableOption "Tailscale client daemon";
|
2022-09-09 17:26:49 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.tailscale;
|
|
|
|
defaultText = literalExpression "pkgs.tailscale";
|
2024-04-14 21:02:32 +00:00
|
|
|
description = "The package to use for tailscale";
|
2022-09-09 17:26:49 +00:00
|
|
|
};
|
|
|
|
|
2023-06-26 02:14:34 +00:00
|
|
|
overrideLocalDns = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
example = true;
|
2024-04-14 21:02:32 +00:00
|
|
|
description = ''
|
2023-06-26 02:14:34 +00:00
|
|
|
This option implements `Override local DNS` as it is not yet implemented in Tailscaled-on-macOS.
|
|
|
|
|
|
|
|
To use this option, in the Tailscale control panel:
|
|
|
|
1. at least one DNS server is added
|
|
|
|
2. `Override local DNS` is enabled
|
|
|
|
|
|
|
|
As this option sets 100.100.100.100 as your sole DNS server, if the requirements above are not met,
|
|
|
|
all non-MagicDNS queries WILL fail.
|
|
|
|
'';
|
2022-09-09 17:26:49 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2023-06-26 02:14:34 +00:00
|
|
|
assertions = [{
|
|
|
|
assertion = !cfg.overrideLocalDns || config.networking.dns == [ "100.100.100.100" ];
|
2022-09-20 08:03:50 +00:00
|
|
|
message = ''
|
2023-06-26 02:14:34 +00:00
|
|
|
DNS servers should be configured on the Tailscale control panel when `services.tailscale.overrideLocalDns` is enabled.
|
2022-09-20 08:03:50 +00:00
|
|
|
|
2023-06-26 02:14:34 +00:00
|
|
|
A race condition can occur when DNS servers are set locally, leading to MagicDNS to not work.
|
2022-09-20 08:03:50 +00:00
|
|
|
'';
|
2023-06-26 02:14:34 +00:00
|
|
|
}];
|
2022-09-20 08:03:50 +00:00
|
|
|
|
2022-09-09 17:26:49 +00:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2022-09-20 06:09:02 +00:00
|
|
|
|
|
|
|
launchd.daemons.tailscaled = {
|
2022-09-09 17:26:49 +00:00
|
|
|
# derived from
|
|
|
|
# https://github.com/tailscale/tailscale/blob/main/cmd/tailscaled/install_darwin.go#L30
|
2024-08-31 08:27:10 +00:00
|
|
|
command = lib.getExe' cfg.package "tailscaled";
|
2022-09-09 17:26:49 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Label = "com.tailscale.tailscaled";
|
|
|
|
RunAtLoad = true;
|
|
|
|
};
|
|
|
|
};
|
2022-09-20 06:09:02 +00:00
|
|
|
|
2023-06-26 02:14:34 +00:00
|
|
|
networking.dns = mkIf cfg.overrideLocalDns [ "100.100.100.100" ];
|
|
|
|
|
|
|
|
# Ensures Tailscale MagicDNS always works even without adding 100.100.100.100 to DNS servers
|
|
|
|
environment.etc."resolver/ts.net".text = "nameserver 100.100.100.100";
|
|
|
|
|
|
|
|
# This file gets created by tailscaled when `Override local DNS` is turned off
|
|
|
|
environment.etc."resolver/ts.net".knownSha256Hashes = [
|
|
|
|
"2c28f4fe3b4a958cd86b120e7eb799eee6976daa35b228c885f0630c55ef626c"
|
|
|
|
];
|
|
|
|
|
2023-07-11 08:58:29 +00:00
|
|
|
# Cleaning up the .before-nix-darwin file is necessary as any files in /etc/resolver will be used.
|
2023-06-26 02:14:34 +00:00
|
|
|
system.activationScripts.etc.text = mkAfter ''
|
2023-07-11 08:58:29 +00:00
|
|
|
if [ -e /etc/resolver/ts.net.before-nix-darwin ]; then
|
|
|
|
rm /etc/resolver/ts.net.before-nix-darwin
|
2023-06-26 02:14:34 +00:00
|
|
|
fi
|
|
|
|
'';
|
2022-09-09 17:26:49 +00:00
|
|
|
};
|
|
|
|
}
|