nix-instantiate(1)

πŸ’– nix-instantiate(1) β€” General Commands Manual πŸ’–

okay bestie, pull up a chair, because this one is the glow-up tool. it's nix-instantiate, from the Nix manual, and it's literally the moment your cute little Nix expressions become real, actual store derivations. no cap. πŸ¦‹


✨ NAME ✨

nix-instantiate β€” instantiate store derivations from Nix expressions

like, that's the whole vibe. expressions in, store derivations out. periodt. πŸ’…


🌸 SYNOPSIS 🌸

nix-instantiate [--parse | --eval [--strict] [--raw | --json | --xml] ] [--read-write-mode] [--arg name value] [{--attr| -A} attrPath] [--add-root path] [--expr | -E] files…

nix-instantiate --find-file files…

two whole modes, two whole moods. the first one is the main character ✨, the second one is the girl who just wants to find a file. πŸŽ€


🧚 DESCRIPTION 🧚

so nix-instantiate takes your (high-level) Nix expressions and produces store derivations out of them. it evaluates the Nix expressions sitting inside each of the files β€” and if you don't hand it any files at all, it just quietly defaults to ./default.nix, because of course it does. πŸ™ˆ

now, every top-level expression you feed it should evaluate to one of these three things, no exceptions, that's the rule bestie:

  • a derivation πŸŽ€
  • a list of derivations 🌈
  • a set of derivations ⭐

and then it prints the paths of all the resulting store derivations straight onto standard output. iconic behaviour, honestly. πŸ‘‘

oh and one tiny lil thing that's lowkey important: if files is just the character -, then the Nix expression gets read from standard input instead. cute, right? πŸ’œ


πŸ‘‘ OPTIONS πŸ‘‘

this is the part where the choices start, and there are a lot, so stay with me bestie, we're going through every single one.

  • --add-root path β€” gotta go read the matching option over in nix-store, that's where the lore lives. iykyk. 🫢
  • --parse β€” just parses the input files, nothing more, and prints their abstract syntax trees on standard output as a Nix expression. no evaluation, no thoughts, head empty, just vibes. 🌸
  • --eval β€” just parses and evaluates the input files, then prints the resulting values on standard output. and here's the tea: store derivations are not serialized and written to the store β€” they're instead just hashed and discarded. 😭
⚠️ Warning πŸ‘€ this option produces output which can be parsed as a Nix expression which will produce a different result than the input expression when evaluated. so it looks the same but it is not the same. for example, these two Nix expressions print the same result despite having different meaning:
$ nix-instantiate --eval --expr '{ a = {}; }'
{ a = <CODE>; }
$ nix-instantiate --eval --expr '{ a = <CODE>; }'
{ a = <CODE>; }
girl. they are NOT the same girl. πŸ’€

for human-readable output, nix eval (experimental) is more informative:

$ nix-instantiate --eval --expr 'a: a'
<LAMBDA>
$ nix eval --expr 'a: a'
Β«lambda @ Β«stringΒ»:1:1Β»

for machine-readable output, the --xml option produces unambiguous output:

$ nix-instantiate --eval --xml --expr '{ foo = <CODE>; }'
<?xml version='1.0' encoding='utf-8'?>
<expr>
  <attrs>
    <attr column="3" line="1" name="foo">
      <unevaluated />
    </attr>
  </attrs>
</expr>

that XML? ✨*precise*✨. that XML? unambiguous. that XML? slayed. πŸ”₯

  • --find-file β€” this one looks up the given files in Nix's search path (as specified by the NIX_PATH environment variable πŸ”). if it finds them, it prints the corresponding absolute paths on standard output. for instance, if NIX_PATH is nixpkgs=/home/alice/nixpkgs, then nix-instantiate --find-file nixpkgs/default.nix will print /home/alice/nixpkgs/default.nix. she found it, she's serving, she's absolute. πŸ’…
  • --strict β€” when you use this one with --eval, it recursively evaluates list elements and attributes. normally, those sub-expressions are just left unevaluated, because the Nix language is lazy β€” and honestly, same. πŸ™ˆ
⚠️ Warning this option can cause non-termination, because lazy data structures can be infinitely large. so if it never comes back... that's why bestie. 😭
  • --raw β€” with --eval, the evaluation result must be a string, and that string gets printed verbatim β€” no quoting, no escaping, no trailing newline. we are not decorating it, we are not adding anything, we are just letting it breathe. 🌿
  • --json β€” with --eval, prints the resulting value as an JSON representation of the abstract syntax tree instead of as a Nix expression. for the girls who speak JSON. πŸ“Š
  • --xml β€” with --eval, prints the resulting value as an XML representation of the abstract syntax tree instead of as a Nix expression. the schema is the same as that used by the toXML built-in. yes, the same one. she's consistent like that. πŸŽ€
  • --read-write-mode β€” with --eval, this performs evaluation in read/write mode so that nix language features that require it will still work (at the cost of needing to do instantiation of every evaluated derivation πŸ’€). heads up though: if this option is not enabled, there may be uninstantiated store paths in the final output. so like, either you instantiate everything, or you live with the loose ends. your call bestie. πŸ¦‹

πŸŽ€ Common Options πŸŽ€

Okay bestie, so like most Nix commands accept these same little guys. They're the shared wardrobe, the communal closet, the it-girl basics that basically every Nix command is wearing. Let's go πŸ’…

  • --help β€” prints out a lil summary of the command syntax and then dips. That's it. That's the whole job.
  • --version β€” prints the Nix version number on standard output and exits. Short, sweet, iconic.
  • --verbose / -v β€” turns up the volume on diagnostic messages printed to standard error. Here's the thing though: for every Nix operation, whatever goes to standard output is well-defined β€” all the messy diagnostic tea goes to standard error, never to standard output. You can stack this option over and over. Current verbosity levels are:
    • 0 "Errors only" β€” only prints messages explaining why the Nix invocation failed. That's it, that's the whole vibe 😭
    • 1 "Informational" β€” prints useful messages about what Nix is doing. This is the default ✨
    • 2 "Talkative" β€” prints more informational messages.
    • 3 "Chatty" β€” prints even more informational messages.
    • 4 "Debug" β€” prints debug information.
    • 5 "Vomit" β€” prints vast amounts of debug information. Yes it's literally called Vomit. Not me screaming πŸ’€
  • --quiet β€” turns the volume down on diagnostic messages printed to standard error. It's the inverse option to -v / --verbose. Can be specified repeatedly too, so go off. See the verbosity levels list above πŸ‘†
  • --log-format format β€” changes the output of the log format, where format is one of:
    • raw β€” the raw format, 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 don't get too attached, bestie 😭
  • bar β€” only display a progress bar during the builds. Minimalist queen πŸŽ€
  • bar-with-logs β€” display the raw logs, with the progress bar at the bottom. Best of both worlds, fr fr.
  • --no-build-output / -Q β€” by default, output written by builders to standard output and standard error gets echoed to the Nix command's standard error. This option suppresses that behaviour. Note that the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix. So nothing is lost, it's just being quiet about it 🫢
  • --max-jobs / -j number β€” sets the maximum number of build jobs Nix will perform in parallel to the specified number. Specify auto to use the number of CPUs in the system. The default is specified by 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 β€” which is exactly what you want 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 this variable at their discretion 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 set, or 1 otherwise. The value 0 means that the builder should use all available CPU cores in the system. All of them. Every last one. Periodt πŸ”₯
  • --max-silent-time β€” sets the maximum number of seconds that a builder can go without producing any data on standard output or standard error. The default is specified by the max-silent-time configuration setting. 0 means no time-out. Silence really is golden, apparently ✨
  • --timeout β€” sets the maximum number of seconds that a builder can run. The default is specified by the timeout configuration setting. 0 means no timeout. Live your truth, builder πŸ’…
  • --keep-going / -k β€” keep going in case of failed builds, to the greatest extent possible. That is, 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 (in case of parallel or distributed builds). It's giving "we don't stop, we keep going" energy πŸ’–
  • --keep-failed / -K β€” specifies that in case of a build failure, the temporary directory (usually in /tmp) in which the build takes place should not be deleted. The path of the build directory is printed as an informational message. Perfect for when you need to go poke at the wreckage πŸ”
  • --fallback β€” whenever Nix attempts to build a derivation for which substitutes are known for each output path, but realising the output paths through the substitutes fails, fall back on building the derivation.

The most common scenario in which this is useful is when we have registered substitutes in order to perform binary distribution from, say, a network repository. If the repository is down, the realisation of the derivation will fail. When this option is specified, Nix will build the derivation instead. Thus, installation from binaries falls back on installation from source. This option is not the default, since it is generally not desirable for a transient failure in obtaining the substitutes to lead to a full build from source (with the related consumption of resources). So like, use it on purpose, not by accident, bestie 😭

  • --readonly-mode β€” when this option is used, no attempt is made to open the Nix database. Most Nix operations do need database access, so those operations will fail. Look but don't touch, basically πŸ™ˆ
  • --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 for which 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). That is, if the evaluator encounters a function with an argument named name, it will call it with value 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 will be called automatically using the value builtins.currentSystem for the system argument. You can override this 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.) She's flexible like that πŸ’…

  • --arg-from-file name path β€” pass the contents of file path as the argument name to Nix functions. File girlies rise πŸŽ€
  • --arg-from-stdin name β€” pass the contents of stdin as the argument name to Nix functions. Piping? We love her.
  • --argstr name value β€” this option is like --arg, only the value is not a Nix expression but a string. So instead of --arg system "i686-linux" (the outer quotes are to keep the shell happy) you can say --argstr system i686-linux. Way less quoting drama, no cap ✨
  • --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. Dotted paths that reach into arrays? She contains multitudes πŸ¦‹

  • --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 to get a shell which contain the built packages ready for use, give your expression to the nix-shell --packages convenience flag instead. Two different shells, two different moods, know the difference bestie πŸ’œ

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

Paths added through -I take precedence over the nix-path configuration setting and the NIX_PATH environment variable. Main character energy, honestly πŸ‘‘

  • --impure β€” allow access to mutable paths and repositories. Living dangerously, I see 🫣
  • --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. Glow-up takes time, babe 😭
Note β€” see man nix.conf for overriding configuration settings with command line flags.

✨ Common Environment Variables ✨

Okay bestie so most Nix commands read the env vars below, and they're lowkey the whole personality of how Nix behaves. Let's go one by one πŸ’…


πŸ› οΈ Configuration environment variables

These ones decide how Nix loads its configuration. Want the whole tea? Hit up the configuration file documentation for full details. 🫢

  • NIX_CONF_DIR β€” overrides the system configuration directory.
  • NIX_USER_CONF_FILES β€” overrides the locations of the user configuration files.
  • NIX_CONFIG β€” hands Nix configuration settings inline, right there in the variable. no file needed, periodt.

πŸ“¦ Store setting environment variables

  • NIX_IGNORE_SYMLINK_STORE β€” okay so normally the Nix store directory (usually /nix/store) is not allowed to have any symlink components anywhere in its path. Why? To stop "impure" builds, that's why. Builders sometimes "canonicalise" paths by resolving every symlink component, so builds on different machines (where /nix/store resolves to different places!) could totally give you different results 😭. Usually that's chill β€” except when you deploy builds to machines where /nix/store resolves differently. If you're 100% sure you're never gonna do that, set NIX_IGNORE_SYMLINK_STORE to 1.
Heads up: if you're symlinking the Nix store so you can park it on another filesystem than the root filesystem, on Linux you're fr better off using bind mount points, like this:
$ mkdir /nix
$ mount -o bind /mnt/otherdisk/nix /nix

Go consult the mount 8 manual page for the deets. 🧚

  • NIX_STORE_DIR β€” overrides where the Nix store lives. On Unix the default is /nix/store, baked in at compile time by the libstore:store-dir build option. On Windows there's no compile-time setting; the default is %PROGRAMDATA%\nix\store, figured out at runtime using the %PROGRAMDATA% Known Folder.

Peep the Store Types chapter β€” every store has a store setting that links back to this as its default. πŸ‘‘

  • NIX_LOG_DIR β€” overrides where the Nix log directory lives. On Unix the default is /nix/var/log/nix, set at compile time by the log-dir build option. On Windows, again, no compile-time setting; the default is %PROGRAMDATA%\nix\log, determined at runtime via the %PROGRAMDATA% Known Folder.

The Local Store, Local Daemon Store, and Experimental SSH Store with filesystem mounted all have per-store settings that override this. (Not me having three stores fight over one setting 😭)

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

Same trio as above β€” Local Store, Local Daemon Store, and Experimental SSH Store with filesystem mounted β€” have per-store settings that override it.

  • NIX_DAEMON_SOCKET_PATH β€” overrides the path to the Unix domain socket used to talk to the Nix daemon. Defaults to daemon-socket/socket inside the state directory (see NIX_STATE_DIR above πŸ’–).

Wanna know how the socket path actually gets resolved? The Local Daemon Store documentation has you covered.

  • TMPDIR β€” use this directory for temporary files. And yes, that includes temporary build directories, which can eat up substantial amounts of disk space, so pick wisely. Default is /tmp.
  • NIX_REMOTE β€” overrides the store setting. simple as that ✨

🧠 Nix language evaluator settings

  • NIX_SHOW_STATS β€” set it to 1 and Nix prints some evaluation statistics, like how many values got allocated. numbers go brrr πŸ“Š
  • NIX_COUNT_CALLS β€” set it to 1 and Nix prints how often functions were called while evaluating Nix expressions. Super useful for profiling your Nix expressions, ngl.
  • GC_INITIAL_HEAP_SIZE β€” if Nix was configured to use the Boehm garbage collector, this sets the initial heap size in bytes. Defaults to 384 MiB. Setting it low cuts memory consumption but makes runtime longer because of all that garbage collection overhead. it's giving tradeoffs 🫠
  • NIX_PATH β€” a colon-separated list of search path entries, used to resolve lookup paths.

This env var overrides the value of the nix-path configuration setting.

You can also extend it with 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. no cap.

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 where those directories go:

  • user configuration directory: NIX_CONFIG_HOME
  • user state directory: NIX_STATE_HOME
  • user cache directory: NIX_CACHE_HOME

When these aren't 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. The new Nix commands (experimental) use the proper directories by default. glow up πŸ’…

When use-xdg-base-directories is enabled, the configuration directory is resolved like this:

  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 energy, different dirs πŸ’–


🎲 Miscellaneous environment variables

  • IN_NIX_SHELL β€” a little indicator that tells you whether the current environment was set up by nix-shell. It can have the values pure or impure. vibe check for your shell, basically 🐚

πŸ“Ž Appendix: OS-specific conventions

Just so you know β€” this info is not Nix-specific, but it got referenced up above, so here it is anyway. 🫢


🍎 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) β€” a 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. slay πŸ’™

The relevant folders are:

  • %APPDATA% β€” per-user roaming application data
  • %LOCALAPPDATA% β€” per-user local application data
  • %PROGRAMDATA% β€” system-wide application data

🌟 Examples

Okay this is the fun part, let's actually do stuff. πŸ’–

Instantiate store derivations from a Nix expression, and then build them using nix-store:

$ nix-instantiate test.nix (instantiate)
/nix/store/cigxbmvy6dzix98dxxh9b6shg7ar5bvs-perl-BerkeleyDB-0.26.drv

$ nix-store --realise $(nix-instantiate test.nix) (build)
...
/nix/store/qhqk4n8ci095g3sdp93x7rgwyh9rdvgk-perl-BerkeleyDB-0.26 (output path)

$ ls -l /nix/store/qhqk4n8ci095g3sdp93x7rgwyh9rdvgk-perl-BerkeleyDB-0.26
dr-xr-xr-x    2 eelco    users        4096 1970-01-01 01:00 lib
...

You can also just hand a Nix expression straight to the command line, main character energy:

$ nix-instantiate --expr 'with import <nixpkgs> { }; hello'
/nix/store/j8s4zyv75a724q38cb0r87rlczaiag4y-hello-2.8.drv

That's literally equivalent to:

$ nix-instantiate '<nixpkgs>' --attr hello

Parsing and evaluating Nix expressions:

$ nix-instantiate --parse --expr '1 + 2'
1 + 2

$ nix-instantiate --eval --expr '1 + 2'
3

$ nix-instantiate --eval --xml --expr '1 + 2'
<?xml version='1.0' encoding='utf-8'?>
<expr>
  <int value="3" />
</expr>

And here's the difference between non-strict and strict evaluation. watch this:

$ nix-instantiate --eval --xml --expr '{ x = {}; }'
<?xml version='1.0' encoding='utf-8'?>
<expr>
  <attrs>
    <attr column="3" line="1" name="x">
      <unevaluated />
    </attr>
  </attrs>
</expr>

$ nix-instantiate --eval --xml --strict --expr '{ x = {}; }'
<?xml version='1.0' encoding='utf-8'?>
<expr>
  <attrs>
    <attr column="3" line="1" name="x">
      <attrs>
      </attrs>
    </attr>
  </attrs>
</expr>

Look at that β€” one says <unevaluated /> and the other actually went in and did the thing. strict vs non-strict, bestie. <rainbow>you get it now, right?</rainbow> πŸ’…βœ¨


aaaand that's the whole page!! πŸŽ€ huge shoutout to nix-instantiate(1) β€” that's the page you just read, the source the whole thing came from, in all its version-and-date glory. go forth and instantiate, you absolute icon πŸ‘‘πŸ¦‹ fr fr, bye bestie!! πŸ’–

πŸ“¦ where this page comes from