NIX-STORE-PRINT-ENV(1)

πŸ’– nix-store --print-env β€” from the General Commands Manual (girl help, it's a nix-store(1) situation) πŸ’–


✨ NAME ✨

nix-store --print-env β€” bestie, it prints out the build environment of a derivation πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

nix-store --print-env drvpath

🌸 DESCRIPTION 🌸

okay so. The --print-env operation prints out the environment of a derivation in a format that can be evaluated by a shell. No cap, it's genuinely that simple β€” you hand it a derivation path, it hands you environment variables back, and they're shell-eval-able. slay.

And the plot twist 🫢: the command line arguments of the builder get placed in the variable _args.


πŸŽ€ OPTIONS πŸŽ€

These following options are allowed for all nix-store operations, but heads up β€” they may not always have an effect. it's giving "available everywhere, does something sometimes" πŸ’€

  • --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. Plus, 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. iconic behaviour honestly 🌈

⚠️ 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. so don't even try it bestie 😭 it will Not work.

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). the way it just counts up πŸ’… periodt.


…part 1 of 3, bestie, keep scrolling πŸ‘€

✨ COMMON OPTIONS ✨

okay so bestie, here's the tea β˜• β€” like most Nix commands speak this exact same flag dialect, so once you learn these you've basically unlocked the whole squad. let's go πŸ’…


  • --help β€” prints a lil summary of the command syntax and then dips out. that's it, that's the whole move ✨
  • --version β€” spits the Nix version number onto standard output and exits. iconic, no notes πŸŽ€
  • --verbose / -v β€” turns the volume UP on those diagnostic messages printed to standard error. here's the thing that matters: for every Nix operation, whatever goes to standard output is well-defined, and any diagnostic chatter goes to standard error, never to standard output. periodt.

you can drop this flag as many times as you want, and rn these are the verbosity levels that exist:

  • 0 "Errors only" β€” only prints messages telling you why the Nix invocation failed. tough love πŸ’€
  • 1 "Informational" β€” prints useful messages about what Nix is actually doing. this one is the default 🫢
  • 2 "Talkative" β€” more informational messages. she's chatty now.
  • 3 "Chatty" β€” even more informational messages. okay she's really talking.
  • 4 "Debug" β€” prints debug info. tech support mode πŸ”₯
  • 5 "Vomit" β€” prints a vast amount of debug info. the name is not a joke bestie, it's giving firehose 🌊
  • --quiet β€” turns the volume DOWN on diagnostic messages to standard error. it's literally the inverse of -v / --verbose.

this one can also be given multiple times, and yeah, same verbosity level list as above πŸ‘†

  • --log-format format β€” use this to switch up how the log output gets 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 β€” the schema itself is relatively stable, but the format of the error-messages (namely of the msg-field) can change between releases. so like... don't hardcode your whole personality around it, k? 😭
  • bar β€” only shows a progress bar during the builds. minimal queen πŸ’…
  • bar-with-logs β€” shows the raw logs, with the progress bar chilling at the bottom.
  • --no-build-output / -Q β€” by default, anything builders write to standard output and standard error gets echoed to the Nix command's standard error. this option suppresses that behaviour. important heads up tho: the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix, no matter what 🫢
  • --max-jobs / -j number β€” sets the max number of build jobs Nix will run in parallel to number. say auto if you want it to use the number of CPUs on the system. the default comes from the max-jobs configuration setting, which itself defaults to 1. a higher value is useful on SMP systems or to exploit I/O latency ✨

setting it to 0 disallows building on the local machine β€” and that's <highkey>genuinely useful</highkey> when you want builds to happen only on remote builders 🌈

  • --cores β€” sets the value of the NIX_BUILD_CORES environment variable in the invocation of builders. builders can use that variable however they like to control their max 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 set, or 1 otherwise. and the value 0 means the builder should go feral and use all available CPU cores in the system πŸ’€πŸ”₯
  • --max-silent-time β€” sets the max number of seconds a builder can go without producing any data on standard output or standard error. default comes from the max-silent-time configuration setting. 0 means no time-out πŸ•
  • --timeout β€” sets the max number of seconds a builder can run. default comes from the timeout configuration setting. 0 means no timeout.
  • --keep-going / -k β€” keeps going in case of failed builds, to the greatest extent possible. so 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), possibly killing builds in progress (that's the parallel or distributed builds case btw) πŸ’€
  • --keep-failed / -K β€” says that in case of a build failure, the temporary directory (usually in /tmp) where the build happens should not be deleted. the path of the build directory gets printed as an informational message, so you can go poke around πŸ•΅οΈβ™€οΈ
  • --fallback β€” whenever Nix tries to build a derivation where substitutes are known for each output path, but realising those output paths through the substitutes fails, it falls back on building the derivation.

the most common scenario where this saves you: you've registered substitutes to do binary distribution from, say, a network repository. if that repository is down, realising the derivation will fail. with this option, Nix builds the derivation instead β€” so installation from binaries falls back on installation from source ✨ this isn't the default because it's generally not desirable for a transient failure in obtaining substitutes to lead to a full build from source (plus all the resource consumption that comes with it).

  • --readonly-mode β€” when this is on, no attempt is made to open the Nix database. most Nix operations do need database access, so those operations will just straight up fail πŸ™ˆ
  • --arg name value β€” accepted by nix-env, nix-instantiate, nix-shell and nix-build. when evaluating Nix expressions, the evaluator will automatically try to call functions that it encounters. it can automatically call functions where every argument has a default value, like { argName ? defaultValue }: ....

with --arg, you can also call functions that have arguments without a default value (or override a default value). so if the evaluator hits a function with an argument named name, it calls it with 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 (like 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 that with --arg, e.g. nix-env --install --attr pkgname --arg system "i686-freebsd". (heads up: since the argument is a Nix string literal, you gotta escape the quotes 😀)

  • --arg-from-file name path β€” passes the contents of the file path as the argument name to Nix functions πŸ“„
  • --arg-from-stdin name β€” passes the contents of stdin as the argument name to Nix functions 🫢
  • --argstr name value β€” same energy as --arg, except the value isn't a Nix expression, it's a string. so instead of --arg system "i686-linux" (the outer quotes are just there to keep your shell happy), you can say --argstr system i686-linux. way less escaping drama ✨
  • --attr / -A attrPath β€” selects 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 example, given a top-level Nix expression e, the attribute path xorg.xorgserver would make the expression e.xorg.xorgserver get used. check out nix-env --install for some concrete examples πŸ‘‘

and it's not just names β€” you can specify array indices too. 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 πŸ”’

  • --eval-store store-url β€” the URL of the Nix store to use for evaluation, i.e. where to store derivations (.drv files) and inputs referenced by them.
  • --expr / -E β€” interprets the command line arguments as a list of Nix expressions to be parsed and evaluated, instead of 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 where you can build the packages returned by the expression. if you want a shell that contains the built packages ready for use, hand your expression to the nix-shell --packages convenience flag instead 🐚

  • -I / --include path β€” adds an entry to the list of search paths used to resolve lookup paths. this option can be given multiple times.

paths added via -I take precedence over the nix-path configuration setting and the NIX_PATH environment variable. she goes first, periodt πŸ’…

  • --impure β€” allows access to mutable paths and repositories. chaos mode unlocked πŸ”“
  • --option name value β€” sets the Nix configuration option name to value. this overrides settings in the Nix configuration file (see nix.conf5) βš™οΈ
  • --repair β€” fixes corrupted or missing store paths by redownloading or rebuilding them. note that this is slow, because it has to compute a cryptographic hash of the contents of every path in the closure of the build. also go read the warning under nix-store --repair-path 🚨
πŸ“ Note β€” see man nix.conf for overriding configuration settings with command line flags.

🌸 Common Environment Variables 🌸

Okay bestie, these are the env vars that basically every Nix command out there knows how to read. Most Nix commands interpret the following environment variables. 🫢


βš™οΈ Configuration environment variables

These ones decide how Nix loads its configuration in the first place. See the configuration file documentation for full details, 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. Yes, right there in the variable. Iconic.

🏬 Store setting environment variables

  • NIX_IGNORE_SYMLINK_STORE β€” ngl this one's got lore. 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.
Heads up bestie β€” 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, like this:
$ 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. (Same trio as above, they stay meddling ✨)

  • 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 β€” uses 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. Numbers, but make it ✨diagnostic✨.
  • 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. Very main character of your expressions, honestly.
  • 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. Pick your fighter. πŸ’€
  • 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. Fr fr. 😭

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: NIX_CONFIG_HOME
  • user state directory: NIX_STATE_HOME
  • user cache directory: NIX_CACHE_HOME

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. Slay, glow-up era. πŸ’…

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. Same rule, three times, consistency queen. πŸ‘‘


πŸŽ€ 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. Vibe check on your shell, basically.

🌈 Appendix: OS-specific conventions

This information is not Nix-specific, but is referenced above. So like, general OS lore. 🧚

🐧 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 (default ~/.config)
  • XDG_STATE_HOME (default ~/.local/state)
  • XDG_CACHE_HOME (default ~/.cache)
  • XDG_CONFIG_DIRS (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% β€” per-user roaming application data
  • %LOCALAPPDATA% β€” per-user local application data
  • %PROGRAMDATA% β€” system-wide application data

πŸ’– Example

Here's the whole thing in action, bestie:

$ nix-store --print-env $(nix-instantiate '<nixpkgs>' -A firefox)
…
export src; src='/nix/store/plpj7qrwcz94z2psh6fchsi7s8yihc7k-firefox-12.0.source.tar.bz2'
export stdenv; stdenv='/nix/store/7c8asx3yfrg5dg1gzhzyq2236zfgibnn-stdenv'
export system; system='x86_64-linux'
export _args; _args='-e /nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25c-default-builder.sh'

And that's a wrap on the whole page, gorgeous. 🫢 The little footer at the bottom just says nix-store --print-env(1) β€” that's the source/version and date line, holding it down quietly at the end of the manpage like the icon it is. Go forth and print-env responsibly. periodt βœ¨πŸ‘‘

πŸ“¦ where this page comes from