1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2024-12-14 11:57:34 +00:00
nix-darwin/modules/services/tailscale.nix

82 lines
2.8 KiB
Nix
Raw Normal View History

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" ];
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.
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.
'';
2023-06-26 02:14:34 +00:00
}];
2022-09-09 17:26:49 +00:00
environment.systemPackages = [ cfg.package ];
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
command = lib.getExe' cfg.package "tailscaled";
2022-09-09 17:26:49 +00:00
serviceConfig = {
Label = "com.tailscale.tailscaled";
RunAtLoad = true;
};
};
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"
];
# 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 ''
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
};
}