2 Declares what makes the nix-daemon work on systemd.
4 - nixos/modules/config/nix.nix: the nix.conf
5 - nixos/modules/config/nix-remote-build.nix: the nix.conf
17 nixPackage = cfg.package.out;
19 isNixAtLeast = lib.versionAtLeast (lib.getVersion nixPackage);
21 makeNixBuildUser = nr: {
22 name = "nixbld${toString nr}";
24 description = "Nix build user ${toString nr}";
27 For consistency with the setgid(2), setuid(2), and setgroups(2)
28 calls in `libstore/build.cc', don't add any supplementary group
31 uid = builtins.add config.ids.uids.nixbld nr;
34 extraGroups = [ "nixbld" ];
38 nixbldUsers = lib.listToAttrs (map makeNixBuildUser (lib.range 1 cfg.nrBuildUsers));
44 (lib.mkRenamedOptionModuleWith {
52 "daemonIOSchedPriority"
55 (lib.mkRenamedOptionModuleWith {
66 (lib.mkRemovedOptionModule [ "nix" "daemonNiceLevel" ] "Consider nix.daemonCPUSchedPolicy instead.")
75 enable = lib.mkOption {
76 type = lib.types.bool;
79 Whether to enable Nix.
80 Disabling Nix makes the system hard to modify and the Nix programs and configuration will not be made available by NixOS itself.
84 package = lib.mkOption {
85 type = lib.types.package;
87 defaultText = lib.literalExpression "pkgs.nix";
89 This option specifies the Nix package instance to use throughout the system.
93 daemonCPUSchedPolicy = lib.mkOption {
94 type = lib.types.enum [
102 Nix daemon process CPU scheduling policy. This policy propagates to
103 build processes. `other` is the default scheduling
104 policy for regular tasks. The `batch` policy is
105 similar to `other`, but optimised for
106 non-interactive tasks. `idle` is for extremely
107 low-priority tasks that should only be run when no other task
110 Please note that while using the `idle` policy may
111 greatly improve responsiveness of a system performing expensive
112 builds, it may also slow down and potentially starve crucial
113 configuration updates during load.
115 `idle` may therefore be a sensible policy for
116 systems that experience only intermittent phases of high CPU load,
117 such as desktop or portable computers used interactively. Other
118 systems should use the `other` or
119 `batch` policy instead.
121 For more fine-grained resource control, please refer to
122 {manpage}`systemd.resource-control(5)` and adjust
123 {option}`systemd.services.nix-daemon` directly.
127 daemonIOSchedClass = lib.mkOption {
128 type = lib.types.enum [
132 default = "best-effort";
135 Nix daemon process I/O scheduling class. This class propagates to
136 build processes. `best-effort` is the default
137 class for regular tasks. The `idle` class is for
138 extremely low-priority tasks that should only perform I/O when no
141 Please note that while using the `idle` scheduling
142 class can improve responsiveness of a system performing expensive
143 builds, it might also slow down or starve crucial configuration
146 `idle` may therefore be a sensible class for
147 systems that experience only intermittent phases of high I/O load,
148 such as desktop or portable computers used interactively. Other
149 systems should use the `best-effort` class.
153 daemonIOSchedPriority = lib.mkOption {
154 type = lib.types.int;
158 Nix daemon process I/O scheduling priority. This priority propagates
159 to build processes. The supported priorities depend on the
160 scheduling policy: With idle, priorities are not used in scheduling
161 decisions. best-effort supports values in the range 0 (high) to 7
166 # Environment variables for running Nix.
167 envVars = lib.mkOption {
168 type = lib.types.attrs;
171 description = "Environment variables used by Nix.";
174 nrBuildUsers = lib.mkOption {
175 type = lib.types.int;
177 Number of `nixbld` user accounts created to
178 perform secure concurrent builds. If you receive an error
179 message saying that “all build users are currently in use”,
180 you should increase this value.
186 ###### implementation
188 config = lib.mkIf cfg.enable {
189 environment.systemPackages = [
192 ] ++ lib.optional (config.programs.bash.completion.enable) pkgs.nix-bash-completions;
194 systemd.packages = [ nixPackage ];
196 systemd.tmpfiles = lib.mkMerge [
197 (lib.mkIf (isNixAtLeast "2.8") {
198 packages = [ nixPackage ];
200 (lib.mkIf (!isNixAtLeast "2.8") {
202 "d /nix/var/nix/daemon-socket 0755 root root - -"
207 systemd.sockets.nix-daemon.wantedBy = [ "sockets.target" ];
209 systemd.services.nix-daemon = {
213 config.programs.ssh.package
214 ] ++ lib.optionals cfg.distributedBuilds [ pkgs.gzip ];
219 CURL_CA_BUNDLE = "/etc/ssl/certs/ca-certificates.crt";
221 // config.networking.proxy.envVars;
223 unitConfig.RequiresMountsFor = "/nix/store";
226 CPUSchedulingPolicy = cfg.daemonCPUSchedPolicy;
227 IOSchedulingClass = cfg.daemonIOSchedClass;
228 IOSchedulingPriority = cfg.daemonIOSchedPriority;
229 LimitNOFILE = 1048576;
233 restartTriggers = [ config.environment.etc."nix/nix.conf".source ];
235 # `stopIfChanged = false` changes to switch behavior
236 # from stop -> update units -> start
237 # to update units -> restart
239 # The `stopIfChanged` setting therefore controls a trade-off between a
240 # more predictable lifecycle, which runs the correct "version" of
241 # the `ExecStop` line, and on the other hand the availability of
242 # sockets during the switch, as the effectiveness of the stop operation
243 # depends on the socket being stopped as well.
245 # As `nix-daemon.service` does not make use of `ExecStop`, we prefer
246 # to keep the socket up and available. This is important for machines
247 # that run Nix-based services, such as automated build, test, and deploy
248 # services, that expect the daemon socket to be available at all times.
250 # Notably, the Nix client does not retry on failure to connect to the
251 # daemon socket, and the in-process RemoteStore instance will disable
252 # itself. This makes retries infeasible even for services that are
253 # aware of the issue. Failure to connect can affect not only new client
254 # processes, but also new RemoteStore instances in existing processes,
255 # as well as existing RemoteStore instances that have not saturated
256 # their connection pool.
258 # Also note that `stopIfChanged = true` does not kill existing
259 # connection handling daemons, as one might wish to happen before a
260 # breaking Nix upgrade (which is rare). The daemon forks that handle
261 # the individual connections split off into their own sessions, causing
262 # them not to be stopped by systemd.
263 # If a Nix upgrade does require all existing daemon processes to stop,
264 # nix-daemon must do so on its own accord, and only when the new version
265 # starts and detects that Nix's persistent state needs an upgrade.
266 stopIfChanged = false;
270 # Set up the environment variables for running Nix.
271 environment.sessionVariables = cfg.envVars;
273 nix.nrBuildUsers = lib.mkDefault (
274 if cfg.settings.auto-allocate-uids or false then
277 lib.max 32 (if cfg.settings.max-jobs == "auto" then 0 else cfg.settings.max-jobs)
280 users.users = nixbldUsers;
282 services.displayManager.hiddenUsers = lib.attrNames nixbldUsers;
284 # Legacy configuration conversion.
285 nix.settings = lib.mkMerge [
286 (lib.mkIf (isNixAtLeast "2.3pre") { sandbox-fallback = false; })