1 { config, lib, pkgs, ... }:
6 cfg = config.services.tailscale;
7 isNetworkd = config.networking.useNetworkd;
9 meta.maintainers = with maintainers; [ mbaillie mfrw ];
11 options.services.tailscale = {
12 enable = mkEnableOption "Tailscale client daemon";
17 description = "The port to listen on for tunnel traffic (0=autoselect).";
20 interfaceName = mkOption {
22 default = "tailscale0";
23 description = ''The interface name for tunnel traffic. Use "userspace-networking" (beta) to not use TUN.'';
26 permitCertUid = mkOption {
27 type = types.nullOr types.nonEmptyStr;
29 description = "Username or user ID of the user allowed to to fetch Tailscale TLS certificates for the node.";
32 disableTaildrop = mkOption {
35 description = "Whether to disable the Taildrop feature for sending files between nodes.";
38 package = lib.mkPackageOption pkgs "tailscale" {};
40 openFirewall = mkOption {
43 description = "Whether to open the firewall for the specified port.";
46 useRoutingFeatures = mkOption {
47 type = types.enum [ "none" "client" "server" "both" ];
51 Enables settings required for Tailscale's routing features like subnet routers and exit nodes.
53 To use these these features, you will still need to call `sudo tailscale up` with the relevant flags like `--advertise-exit-node` and `--exit-node`.
55 When set to `client` or `both`, reverse path filtering will be set to loose instead of strict.
56 When set to `server` or `both`, IP forwarding will be enabled.
60 authKeyFile = mkOption {
61 type = types.nullOr types.path;
63 example = "/run/secrets/tailscale_key";
65 A file containing the auth key.
66 Tailscale will be automatically started if provided.
70 authKeyParameters = mkOption {
71 type = types.submodule {
73 ephemeral = mkOption {
74 type = types.nullOr types.bool;
76 description = "Whether to register as an ephemeral node.";
78 preauthorized = mkOption {
79 type = types.nullOr types.bool;
81 description = "Whether to skip manual device approval.";
84 type = types.nullOr types.str;
86 description = "Base URL for the Tailscale API.";
92 Extra parameters to pass after the auth key.
93 See https://tailscale.com/kb/1215/oauth-clients#registering-new-nodes-using-oauth-credentials
97 extraUpFlags = mkOption {
99 Extra flags to pass to {command}`tailscale up`. Only applied if `authKeyFile` is specified.";
101 type = types.listOf types.str;
106 extraSetFlags = mkOption {
107 description = "Extra flags to pass to {command}`tailscale set`.";
108 type = types.listOf types.str;
110 example = ["--advertise-exit-node"];
113 extraDaemonFlags = mkOption {
114 description = "Extra flags to pass to {command}`tailscaled`.";
115 type = types.listOf types.str;
117 example = ["--no-logs-no-support"];
121 config = mkIf cfg.enable {
122 environment.systemPackages = [ cfg.package ]; # for the CLI
123 systemd.packages = [ cfg.package ];
124 systemd.services.tailscaled = {
125 after = lib.mkIf (config.networking.networkmanager.enable) [ "NetworkManager-wait-online.service" ];
126 wantedBy = [ "multi-user.target" ];
128 (builtins.dirOf config.security.wrapperDir) # for `su` to use taildrive with correct access rights
129 pkgs.procps # for collecting running services (opt-in feature)
130 pkgs.getent # for `getent` to look up user shells
131 pkgs.kmod # required to pass tailscale's v6nat check
132 ] ++ lib.optional config.networking.resolvconf.enable config.networking.resolvconf.package;
133 serviceConfig.Environment = [
134 "PORT=${toString cfg.port}"
135 ''"FLAGS=--tun ${lib.escapeShellArg cfg.interfaceName} ${lib.concatStringsSep " " cfg.extraDaemonFlags}"''
136 ] ++ (lib.optionals (cfg.permitCertUid != null) [
137 "TS_PERMIT_CERT_UID=${cfg.permitCertUid}"
138 ]) ++ (lib.optionals (cfg.disableTaildrop) [
139 "TS_DISABLE_TAILDROP=true"
141 # Restart tailscaled with a single `systemctl restart` at the
142 # end of activation, rather than a `stop` followed by a later
143 # `start`. Activation over Tailscale can hang for tens of
144 # seconds in the stop+start setup, if the activation script has
145 # a significant delay between the stop and start phases
146 # (e.g. script blocked on another unit with a slow shutdown).
148 # Tailscale is aware of the correctness tradeoff involved, and
149 # already makes its upstream systemd unit robust against unit
150 # version mismatches on restart for compatibility with other
152 stopIfChanged = false;
155 systemd.services.tailscaled-autoconnect = mkIf (cfg.authKeyFile != null) {
156 after = ["tailscaled.service"];
157 wants = ["tailscaled.service"];
158 wantedBy = [ "multi-user.target" ];
162 # https://github.com/tailscale/tailscale/blob/v1.72.1/ipn/backend.go#L24-L32
164 statusCommand = "${lib.getExe cfg.package} status --json --peers=false | ${lib.getExe pkgs.jq} -r '.BackendState'";
166 if (builtins.isBool v) then (lib.boolToString v)
168 params = lib.pipe cfg.authKeyParameters [
169 (lib.filterAttrs (_: v: v != null))
170 (lib.mapAttrsToList (k: v: "${k}=${paramToString v}"))
171 (builtins.concatStringsSep "&")
172 (params: if params != "" then "?${params}" else "")
175 while [[ "$(${statusCommand})" == "NoState" ]]; do
178 status=$(${statusCommand})
179 if [[ "$status" == "NeedsLogin" || "$status" == "NeedsMachineAuth" ]]; then
180 ${lib.getExe cfg.package} up --auth-key "$(cat ${cfg.authKeyFile})${params}" ${escapeShellArgs cfg.extraUpFlags}
185 systemd.services.tailscaled-set = mkIf (cfg.extraSetFlags != []) {
186 after = ["tailscaled.service"];
187 wants = ["tailscaled.service"];
188 wantedBy = [ "multi-user.target" ];
193 ${lib.getExe cfg.package} set ${escapeShellArgs cfg.extraSetFlags}
197 boot.kernel.sysctl = mkIf (cfg.useRoutingFeatures == "server" || cfg.useRoutingFeatures == "both") {
198 "net.ipv4.conf.all.forwarding" = mkOverride 97 true;
199 "net.ipv6.conf.all.forwarding" = mkOverride 97 true;
202 networking.firewall.allowedUDPPorts = mkIf cfg.openFirewall [ cfg.port ];
204 networking.firewall.checkReversePath = mkIf (cfg.useRoutingFeatures == "client" || cfg.useRoutingFeatures == "both") "loose";
206 networking.dhcpcd.denyInterfaces = [ cfg.interfaceName ];
208 systemd.network.networks."50-tailscale" = mkIf isNetworkd {
210 Name = cfg.interfaceName;
214 ActivationPolicy = "manual";