NIX-STORE-QUERY(1)

okay bestie, this is nix-store --query(1) πŸ’… β€” the man page that lets you grill store paths for information. She's giving detective, she's giving database, she's giving absolute icon. No cap.

🌸 NAME 🌸

nix-store --query β€” display information about store paths. That's it, that's the whole name, and she ate. ✨

πŸ¦‹ SYNOPSIS πŸ¦‹

the exact lineup, bestie:

nix-store {--query | -q} {--outputs | --requisites | -R | --references |
--referrers | --referrers-closure | --deriver | -d | --valid-derivers |
--graph | --tree | --binding name | -b name | --hash | --size | --roots}
[--use-output] [-u] [--force-realise] [-f] paths…

πŸ’– DESCRIPTION πŸ’–

The --query operation displays various bits of information about the store paths. The queries are described below. You can specify at most one query at a time. The default query is --outputs. πŸ’…

The paths may also be symlinks from outside of the Nix store, to the Nix store. In that case, the query is applied to the target of the symlink. So the symlink is just the messenger. 🫢

πŸŽ€ COMMON QUERY OPTIONS πŸŽ€

  • --use-output / -u β€” For each argument to the query that is a store derivation, apply the query to the output path of the derivation instead. ✨
  • --force-realise / -f β€” Realise each argument to the query first (see nix-store --realise). Make it real before you ask it questions. πŸ’–

πŸ”₯ QUERIES πŸ”₯

Pick one, bestie β€” at most one, remember? Here's the menu:

  • --outputs β€” Prints out the output paths of the store derivations paths. These are the paths that will be produced when the derivation is built. πŸ“¦
  • --references β€” Prints the set of references of the store paths paths, that is, their immediate dependencies. (For all dependencies, use --requisites.) πŸ”—
  • --requisites / -R β€” Prints out the set of requisites (better known as the closure) of the store path paths.

This query has one option:

  • --include-outputs β€” Also include the existing output paths of store derivations, and their closures.

This query can be used to implement various kinds of deployment. A source deployment is obtained by distributing the closure of a store derivation. A binary deployment is obtained by distributing the closure of an output path. A cache deployment (combined source/binary deployment, including binaries of build-time-only dependencies) is obtained by distributing the closure of a store derivation and specifying the option --include-outputs. πŸš€

  • --referrers β€” Prints the set of referrers of the store paths paths, that is, the store paths currently existing in the Nix store that refer to one of paths. Note that contrary to the references, the set of referrers is not constant; it can change as store paths are added or removed. She's fluid, she's shifting, she's not staying put. πŸ’€
  • --referrers-closure β€” Prints the closure of the set of store paths paths under the referrers relation; that is, all store paths that directly or indirectly refer to one of paths. These are all the paths currently in the Nix store that are dependent on paths. πŸŒ€
  • --deriver / -d β€” Prints the deriver that was used to build the store paths paths. If the path has no deriver (e.g., if it is a source file), or if the deriver is not known (e.g., in the case of a binary-only deployment), the string unknown-deriver is printed. The returned deriver is not guaranteed to exist in the local store, for example when paths were substituted from a binary cache. Use --valid-derivers instead to obtain valid paths only. 😭
  • --valid-derivers β€” Prints a set of derivation files (.drv) which are supposed to produce said paths when realized. Might print nothing, for example for source paths or paths substituted from a binary cache. She can ghost you, and that's valid. πŸ™ˆ
  • --graph β€” Prints the references graph of the store paths paths in the format of the dot tool of AT&T's Graphviz package. This can be used to visualise dependency graphs. To obtain a build-time dependency graph, apply this to a store derivation. To obtain a runtime dependency graph, apply it to an output path. πŸ“Š
  • --tree β€” Prints the references graph of the store paths paths as a nested ASCII tree. References are ordered by descending closure size; this tends to flatten the tree, making it more readable. The query only recurses into a store path when it is first encountered; this prevents a blowup of the tree representation of the graph. Very demure, very mindful. 🌳
  • --graphml β€” Prints the references graph of the store paths paths in the GraphML file format. This can be used to visualise dependency graphs. To obtain a build-time dependency graph, apply this to a store derivation. To obtain a runtime dependency graph, apply it to an output path. πŸ—ΊοΈ
  • --binding name / -b name β€” Prints the value of the attribute name (i.e., environment variable) of the store derivations paths. It is an error for a derivation to not have the specified attribute. So don't ask for something that isn't there, bestie. 😭
  • --hash β€” Prints the SHA-256 hash of the contents of the store paths paths (that is, the hash of the output of nix-store --dump on the given paths). Since the hash is stored in the Nix database, this is a fast operation. Speedy queen. ⚑
  • --size β€” Prints the size in bytes of the contents of the store paths paths β€” to be precise, the size of the output of nix-store --dump on the given paths. Note that the actual disk space required by the store paths may be higher, especially on filesystems with large cluster sizes. She's precise, not wrong. πŸ’…
  • --roots β€” Prints the garbage collector roots that point, directly or indirectly, at the store paths paths. 🌱

πŸ‘‘ OPTIONS πŸ‘‘

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 most Nix commands accept all of these, bestie. Like, the whole family. Here's the lineup 🫢

  • --help β€” prints out a summary of the command syntax and then dips. That's the whole thing ✨
  • --version β€” prints out the Nix version number on standard output and exits. slay.
  • --verbose / -v β€” cranks up the level of verbosity of diagnostic messages printed on standard error. Important vibe note: for each Nix operation, the info printed on standard output is well-defined; any diagnostic info goes on standard error, never on standard output. no cap.

This option may be specified repeatedly. Currently, the following verbosity levels exist, queen πŸ‘‘

  • 0 β€œErrors only” β€” only print messages explaining why the Nix invocation failed. awkward but necessary 😭
  • 1 β€œInformational” β€” print useful messages about what Nix is doing. This is the default, bestie.
  • 2 β€œTalkative” β€” print more informational messages.
  • 3 β€œChatty” β€” print even more informational messages.
  • 4 β€œDebug” β€” print debug information.
  • 5 β€œVomit” β€” print vast amounts of debug information. it's giving unhinged and we respect it πŸ’€
  • --quiet β€” decreases the level of verbosity of diagnostic messages printed on standard error. It's the inverse option to -v / --verbose. May be specified repeatedly. See the verbosity levels list above ⬆️
  • --log-format format β€” lets you swap out the output of the log format. format has to be one of these:
    • 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.
  • bar β€” only display a progress bar during the builds.
  • bar-with-logs β€” display the raw logs, with the progress bar at the bottom. the best of both worlds tbh 🌈
  • --no-build-output / -Q β€” by default, output written by builders to standard output and standard error is 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. like, always always. πŸ’…
  • --max-jobs / -j number β€” sets the maximum number of build jobs Nix will perform in parallel to number. Say 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 useful when you want builds to happen only on remote builders. go touch grass locally, build in the cloud ✨

  • --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 the builder should use all available CPU cores in the system. all of them. every single one πŸ”₯
  • --max-silent-time β€” sets the maximum number of seconds 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 here πŸ™ˆ
  • --timeout β€” sets the maximum number of seconds a builder can run. The default is specified by the timeout configuration setting. 0 means no timeout.
  • --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). the way that it just keeps going… πŸ’ͺ
  • --keep-failed / -K β€” specifies that in case of a build failure, the temporary directory (usually in /tmp) where the build takes place should not be deleted. The path of the build directory is printed as an informational message. free souvenirs πŸŽ€
  • --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 where 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). lowkey a resource-hungry main character move 😭

  • --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, don't touch, and also most stuff just breaks πŸ’€
  • --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.) quotes are the drama, bestie 🎭

  • --arg-from-file name path β€” pass the contents of file path as the argument name to Nix functions. πŸ“
  • --arg-from-stdin name β€” pass the contents of stdin as the argument name to Nix functions.
  • --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. so much less escaping 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, it's zero-indexed, yes, that means 3 is the fourth one, don't @ me 🧚

  • --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 contains the built packages ready for use, give your expression to the nix-shell --packages convenience flag instead. πŸ‘‘

  • -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. stacked. πŸ’…

  • --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.conf(5)). 😌
  • --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. heal the store, but at what cost 😭
πŸ“ Note: see man nix.conf for overriding configuration settings with command line flags.

πŸ› οΈ Common Environment Variables πŸ› οΈ

Like, most Nix commands read these. They're the whole vibe-setting layer under everything, bestie. πŸ’–

βš™οΈ Configuration environment variables

These three control how Nix loads up its config. The configuration file docs have the full tea, but here's the rundown:

  • NIX_CONF_DIR β€” swaps out the system configuration directory for whatever you point it at.
  • NIX_USER_CONF_FILES β€” swaps out where the user configuration files live.
  • NIX_CONFIG β€” lets you hand Nix configuration settings inline, right there, no file needed. Iconic.

🏬 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 in it at all. Why? To prevent "impure" builds. See, builders sometimes "canonicalise" paths by resolving every single symlink component, so builds on different machines (where /nix/store resolves to different places) could give you different results. Usually that's fine β€” except when you deploy builds to machines where /nix/store resolves somewhere else. If you're sure that's not your situation, set NIX_IGNORE_SYMLINK_STORE to 1.
Heads up: if you're symlinking the Nix store so you can park it on a different filesystem than root, on Linux you're way better off using bind mount points, like:
$ mkdir /nix
$ mount -o bind /mnt/otherdisk/nix /nix

Go read the mount 8 man page for the details, bestie.

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

Peep the Store Types chapter, kay? Every store has a store setting that links back to this one 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, again, no compile-time setting; 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. πŸ’…

  • 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 that defaults to /nix/var. On Windows there's no compile-time setting; the default is %PROGRAMDATA%\nix\state, worked out at runtime using the %PROGRAMDATA% Known Folder.

Same deal as above: Local Store, Local Daemon Store, and Experimental SSH Store with filesystem mounted each have per-store settings that override it.

  • NIX_DAEMON_SOCKET_PATH β€” overrides the path to the Unix domain socket that talks to the Nix daemon. Defaults to daemon-socket/socket inside the state directory (check NIX_STATE_DIR πŸ‘†).

The Local Daemon Store documentation explains exactly how the socket path gets resolved, if you wanna go deep.

  • TMPDIR β€” use this directory for temporary files. It specifically includes temporary build directories, and those can eat up a substantial amount of disk space, so watch it. Default is /tmp.
  • NIX_REMOTE β€” overrides the store setting.

🧠 Nix language evaluator settings

  • NIX_SHOW_STATS β€” if you set it to 1, Nix prints out some evaluation statistics, like how many values got allocated. ✨
  • NIX_COUNT_CALLS β€” set it to 1 and Nix tells you how often functions were called during Nix expression evaluation. Super useful for profiling your Nix expressions, no cap.
  • GC_INITIAL_HEAP_SIZE β€” if Nix was configured to use the Boehm garbage collector, this sets the initial heap size in bytes. It defaults to 384 MiB. Set it low and you use less memory, but runtime goes up because of all the garbage collection overhead. Trade-offs, bestie. 😭
  • 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.

And you can 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.

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 does per-user configuration, state, and cache directories, and these env vars override where those live:

  • user configuration directory (docs anchor it as #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 you don't set those, the defaults depend on your 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

Now, for backwards compatibility, the legacy Nix commands (think nix-env, nix-channel) use dotfiles in $HOME instead of these directories β€” unless use-xdg-base-directories is enabled. The new Nix commands (the experimental ones) use the proper directories by default. Slay.

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

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

Same logic for the state and cache directories. πŸ¦‹

🎲 Miscellaneous environment variables

  • IN_NIX_SHELL β€” an indicator that tells you whether the current environment was set up by nix-shell. It can have the values pure or impure.

🌍 Appendix: OS-specific conventions 🌍

None of this is Nix-specific, it's just referenced up above, but we're including it because completeness is the whole brand. πŸ’œ

🐧 Unix: XDG Base Directories

The XDG Base Directory Specification defines the standard spots for user-specific configuration, state, and cache files on Unix systems.

Here's what gets 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. It's a colon-separated list of additional configuration base directories, and they get searched after XDG_CONFIG_HOME.

πŸͺŸ Windows: Known Folders

On Windows, Known Folders are the standard locations for application data. The relevant ones 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 πŸ”₯

Okay this is the fun part, bestie. Look at what this thing can do. πŸ‘‘

Print the closure (the runtime dependencies) of the svn program in the current user environment:

$ nix-store --query --requisites $(which svn)
/nix/store/5mbglq5ldqld8sj57273aljwkfvj22mc-subversion-1.1.4
/nix/store/9lz9yc6zgmc0vlqmn2ipcpkjlmbi51vv-glibc-2.3.4
...

Print the build-time dependencies of svn:

$ nix-store --query --requisites $(nix-store --query --deriver $(which svn))
/nix/store/y6qa66l9h0pw161crnlk6y16rdrcljx4-grep-2.5.1.tar.bz2.drv
/nix/store/z716h753s97jhnzvfank2srqbljswpgm-gcc-wrapper.sh
/nix/store/f39x0q73rjdyvzm93y9wrkfr6x39lb7f-glibc-2.3.4.drv
... lots of other paths ...

The difference from the last example is that here we're asking for the closure of the derivation (-qd), not the closure of the output path that holds svn. Subtle but so real.

Show the build-time dependencies as a tree:

$ nix-store --query --tree $(nix-store --query --deriver $(which svn))
/nix/store/7i5082kfb6yjbqdbiwdhhza0am2xvh6c-subversion-1.1.4.drv
+---/nix/store/vxnmkc8l8d2ijjha4xwhkfgx9vvc3q4c-builder.sh
+---/nix/store/rn9776dy82n5qrgz7xbcl1iw4vfkcrkk-bash-3.0.drv
|   +---/nix/store/x9j20hz6bln1crzn55qifk0bbsm8v5ac-bash
|   +---/nix/store/ajnn1mcm45wjvn0rlc22gvx2cwhjnazx-builder.sh
...

Show all paths that depend on the same OpenSSL library as svn:

$ nix-store --query --referrers $(nix-store --query --binding openssl $(nix-store --query --deriver $(which svn)))
/nix/store/23ny9l9wixx21632y2wi4p585qhva1q8-sylpheed-1.0.0
/nix/store/5mbglq5ldqld8sj57273aljwkfvj22mc-subversion-1.1.4
/nix/store/dpmvp969yhdqs7lm2r1a3gng7pyq6vy4-subversion-1.1.3
/nix/store/l51240xqsgg8a7yrbqdx1rfzyv6l26fx-lynx-2.8.5

Show all paths that directly or indirectly depend on the Glibc (C library) used by svn:

$ nix-store --query --referrers-closure $(ldd $(which svn) | grep /libc.so | awk '{print $3}')
/nix/store/034a6h4vpz9kds5r6kzb9lhh81mscw43-libgnomeprintui-2.8.2
/nix/store/15l3yi0d45prm7a82pcrknxdh6nzmxza-gawk-3.1.4
...

Quick note: ldd is a command that prints out the dynamic libraries used by an ELF executable. 🧚

Make a picture of the runtime dependency graph of the current user environment:

$ nix-store --query --graph ~/.nix-profile | dot -Tps > graph.ps
$ gv graph.ps

Show every garbage collector root that points to a store path that depends on svn:

$ nix-store --query --roots $(which svn)
/nix/var/nix/profiles/default-81-link
/nix/var/nix/profiles/default-82-link
/home/eelco/.local/state/nix/profiles/profile-97-link

And that's a wrap on the whole thing, bestie!! πŸŽ€βœ¨ You made it to the very end of nix-store --query(1) β€” the footer's got the page name right there, with the Nix version and the date hanging out next to it. Go forth and query some closures. Main character energy only. πŸ’…πŸ’–πŸŒˆ

πŸ“¦ where this page comes from