NIX-STORE-REALISE(1)

πŸ’– nix-store --realise(1) πŸ’–

General Commands Manual has entered the chat, and bestie… it's giving build or fetch store objects energy. No cap. Let's get into it. πŸ’…


🌸 Name 🌸

nix-store --realise - build or fetch store objects


✨ Synopsis ✨

nix-store {--realise | -r} paths… [--dry-run]

πŸ’– Description πŸ’–

Okay so every one of paths is processed like this:

  • If the path leads to a store derivation:
    1. If it is not valid, substitute the store derivation file itself.
    2. Realise its output paths:
      • Try to fetch from substituters the store objects associated with the output paths in the store derivation's closure.
        • With content-addressing derivations: Determine the output paths to realise by querying content-addressed realisation entries in the Nix database.
      • For any store paths that cannot be substituted, produce the required store objects:
        1. Realise all outputs of the derivation's dependencies.
        2. Run the derivation's builder executable.
  • Otherwise, and if the path is not already valid: Try to fetch the associated store objects in the path's closure from substituters.

If no substitutes are available and no store derivation is given, realisation fails.

The resulting paths are printed on standard output. For non-derivation arguments, the argument itself is printed.


🚨 Special exit codes for build failure 🚨

The 1xx status codes are used when requested builds failed. The following codes are in use:

  • 100 Generic build failure β€” The builder process returned with a non-zero exit code.
  • 101 Build timeout β€” The build was aborted because it did not complete within the specified timeout.
  • 102 Hash mismatch β€” The build output was rejected because it does not match the outputHash attribute of the derivation.
  • 104 Not deterministic β€” The build succeeded in check mode but the resulting output is not binary reproducible.

With the --keep-going flag it's possible for multiple failures to occur. In this case the 1xx status codes are or combined using bitwise OR.

0b1100100
     ^^^^
     |||`- timeout
     ||`-- output hash mismatch
     |`--- build failure
     `---- not deterministic

πŸŽ€ Options πŸŽ€

  • --dry-run β€” Print on standard error a description of what packages would be built or downloaded, without actually performing the operation.
  • --ignore-unknown β€” If a non-derivation path does not have a substitute, then silently ignore it.
  • --check β€” This option allows you to check whether a derivation is deterministic. It rebuilds the specified derivation and checks whether the result is bitwise-identical with the existing outputs, printing an error if that’s not the case. The outputs of the specified derivation must already exist. When used with -K, if an output path is not identical to the corresponding output from the previous build, the new output path is left in /nix/store/name.check.

πŸŽ€ Options πŸŽ€ (the ones allowed for all nix-store operations)

The following options are allowed for all nix-store operations, but may not always have an effect.

  • --add-root path β€” Causes the result of a realisation (--realise and --force-realise) to be registered as a root of the garbage collector. path will be created as a symlink to the resulting store path. In addition, a uniquely named symlink to path will be created in /nix/var/nix/gcroots/auto/. For instance,
$ nix-store --add-root /home/eelco/bla/result --realise ...

$ ls -l /nix/var/nix/gcroots/auto
lrwxrwxrwx    1 ... 2005-03-13 21:10 dn54lcypm8f8... -> /home/eelco/bla/result

$ ls -l /home/eelco/bla/result
lrwxrwxrwx    1 ... 2005-03-13 21:10 /home/eelco/bla/result -> /nix/store/1r11343n6qd4...-f-spot-0.0.10

Thus, when /home/eelco/bla/result is removed, the GC root in the auto directory becomes a dangling symlink and will be ignored by the collector.

🚨 Warning 🚨 Note that it is not possible to move or rename GC roots, since the symlink in the auto directory will still point to the old location.

If there are multiple results, then multiple symlinks will be created by sequentially numbering symlinks beyond the first one (e.g., foo, foo-2, foo-3, and so on).

✨ COMMON OPTIONS ✨

okay so bestie, listen πŸ’– most Nix commands take these bad boys right here. they're the shared wardrobe, the communal snack drawer, the universal vibe. let's go one by one, no skipping, no skimming, periodt.

  • --help β€” prints a lil summary of the command syntax and then dips out. that's it, that's the whole thing 🫢
  • --version β€” prints the Nix version number on standard output and exits. iconic, brief, slays.
  • --verbose, -v β€” turns up the volume on the diagnostic messages printed to standard error. here's the deal: for every Nix operation, whatever shows up on standard output is well-defined, and any diagnostic tea gets spilled on standard error, never on standard output. you can pass this option over and over again, and each time it stacks πŸ“ˆ

currently these are the verbosity levels that exist:

  • 0 β€œErrors only” β€” only prints messages telling you why the Nix invocation flopped. nothing else, no commentary πŸ’€
  • 1 β€œInformational” β€” prints actually useful messages about what Nix is up to. this is the default, so this is the baseline reality you live in.
  • 2 β€œTalkative” β€” more informational messages. it's yapping now, affectionately.
  • 3 β€œChatty” β€” even more informational messages. the yapping has escalated 🌸
  • 4 β€œDebug” β€” prints debug information. we're in the engine room now.
  • 5 β€œVomit” β€” prints vast amounts of debug information. truly unhinged quantities. you asked for this πŸ‘‘
  • --quiet β€” turns the volume down on diagnostic messages printed to standard error. it's the inverse option to -v / --verbose. also stackable, so you can pass it repeatedly, and it walks back down the verbosity levels list from up above.
  • --log-format format β€” changes how the log output is formatted, where format is one of these:
    • raw β€” the raw format, exactly as outputted by nix-build.
    • internal-json β€” outputs the logs in a structured manner.
Warning ⚠️ while the schema itself is relatively stable, the format of the error-messages (namely of the msg-field) can change between releases. so like, don't hardcode your whole personality around parsing it, bestie.
  • bar β€” only displays a progress bar while the builds are happening. minimal, clean, a vibe.
  • bar-with-logs β€” displays the raw logs, with the progress bar chillin' at the bottom.
  • --no-build-output, -Q β€” by default, whatever the builders write to standard output and standard error gets echoed to the Nix command's standard error. this option suppresses that behaviour entirely, shhh 🀫 note that the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix no matter what. the receipts are eternal.
  • --max-jobs, -j number β€” sets the maximum number of build jobs Nix will run in parallel to the number you give it. you can literally say auto to use however many CPUs the system has. the default comes from the max-jobs configuration setting, which itself defaults to 1. a higher value is a good move on SMP systems, or when you wanna exploit I/O latency like the main character you are πŸ”₯

setting it to 0 disallows building on the local machine at all β€” which is exactly what you want when you only want builds happening on remote builders.

  • --cores β€” sets the value of the NIX_BUILD_CORES environment variable in the invocation of builders. builders can use this variable however they please to control the maximum amount of parallelism. for instance, in Nixpkgs, if the derivation attribute enableParallelBuilding is set to true, the builder passes the -jN flag to GNU Make. it defaults to the value of the cores configuration setting, if that's set β€” otherwise 1. the value 0 means the builder should use all available CPU cores in the system. go wild πŸ’…
  • --max-silent-time β€” sets the maximum number of seconds a builder can go without producing any data on standard output or standard error before it gets timed out. the default comes from the max-silent-time configuration setting. 0 means no time-out. the silence can continue forever, apparently.
  • --timeout β€” sets the maximum number of seconds a builder can run. the default comes from the timeout configuration setting. 0 means no timeout. zero is doing a lot of heavy lifting in this section honestly.
  • --keep-going, -k β€” keep going even when builds fail, to the greatest extent possible. like, if building an input of some derivation fails, Nix will still build the other inputs β€” but not the derivation itself. without this option, Nix stops if any build fails (except for builds of substitutes), and it might even kill builds that are already in progress (in the case of parallel or distributed builds). ruthless 😭
  • --keep-failed, -K β€” says that when a build fails, the temporary directory where the build took place (usually in /tmp) should not be deleted. the path of the build directory gets printed as an informational message, so you can go poke around the crime scene πŸ”
  • --fallback β€” whenever Nix tries to build a derivation where substitutes are known for every output path, but realising those output paths through the substitutes fails, fall back on actually building the derivation.

the most common scenario where this hits is when we have registered substitutes so we can do binary distribution from, like, a network repository. if that repository is down, realising the derivation will fail. with this option specified, Nix just builds the derivation instead. so basically: installation from binaries falls back on installation from source πŸ¦‹

this is not the default, and here's why β€” it's generally not desirable for a transient failure in getting the substitutes to lead to a full build from source (and the whole associated resource consumption that comes with it). so it's a choice, not a lifestyle.

  • --readonly-mode β€” when this is used, no attempt is made to open the Nix database. most Nix operations genuinely do need database access, so those operations are gonna fail. fair warning 🫠
  • --arg name value β€” this option is accepted by nix-env, nix-instantiate, nix-shell and nix-build. when evaluating Nix expressions, the expression evaluator will automatically try to call functions that it encounters. it can automatically call functions where every argument has a default value (e.g., { argName ? defaultValue }: ...).

with --arg, you can also call functions that have arguments without a default value (or override a default value). so like: if the evaluator encounters a function with an argument named name, it will call it with value as the value.

for instance, the top-level default.nix in Nixpkgs is actually a function:

{ # The system (e.g., `i686-linux') for which to build the packages.
system ? builtins.currentSystem
...
}: ...

so if you call this Nix expression (e.g., when you do nix-env --install --attr pkgname), the function gets called automatically using the value builtins.currentSystem for the system argument. you can override it using --arg, e.g., nix-env --install --attr pkgname --arg system "i686-freebsd". (note that since the argument is a Nix string literal, you have to escape the quotes, bestie β€” the shell will absolutely not be nice to you otherwise πŸ’€)

  • --arg-from-file name path β€” pass the contents of the file at path as the argument name to Nix functions. file goes in, argument comes out ✨
  • --arg-from-stdin name β€” pass the contents of stdin as the argument name to Nix functions. pipe it in, babe πŸŽ€
  • --argstr name value β€” this is like --arg, except the value is not a Nix expression, it's a string. so instead of --arg system "i686-linux" (the outer quotes are there to keep the shell happy), you can just say --argstr system i686-linux. so much less quoting drama 🌈
  • --attr, -A attrPath β€” select an attribute from the top-level Nix expression being evaluated. (nix-env, nix-instantiate, nix-build and nix-shell only.) the attribute path attrPath is a sequence of attribute names separated by dots. for instance, given a top-level Nix expression e, the attribute path xorg.xorgserver would cause the expression e.xorg.xorgserver to be used. see nix-env --install for some concrete examples.

in addition to attribute names, you can also specify array indices. for instance, the attribute path foo.3.bar selects the bar attribute of the fourth element of the array in the foo attribute of the top-level expression. yes, indices are zero-based, yes it's 3 for the fourth one, don't think about it too hard, just trust πŸ’œ

  • --eval-store store-url β€” the URL to the Nix store to use for evaluation, i.e. where to store derivations (.drv files) and inputs referenced by them.
  • --expr, -E β€” interpret the command line arguments as a list of Nix expressions to be parsed and evaluated, rather than as a list of file names of Nix expressions. (nix-instantiate, nix-build and nix-shell only.)

for nix-shell, this option is commonly used to give you a shell in which you can build the packages returned by the expression. if you want a shell that contains the built packages ready for use, give your expression to the nix-shell --packages convenience flag instead. different tool, different result, don't mix them up 🫢

  • -I, --include path β€” add an entry to the list of search paths used to resolve lookup paths. this option can be given multiple times. stack 'em up πŸ“š

paths added through -I take precedence over the nix-path configuration setting and the NIX_PATH environment variable. they're the VIPs of the search path list πŸ‘‘

  • --impure β€” allow access to mutable paths and repositories. living dangerously, love that for you.
  • --option name value β€” set the Nix configuration option name to value. this overrides settings in the Nix configuration file (see nix.conf5).
  • --repair β€” fix corrupted or missing store paths by redownloading or rebuilding them. note that this is slow, because it requires computing a cryptographic hash of the contents of every path in the closure of the build. also note the warning under nix-store --repair-path. it works, it's just not fast, ok? go make a snack while it runs πŸ“
Note β€” see man nix.conf for overriding configuration settings with command line flags.

✨ Common Environment Variables ✨

Okay bestie, most Nix commands be like, yeah, I read these env vars, periodt. So here’s the whole vibe. πŸ’…


🌸 Configuration environment variables 🌸

These lil’ guys affect how Nix loads its config. The configuration file docs have the full tea, iykyk. β˜•οΈ

  • NIX_CONF_DIR β€” Overrides the system configuration directory. πŸ—‚οΈ
  • NIX_USER_CONF_FILES β€” Overrides the user configuration file locations. πŸ“
  • NIX_CONFIG β€” Provides configuration settings inline. πŸ“

πŸ›οΈ Store setting environment variables πŸ›οΈ

  • NIX_IGNORE_SYMLINK_STORE β€” So normally, the Nix store directory (typically /nix/store) is not allowed to contain any symlink components. This is to prevent β€œimpure” builds. Builders sometimes β€œcanonicalise” paths by resolving all symlink components. Thus, builds on different machines (with /nix/store resolving to different locations) could yield different results. This is generally not a problem, except when builds are deployed to machines where /nix/store resolves differently. If you are sure that you’re not going to do that, you can set NIX_IGNORE_SYMLINK_STORE to 1. 😈

Note that if you’re symlinking the Nix store so that you can put it on another file system than the root file system, on Linux you’re better off using bind mount points, e.g.,

$ mkdir /nix
$ mount -o bind /mnt/otherdisk/nix /nix

Consult the mount 8 manual page for details. πŸ“–

  • NIX_STORE_DIR β€” Overrides the location of the Nix store. On Unix, the default is /nix/store, set at compile time by the libstore:store-dir build option. On Windows, there is no compile-time setting; the default is %PROGRAMDATA%\nix\store, determined at runtime using the %PROGRAMDATA% Known Folder. πŸͺŸ

See the Store Types chapter; each store has a store setting linking back to this as its default. πŸ”—

  • NIX_LOG_DIR β€” Overrides the location of the Nix log directory. On Unix, the default is /nix/var/log/nix, set at compile time by the log-dir build option. On Windows, there is no compile-time setting; the default is %PROGRAMDATA%\nix\log, determined at runtime using the %PROGRAMDATA% Known Folder.

The Local Store, Local Daemon Store, and Experimental SSH Store with filesystem mounted have per-store settings that override this. πŸͺ

  • NIX_STATE_DIR β€” Overrides the location of the Nix state directory. On Unix, the default is ${localstatedir}/nix, where localstatedir is a compile-time build option defaulting to /nix/var. On Windows, there is no compile-time setting; the default is %PROGRAMDATA%\nix\state, determined at runtime using the %PROGRAMDATA% Known Folder.

The Local Store, Local Daemon Store, and Experimental SSH Store with filesystem mounted have per-store settings that override this. πŸͺ

  • NIX_DAEMON_SOCKET_PATH β€” Overrides the path to the Unix domain socket used to communicate with the Nix daemon. Defaults to daemon-socket/socket within the state directory (see NIX_STATE_DIR). πŸ”Œ

See the Local Daemon Store documentation for details on how the socket path is resolved. πŸ“‘

  • TMPDIR β€” Use the specified directory to store temporary files. In particular, this includes temporary build directories; these can take up substantial amounts of disk space. The default is /tmp. πŸ’Ύ
  • NIX_REMOTE β€” Overrides the store setting. 🌐

🧠 Nix language evaluator settings 🧠

  • NIX_SHOW_STATS β€” If set to 1, Nix will print some evaluation statistics, such as the number of values allocated. πŸ“Š
  • NIX_COUNT_CALLS β€” If set to 1, Nix will print how often functions were called during Nix expression evaluation. This is useful for profiling your Nix expressions. πŸ“ˆ
  • GC_INITIAL_HEAP_SIZE β€” If Nix has been configured to use the Boehm garbage collector, this variable sets the initial size of the heap in bytes. It defaults to 384 MiB. Setting it to a low value reduces memory consumption, but will increase runtime due to the overhead of garbage collection. ♻️
  • NIX_PATH β€” A colon-separated list of search path entries used to resolve lookup paths. 🧭

This environment variable overrides the value of the nix-path configuration setting.

It can be extended using the -I option.

Example:

$ export NIX_PATH=/home/eelco/Dev:nixos-config=/etc/nixos

If NIX_PATH is set to an empty string, resolving search paths will always fail. 🚫

Example:

$ NIX_PATH= nix-instantiate --eval '<nixpkgs>'
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)

🏠 User Directories 🏠

Nix supports per-user configuration, state, and cache directories. The following environment variables override the locations for these directories: πŸ“‚

  • user configuration directory (anchor: user-conf-dir): NIX_CONFIG_HOME (anchor: env-NIXCONFIGHOME) πŸ› οΈ
  • user state directory: NIX_STATE_HOME (anchor: env-NIXSTATEHOME) πŸ“
  • user cache directory: NIX_CACHE_HOME (anchor: env-NIXCACHEHOME) πŸ—ƒοΈ

When these are not set, the defaults depend on the platform: 🌍

  • On Unix, the XDG base directories: $XDG_CONFIG_HOME/nix, $XDG_STATE_HOME/nix, $XDG_CACHE_HOME/nix 🐧
  • On Windows, the Windows Known Folders: %APPDATA%\nix\config, %LOCALAPPDATA%\nix\state, %LOCALAPPDATA%\nix\cache πŸͺŸ

For backwards compatibility, legacy Nix commands (e.g. nix-env, nix-channel) use dotfiles in $HOME instead of these directories unless use-xdg-base-directories is enabled. New Nix commands (experimental) use the proper directories by default. πŸ‘΅

When use-xdg-base-directories is enabled, the configuration directory is resolved as: πŸ”

  1. $NIX_CONFIG_HOME, if it is defined
  2. Otherwise, the platform default (e.g. $XDG_CONFIG_HOME/nix on Unix)

Likewise for the state and cache directories. ✨


🎲 Miscellaneous environment variables 🎲

  • IN_NIX_SHELL β€” Indicator that tells if the current environment was set up by nix-shell. It can have the values pure or impure. 🐚

πŸ“š Appendix: OS-specific conventions πŸ“š

This information is not Nix-specific, but is referenced above. So here’s a lil’ bonus. 🎁

🐧 Unix: XDG Base Directories 🐧

The XDG Base Directory Specification defines standard locations for user-specific configuration, state, and cache files on Unix systems. πŸ—ΊοΈ

The following environment variables are used:

  • XDG_CONFIG_HOME (anchor: env-XDGCONFIGHOME) (default ~/.config) βš™οΈ
  • XDG_STATE_HOME (anchor: env-XDGSTATEHOME) (default ~/.local/state) πŸ“Œ
  • XDG_CACHE_HOME (anchor: env-XDGCACHEHOME) (default ~/.cache) πŸ—„οΈ
  • XDG_CONFIG_DIRS (anchor: env-XDGCONFIGDIRS) (default /etc/xdg) β€” colon-separated list of additional configuration base directories, searched after XDG_CONFIG_HOME πŸ”Ž

πŸͺŸ Windows: Known Folders πŸͺŸ

On Windows, Known Folders provide standard locations for application data on Windows. πŸͺŸ

The relevant folders are:

  • %APPDATA% (anchor: env-APPDATA) β€” per-user roaming application data πŸš€
  • %LOCALAPPDATA% (anchor: env-LOCALAPPDATA) β€” per-user local application data 🏠
  • %PROGRAMDATA% (anchor: env-PROGRAMDATA) β€” system-wide application data 🌐

🌟 Examples 🌟

This operation is typically used to build store derivations produced by nix-instantiate: πŸ—οΈ

$ nix-store --realise $(nix-instantiate ./test.nix)
/nix/store/6gwmy5jcnwdlz6aqqhksz863f1l8xc2w-aterm-2.3.1

This is essentially what nix-build does. 🎯

To test whether a previously-built derivation is deterministic:

$ nix-build '<nixpkgs>' --attr hello --check -K

Use nix-store --read-log to show the stderr and stdout of a build:

$ nix-store --read-log $(nix-instantiate ./test.nix)

And that’s the whole vibe, bestie! This was nix-store --realise(1), no cap. Stay slay, keep your store pure, and don’t forget to read the docs. πŸ’–βœ¨πŸ‘‘

πŸ“¦ where this page comes from