10#include "nix/store/config.hh"
19typedef enum { smEnabled, smRelaxed, smDisabled } SandboxMode;
24std::string BaseSetting<SandboxMode>::to_string()
const;
29std::string BaseSetting<PathsInChroot>::to_string()
const;
34 static constexpr bool appendable =
true;
43 void anchor()
override;
50 "Amount of reserved disk space for the garbage collector.",
58 If `true`, the garbage collector keeps the outputs of
59 non-garbage derivations. If `false` (default), outputs are
60 deleted unless they are GC roots themselves (or reachable from other
63 In general, outputs must be registered as roots separately. However,
64 even if the output of a derivation is registered as a root, the
65 collector still deletes store paths that are used only at build
66 time (e.g., the C compiler, or source tarballs downloaded from the
67 network). To prevent it from doing so, set this option to `true`.
69 This option only applies to garbage collection of the whole store
70 and does not affect deleting explicit paths.
80 If `true` (default), the garbage collector keeps the derivations
81 from which non-garbage store paths were built. If `false`, they are
82 deleted unless explicitly registered as a root (or reachable from
85 Keeping derivation around is useful for querying and traceability
86 (e.g., it allows you to ask with what dependencies or options a
87 store path was built), so by default this option is on. Turn it off
88 to save a bit of disk space (or a lot if `keep-outputs` is also
91 This option only applies to garbage collection of the whole store
92 and does not affect deleting explicit paths.
94 {"gc-keep-derivations"},
102 When free disk space in `/nix/store` drops below `min-free` during a
103 build, Nix performs a garbage-collection until `max-free` bytes are
104 available or there is no more garbage. A value of `0` (the default)
105 disables this feature.
114 std::numeric_limits<int64_t>::max(),
117 When a garbage collection is triggered by the `min-free` option, it
118 stops as soon as `max-free` bytes are available. The default is
119 infinity (i.e. delete all garbage).
126 "min-free-check-interval",
127 "Number of seconds between checking free disk space.",
131const uint32_t maxIdsPerBuild =
142 void anchor()
override;
153 "The first UID and GID to use for dynamic ID allocation."};
158 maxIdsPerBuild * 128,
163 "The number of UIDs/GIDs to use for dynamic ID allocation."};
181 void anchor()
override;
203 return autoAllocateUids ? this :
nullptr;
211 Sets the value of the `NIX_BUILD_CORES` environment variable in the [invocation of the `builder` executable](@docroot@/store/building.md#env-vars) of a derivation.
212 The `builder` executable can use this variable to control its own maximum amount of parallelism.
215 FIXME(@fricklerhandwerk): I don't think this should even be mentioned here.
216 A very generic example using `derivation` and `xargs` may be more appropriate to explain the mechanism.
217 Using `mkDerivation` as an example requires being aware of that there are multiple independent layers that are completely opaque here.
219 For instance, in Nixpkgs, if the attribute `enableParallelBuilding` for the `mkDerivation` build helper is set to `true`, it passes the `-j${NIX_BUILD_CORES}` flag to GNU Make.
221 If set to `0`, nix will detect the number of CPU cores and pass this number via `NIX_BUILD_CORES`.
225 > The number of parallel local Nix build jobs is independently controlled with the [`max-jobs`](#conf-max-jobs) setting.
229 Setting<bool> fsyncMetadata{
234 If set to `true`, changes to the Nix store metadata (in
235 `/nix/var/nix/db`) are synchronously flushed to disk. This improves
236 robustness in case of system crashes, but reduces performance. The
240 Setting<bool> fsyncStorePaths{
245 Whether to call `fsync()` on store paths before registering them, to
246 flush them to disk. This improves robustness in case of system crashes,
247 but reduces performance. The default is `false`.
252 Setting<bool> syncBeforeRegistering{
253 this,
false,
"sync-before-registering",
"Whether to call `sync()` before registering a path as valid."};
256 Setting<bool> autoOptimiseStore{
259 "auto-optimise-store",
261 If set to `true`, Nix automatically detects files in the store
262 that have identical contents, and replaces them with hard links to
263 a single copy. This saves disk space. If set to `false` (the
264 default), you can still run `nix-store --optimise` to get rid of
268 Setting<size_t> narBufferSize{
269 this, 32 * 1024 * 1024,
"nar-buffer-size",
"Maximum size of NARs before spilling them to disk."};
271 Setting<bool> allowSymlinkedStore{
274 "allow-symlinked-store",
276 If set to `true`, Nix stops complaining if the store directory
277 (typically `/nix/store`) contains symlink components.
279 This risks making some builds "impure" because builders sometimes
280 "canonicalise" paths by resolving all symlink components. Problems
281 occur if those builds are then deployed to machines where /nix/store
282 resolves to a different location from that of the build machine. You
283 can enable this setting if you are sure you're not going to do that.
286 Setting<std::string> buildUsersGroup{
291 This options specifies the Unix group containing the Nix build user
292 accounts. In multi-user Nix installations, builds should not be
293 performed by the Nix account since that would allow users to
294 arbitrarily modify the Nix store and database by supplying specially
295 crafted builders; and they cannot be performed by the calling user
296 since that would allow him/her to influence the build result.
298 Therefore, if this option is non-empty and specifies a valid group,
299 builds are performed under the user accounts that are a member
300 of the group specified here (as listed in `/etc/group`). Those user
301 accounts should not be used for any other purpose\!
303 Nix never runs two builds under the same user account at the
304 same time. This is to prevent an obvious security hole: a malicious
305 user writing a Nix expression that modifies the build result of a
306 legitimate Nix expression being built by another user. Therefore it
307 is good to have as many Nix build user accounts as you can spare.
308 (Remember: uids are cheap.)
310 The build users should have permission to create files in the Nix
311 store, but not delete them. Therefore, `/nix/store` should be owned
312 by the Nix account, its group should be the group specified here,
313 and its mode should be `1775`.
315 If the build users group is empty, builds are performed under
316 the uid of the Nix process (that is, the uid of the caller if
317 `NIX_REMOTE` is empty, the uid under which the Nix daemon runs if
318 `NIX_REMOTE` is `daemon`). Obviously, this should not be used
319 with a nix daemon accessible to untrusted clients.
321 Defaults to `nixbld` when running as root, *empty* otherwise.
326 Setting<bool> autoAllocateUids{
329 "auto-allocate-uids",
331 Whether to select UIDs for builds automatically, instead of using the
332 users in `build-users-group`.
334 UIDs are allocated starting at 872415232 (0x34000000) on Linux and 56930 on macOS.
338 Xp::AutoAllocateUids};
341 Setting<bool> useCgroups{
346 Whether to execute builds inside cgroups.
347 This is only supported on Linux.
349 Cgroups are required and enabled automatically for derivations
350 that require the `uid-range` system feature.
354 Setting<SandboxMode> sandboxMode{
356#if defined(__linux__) || defined(__FreeBSD__)
364 If set to `true`, builds are performed in a *sandboxed
365 environment*, i.e., they're isolated from the normal file system
366 hierarchy and only see their dependencies in the Nix store,
367 the temporary build directory, private versions of `/proc`,
368 `/dev`, `/dev/shm` and `/dev/pts` (on Linux), and the paths
369 configured with the `sandbox-paths` option. This is useful to
370 prevent undeclared dependencies on files in directories such as
371 `/usr/bin`. In addition, on Linux, builds run in private PID,
372 mount, network, IPC and UTS namespaces to isolate them from other
373 processes in the system (except that fixed-output derivations do
374 not run in private network namespace to ensure they can access the
377 Currently, sandboxing only work on Linux and macOS. The use of a
378 sandbox requires that Nix is run as root (so you should use the
379 "build users" feature to perform the actual builds under different
382 If this option is set to `relaxed`, then fixed-output derivations
383 and derivations that have the `__noChroot` attribute set to `true`
384 do not run in sandboxes.
386 The default is `true` on Linux and `false` on all other platforms.
388 {"build-use-chroot",
"build-use-sandbox"}};
390 Setting<PathsInChroot> sandboxPaths{
395 A list of paths bind-mounted into Nix sandbox environments. You can
396 use the syntax `target=source` to mount a path in a different
397 location in the sandbox; for instance, `/bin=/nix-bin` mounts
398 the path `/nix-bin` as `/bin` inside the sandbox. If *source* is
399 followed by `?`, then it is not an error if *source* does not exist;
400 for example, `/dev/nvidiactl?` specifies that `/dev/nvidiactl`
401 only be mounted in the sandbox if it exists in the host filesystem.
403 If the source is in the Nix store, then its closure is added to
406 Depending on how Nix was built, the default value for this option
407 may be empty or provide `/bin/sh` as a bind-mount of `bash`.
409 {"build-chroot-dirs",
"build-sandbox-paths"}};
411 Setting<bool> sandboxFallback{
412 this,
true,
"sandbox-fallback",
"Whether to disable sandboxing when the kernel doesn't allow it."};
415 Setting<bool> requireDropSupplementaryGroups{
418 "require-drop-supplementary-groups",
420 Following the principle of least privilege,
421 Nix attempts to drop supplementary groups when building with sandboxing.
423 However this can fail under some circumstances.
424 For example, if the user lacks the `CAP_SETGID` capability.
425 Search `setgroups(2)` for `EPERM` to find more detailed information on this.
427 If you encounter such a failure, setting this option to `false` enables you to ignore it and continue.
428 But before doing so, you should consider the security implications carefully.
429 Not dropping supplementary groups means the build sandbox is less restricted than intended.
431 This option defaults to `true` when the user is root
432 (since `root` usually has permissions to call setgroups)
433 and `false` otherwise.
438 Setting<std::string> sandboxShmSize{
441 "sandbox-dev-shm-size",
445 This option determines the maximum size of the `tmpfs` filesystem
446 mounted on `/dev/shm` in Linux sandboxes. For the format, see the
447 description of the `size` option of `tmpfs` in mount(8). The default
452#if defined(__linux__) || defined(__FreeBSD__)
453 Setting<AbsolutePath> sandboxBuildDir{
460 The build directory inside the sandbox.
462 This directory is backed by [`build-dir`](#conf-build-dir) on the host.
466 Setting<std::optional<AbsolutePath>> buildDir{
471 Override the `build-dir` store setting for all stores that have this setting.
473 See also the per-store [`build-dir`](@docroot@/store/types/local-store.md#store-local-store-build-dir) setting.
476 Setting<std::set<std::filesystem::path>> allowedImpureHostPrefixes{
479 "allowed-impure-host-deps",
480 "Which prefixes to allow derivations to ask for access to (primarily for Darwin)."};
483 Setting<bool> darwinLogSandboxViolations{
486 "darwin-log-sandbox-violations",
487 "Whether to log Darwin sandbox access violations to the system log."};
490 Setting<bool> runDiffHook{
495 If true, enable the execution of the `diff-hook` program.
497 When using the Nix daemon, `run-diff-hook` must be set in the
498 `nix.conf` configuration file, and cannot be passed at the command
504 Setting<std::optional<AbsolutePath>> diffHook{
509 Absolute path to an executable capable of diffing build
510 results. The hook is executed if `run-diff-hook` is true, and the
511 output of a build is known to not be the same. This program is not
512 executed to determine if two results are the same.
514 The diff hook is executed by the same user and group who ran the
515 build. However, the diff hook does not have write access to the
516 store path just built.
518 The diff hook program receives three parameters:
520 1. A path to the previous build's results
522 2. A path to the current build's results
524 3. The path to the build's derivation
526 4. The path to the build's scratch directory. This directory
527 exists only if the build was run with `--keep-failed`.
529 The stderr and stdout output from the diff hook isn't displayed
530 to the user. Instead, it prints to the nix-daemon's log.
532 When using the Nix daemon, `diff-hook` must be set in the `nix.conf`
533 configuration file, and cannot be passed at the command line.
544 if (!runDiffHook.get()) {
547 return get(diffHook.get());
555 If set, the path to a program that can set extra derivation-specific
556 settings for this system. This is used for settings that can't be
557 captured by the derivation model itself and are too variable between
558 different versions of the same system to be hard-coded into nix.
560 The hook is passed the derivation path and, if sandboxes are
561 enabled, the sandbox directory. It can then modify the sandbox and
562 send a series of commands to modify various settings to stdout. The
563 currently recognized commands are:
565 - `extra-sandbox-paths`\
566 Pass a list of files and directories to be included in the
567 sandbox for this build. One entry per line, terminated by an
568 empty line. Entries have the same format as `sandbox-paths`.
572 Setting<bool> filterSyscalls{
577 Whether to prevent certain dangerous system calls, such as
578 creation of setuid/setgid files or adding ACLs or extended
579 attributes. Only disable this if you're aware of the
580 security implications.
583 Setting<bool> allowNewPrivileges{
586 "allow-new-privileges",
588 (Linux-specific.) By default, builders on Linux cannot acquire new
589 privileges by calling setuid/setgid programs or programs that have
590 file capabilities. For example, programs such as `sudo` or `ping`
591 should fail. (Note that in sandbox builds, no such programs are
592 available unless you bind-mount them into the sandbox via the
593 `sandbox-paths` option.) You can allow the use of such programs by
594 enabling this option. This is impure and usually undesirable, but
595 may be useful in certain scenarios (e.g. to spin up containers or
596 set up userspace network interfaces in tests).
601 Setting<StringSet> ignoredAcls{
603 {
"security.selinux",
"system.nfs4_acl",
"security.csm"},
606 A list of ACLs that should be ignored, normally Nix attempts to
607 remove all ACLs from files and directories in the Nix store, but
608 some ACLs like `security.selinux` or `system.nfs4_acl` can't be
609 removed even by root. Therefore it's best to just ignore them.
613 Setting<StringMap> impureEnv{
618 A list of items, each in the format of:
620 - `name=value`: Set environment variable `name` to `value`.
622 If the user is trusted (see `trusted-users` option), when building
623 a fixed-output derivation, environment variables set in this option
624 is passed to the builder if they are listed in [`impureEnvVars`](@docroot@/language/advanced-attributes.md#adv-attr-impureEnvVars).
626 This option is useful for, e.g., setting `https_proxy` for
627 fixed-output derivations and in a multi-user Nix installation, or
628 setting private access tokens when fetching a private repository.
632 Xp::ConfigurableImpureEnv};
634 Setting<Strings> hashedMirrors{
639 A list of web servers used by `builtins.fetchurl` to obtain files by
640 hash. Given a hash algorithm *ha* and a base-16 hash *h*, Nix tries to
641 download the file from *hashed-mirror*/*ha*/*h*. This allows files to
642 be downloaded even if they have disappeared from their original URI.
643 For example, given an example mirror `http://tarballs.nixos.org/`,
644 when building the derivation
648 url = "https://example.org/foo-1.2.3.tar.xz";
649 sha256 = "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae";
653 Nix will attempt to download this file from
654 `http://tarballs.nixos.org/sha256/2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae`
655 first. If it is not available there, it tries the original URI.
658 using ExternalBuilders = std::vector<ExternalBuilder>;
660 Setting<ExternalBuilders> externalBuilders{
665 Helper programs for building derivations, specified in JSON format, e.g.
668 [ {"systems": ["aarch64-linux"], "program": "/path/to/helper", "args": ["bla"]} ]
671 Whenever Nix needs to build a derivation for a system contained in *systems*, it will invoke *program* with the command line arguments *args*, and an additional argument containing the path of a JSON document that describes the build environment.
672 The JSON document looks like this:
678 "/nix/store/vj1c3wf9…-source-stdenv.sh",
679 "/nix/store/shkw4qm9…-default-builder.sh"
681 "builder": "/nix/store/s1qkj0ph…-bash-5.2p37/bin/bash",
683 "HOME": "/homeless-shelter",
684 "builder": "/nix/store/s1qkj0ph…-bash-5.2p37/bin/bash",
685 "nativeBuildInputs": "/nix/store/l31j72f1…-version-check-hook",
686 "out": "/nix/store/2yx2prgx…-hello-2.12.2"
690 "/nix/store/14dciax3…-glibc-2.32-54-dev",
691 "/nix/store/1azs5s8z…-gettext-0.21",
695 "out": "/nix/store/2yx2prgx…-hello-2.12.2"
697 "realStoreDir": "/nix/store",
698 "storeDir": "/nix/store",
699 "system": "aarch64-linux",
700 "tmpDir": "/private/tmp/nix-build-hello-2.12.2.drv-0/build",
701 "tmpDirInSandbox": "/build",
702 "topTmpDir": "/private/tmp/nix-build-hello-2.12.2.drv-0",
739std::string BaseSetting<LocalSettings::ExternalBuilders>::to_string()
const;
Definition configuration.hh:339
virtual T parse(const std::string &str) const
Definition config-impl.hh:139
virtual void appendOrSet(T newValue, bool append)
Definition config-impl.hh:70
Definition configuration.hh:476
Definition configuration.hh:242
Definition local-settings.hh:140
Definition local-settings.hh:41
Definition local-settings.hh:179
const AbsolutePath * getDiffHook() const
Definition local-settings.hh:542
const ExternalBuilder * findExternalDerivationBuilderIfSupported(const BasicDerivation &drv)
Definition globals.cc:283
GCSettings & getGCSettings()
Definition local-settings.hh:187
const AutoAllocateUidSettings * getAutoAllocateUidSettings() const
Definition local-settings.hh:201
bool isRootUser()
Definition users.cc:65
const T * get(const std::optional< T > &opt)
Definition util.hh:286