NIX-ENV-QUERY(1)

๐Ÿ‘‘ nix-env --query โ€” the "so what do I even have installed??" command

ok so this page is from the General Commands Manual (section 1), and bestie, it's the nix-env --query one. It's giving "let me peek inside my Nix world and tell you everything." ๐Ÿฆ‹


โœจ Name โœจ

nix-env --query โ€” display information about packages ๐Ÿ’…


๐Ÿ“œ Synopsis ๐Ÿ“œ

nix-env {--query | -q} namesโ€ฆ [--installed | --available | -a]
[{--status | -s}] [{--attr-path | -P}] [--no-name] [{--compare-versions
| -c}] [--system] [--drv-path] [--out-path] [--description] [--meta]
[--xml] [--json] [{--prebuilt-only | -b}] [{--attr | -A} attribute-path]

๐Ÿ’– Description ๐Ÿ’–

The query operation is literally just "tell me things." It displays information about either:

  • the store paths that are installed in the current generation of the active profile (that's --installed), or
  • the derivations that are available for installation in the active Nix expression (that's --available).

so either "what's already in my world" or "what could be in my world." vibe check โœ…

It only prints info about derivations whose symbolic name matches one of names. no cap. ๐Ÿ™ˆ

And the derivations come out sorted by their name attributes. organized queen behavior. ๐Ÿ‘‘


๐ŸŒธ Source selection ๐ŸŒธ

These flags pick which pile of stuff the query is looking at. ๐Ÿ’…

  • --installed โ€” the query operates on the store paths that are installed in the current generation of the active profile. this is the default, bestie, so if you don't say anything, this is what you get. ๐Ÿซถ
  • --available, -a โ€” the query operates on the derivations that are available in the active Nix expression. i.e. the stuff you could install. โœจ

๐Ÿ”ฅ Queries ๐Ÿ”ฅ

These flags choose what info gets shown about the derivations you selected. You can stack multiple of them โ€” and when you do, the info appears in the order given here. ๐Ÿ“‹

heads up: the name of the derivation is shown unless you pass --no-name. so it's on by default, like a little name tag ๐ŸŽ€
  • --xml โ€” prints the result in an XML representation, suitable for automatic processing by other tools. The root element is called items, which contains an item element for each available or installed derivation. The fields discussed below are all stored in attributes of the item elements. robot-friendly, love that for us ๐Ÿค–
  • --json โ€” prints the result in a JSON representation suitable for automatic processing by other tools. same energy, different syntax. ๐ŸŒˆ
  • --prebuilt-only, -b โ€” shows only derivations for which a substitute is registered, i.e. there's a pre-built binary available that can be downloaded in lieu of building the derivation. So basically this shows all packages that probably can be installed quickly. fast girl summer. ๐Ÿ”ฅ
  • --status, -s โ€” prints the status of the derivation. The status is three characters:
    • the first is I or -, indicating whether the derivation is currently installed in the current generation of the active profile. (by definition this is the case for --installed, but not for --available)
    • the second is P or -, indicating whether the derivation is present on the system. This tells you whether installing an available derivation will require the derivation to be built.
    • the third is S or -, indicating whether a substitute is available for the derivation.

three little letters carrying so much lore. ๐Ÿ˜ญ

  • --attr-path, -P โ€” prints the attribute path of the derivation, which you can use to unambiguously select it with the --attr option available in commands that install derivations, like nix-env --install.
โš ๏ธ this option only works together with --available. don't try it alone, it's not that girl ๐Ÿ’€
  • --no-name โ€” suppresses printing of the name attribute of each derivation. quiet mode for the name tag ๐ŸŽ€
  • --compare-versions, -c โ€” compares installed versions to available versions, or vice versa (if --available is given). This is so useful for quickly seeing whether upgrades for installed packages are available in a Nix expression. A column gets added, with this meaning:
    • < version โ€” a newer version of the package is available or installed. โฌ†๏ธ
    • = version โ€” at most the same version of the package is available or installed. โžก๏ธ
    • > version โ€” only older versions of the package are available or installed. โฌ‡๏ธ
    • - ? โ€” no version of the package is available or installed. ๐Ÿšซ
  • --system โ€” prints the system attribute of the derivation. ๐Ÿ–ฅ๏ธ
  • --drv-path โ€” prints the [store path] to the [store derivation]. (the glossary links being referenced are: https://nix.dev/manual/nix/2.35/glossary.html#gloss-store-path and https://nix.dev/manual/nix/2.35/glossary.html#gloss-derivation) ๐Ÿฆ‹
  • --out-path โ€” prints the output path of the derivation. ๐Ÿ“ฆ
  • --description โ€” prints a short (one-line) description of the derivation, if available. The description is taken from the meta.description attribute of the derivation. a lil blurb bestie ๐Ÿซถ
  • --meta โ€” prints all of the meta-attributes of the derivation.
โš ๏ธ this option is only available with --xml or --json. it does not ride solo. ๐Ÿ’…

โญ Options โญ

These options are allowed for all nix-env operations, but they may not always have an effect. (iykyk, sometimes they just vibe in the background.) ๐Ÿ‘‘

  • --file, -f path โ€” specifies the Nix expression (called below the active Nix expression) used by the --install, --upgrade, and --query --available operations to obtain derivations. The default is ~/.nix-defexpr.

If the argument starts with http:// or https://, it's interpreted as the URL of a tarball that will be downloaded and unpacked to a temporary location. The tarball must include a single top-level directory containing at least a file named default.nix. ๐Ÿ“ฆโœจ

  • --profile, -p path โ€” specifies the profile to be used by those operations that operate on a profile (called below the active profile). A profile is a sequence of user environments called generations, one of which is the current generation. generational trauma but make it Nix ๐Ÿ’€
  • --dry-run โ€” for the --install, --upgrade, --uninstall, --switch-generation, --delete-generations and --rollback operations, this flag causes nix-env to print what would be done if this flag had not been specified, without actually doing it. window shopping, no purchases ๐Ÿ›๏ธ

--dry-run also prints out which paths will be substituted (i.e., downloaded) and which paths will be built from source (because no substitute is available). so helpful honestly ๐Ÿ˜ญ

  • --system-filter system โ€” by default, operations such as --query --available show derivations matching any platform. This option lets you use derivations for the specified platform system. filter it, bestie ๐ŸŽ€

ok that's part 1 and there's more coming, don't go anywhere ๐Ÿ’–

๐ŸŒˆ COMMON OPTIONS ๐ŸŒˆ

okay bestie, buckle UP, because basically every Nix command out there speaks this same little dialect of flags ๐Ÿ’–. Learn these once and you're fluent forever. No cap.

  • --help โ€” prints a lil summary of the command syntax and then dips (exits). Your friendship bracelet of flags ๐Ÿซถ
  • --version โ€” prints the Nix version number on standard output and peaces out. That's it, that's the whole thing โœจ
  • --verbose / -v โ€” turns the volume UP on diagnostic messages printed on standard error. Here's the tea: for each Nix operation, whatever goes to standard output is well-defined, and any diagnostic stuff goes to standard error, never standard output. Clean separation, obsessed.

You can stack this flag repeatedly ๐Ÿ”. The verbosity levels, ranked:

  • 0 โ€œErrors onlyโ€ โ€” only prints messages explaining why the Nix invocation failed ๐Ÿ˜ญ
  • 1 โ€œInformationalโ€ โ€” useful messages about what Nix is doing. This is the default.
  • 2 โ€œTalkativeโ€ โ€” more informational messages ๐Ÿ—ฃ๏ธ
  • 3 โ€œChattyโ€ โ€” even MORE informational messages ๐Ÿ’ฌ
  • 4 โ€œDebugโ€ โ€” debug info ๐Ÿ›
  • 5 โ€œVomitโ€ โ€” vast amounts of debug information ๐Ÿคฎ (yes that's really the name, iconic behaviour fr)
  • --quiet โ€” turns the volume DOWN on diagnostic messages on standard error. It's the inverse option to -v / --verbose. Also stackable repeatedly ๐Ÿ”. Same verbosity levels list as above, just read it backwards bestie ๐Ÿ’œ
  • *--log-format *format โ€” swaps up how the log output looks. 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 you dare hardcode against it, bestie ๐Ÿ’€
  • bar โ€” only shows a progress bar during the builds.
  • bar-with-logs โ€” shows the raw logs, with the progress bar chillin' at the bottom ๐ŸŽ€
  • --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 flag suppresses that behaviour. But heads up: the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix. Always. Periodt ๐Ÿ’…
  • *--max-jobs / -j *number โ€” sets the max number of build jobs Nix will run in parallel. Say auto and it'll use the number of CPUs in the system ๐Ÿ–ฅ๏ธ. 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 โ€” super handy when you want builds to happen only on remote builders, no local nonsense โœจ

  • --cores โ€” sets the value of the NIX_BUILD_CORES environment variable in the invocation of builders. Builders can use that variable at their own discretion to control 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. The value 0 means the builder should use all available CPU cores in the system. Go big or go home fr ๐Ÿ”ฅ
  • --max-silent-time โ€” sets the max number of seconds a builder can go without producing ANY data on standard output or standard error ๐Ÿ˜ถ. Default is specified by the max-silent-time configuration setting. 0 means no time-out. Silence is golden but also suspicious, bestie ๐Ÿ™ˆ
  • --timeout โ€” sets the max number of seconds a builder can run. Default is specified by the timeout configuration setting. 0 means no timeout. Let it cook ๐Ÿณ
  • --keep-going / -k โ€” keeps going in case of failed builds, to the greatest extent possible. Meaning: 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). Rude, honestly ๐Ÿ˜ญ
  • --keep-failed / -K โ€” says that in case of a build failure, the temporary directory (usually in /tmp) where the build happened should not be deleted. The path of the build directory gets printed as an informational message. Perfect for when you need to go snooping through the crime scene ๐Ÿ•ต๏ธโ™€๏ธ
  • --fallback โ€” whenever Nix tries to build a derivation for which substitutes are known for each output path, but realising the output paths through the substitutes fails, it falls back on building the derivation instead ๐Ÿช‚

The most common scenario where this slays: you've got registered substitutes so you can do binary distribution from, like, a network repository ๐ŸŒ. If the repo's down, realising the derivation fails. With this option, Nix just builds the derivation instead. So installation from binaries falls back on installation from source. It's not the default because it's generally not desirable for a transient failure in obtaining substitutes to lead to a full build from source (and all the resource consumption that comes with it ๐Ÿ˜ฎโ€๐Ÿ’จ)

  • --readonly-mode โ€” when you use this, no attempt is made to open the Nix database. Most Nix operations genuinely need database access, so those operations will fail. Look but don't touch energy ๐Ÿšซ
  • *--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 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 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\". (Note that since the argument is a Nix string literal, you have to escape the quotes, bestie โ€” don't forget, or the shell will come for you ๐Ÿ”ช)

  • *--arg-from-file *name path โ€” passes the contents of 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 ๐Ÿ“ฅ (piping it straight in, no thoughts, just vibes)
  • *--argstr *name value โ€” this one's 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 just say --argstr system i686-linux. So much less escaping, so much more slay ๐ŸŽ€
  • *--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 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, don't come for 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 a shell which contains the built packages ready for use, give your expression to the nix-shell --packages convenience flag instead. Different vibe, different result โœจ

  • *-I / --include *path โ€” adds an entry to the list of search paths used to resolve lookup paths. May be given multiple times ๐Ÿ”

Paths added through -I take precedence over the nix-path configuration setting and the NIX_PATH environment variable. The way they get priorityโ€ฆ queen behaviour ๐Ÿ‘‘

  • --impure โ€” allows access to mutable paths and repositories. Living dangerously, love that for you ๐ŸŒช๏ธ
  • *--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 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 โš ๏ธ
Note: See man nix.conf for overriding configuration settings with command line flags.

๐Ÿฆ‹ ENVIRONMENT VARIABLES ๐Ÿฆ‹

  • NIX_PROFILE โ€” the location of the Nix profile. Defaults to the target of the symlink ~/.nix-profile, if it exists, or /nix/var/nix/profiles/default otherwise. Your home base, your main character address ๐Ÿ ๐Ÿ’–

๐Ÿ’– Common Environment Variables ๐Ÿ’–

okay bestie, so most Nix commands are out here reading these environment variables like it's their whole personality (because it literally is). let's get into it ๐Ÿซถ

โš™๏ธ Configuration environment variables

These ones decide how Nix loads its config. full deets live in the configuration file documentation, but here's the vibe:

  • NIX_CONF_DIR โ€” overrides the system configuration directory. yes bestie, the whole system config dir.
  • NIX_USER_CONF_FILES โ€” overrides the user configuration file locations.
  • NIX_CONFIG โ€” gives you configuration settings inline. no extra files needed, just vibes โœจ

๐Ÿฌ 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. why? to prevent โ€œimpureโ€ builds. builders sometimes โ€œcanonicaliseโ€ paths by resolving all symlink components. so builds on different machines (where /nix/store resolves to different locations) could yield different results. that's generally not a problem, except when builds are deployed to machines where /nix/store resolves differently. if you're sure you're not gonna do that, you can set NIX_IGNORE_SYMLINK_STORE to 1.
ngl if you're symlinking the Nix store so you can put it on another file system than the root file system, on Linux you're way better off using bind mount points, like:
$ mkdir /nix
$ mount -o bind /mnt/otherdisk/nix /nix

go consult the mount 8 manual page for the details, bestie.

  • 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. cute little numbers for the girlies who love metrics.
  • NIX_COUNT_CALLS โ€” if set to 1, Nix will print how often functions were called during Nix expression evaluation. useful for profiling your Nix expressions, bestie.
  • 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. trade-offs, amirite?
  • 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]{#user-conf-dir}: [NIX_CONFIG_HOME]{#env-NIXCONFIGHOME}
  • user state directory: [NIX_STATE_HOME]{#env-NIXSTATEHOME}
  • user cache directory: [NIX_CACHE_HOME]{#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. periodt. ๐Ÿ’…

๐ŸŽฒ 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 info is not Nix-specific, but it's referenced above, so here we are. don't skip it, bestie.

๐Ÿง 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]{#env-XDGCONFIGHOME} (default ~/.config)
  • [XDG_STATE_HOME]{#env-XDGSTATEHOME} (default ~/.local/state)
  • [XDG_CACHE_HOME]{#env-XDGCACHEHOME} (default ~/.cache)
  • [XDG_CONFIG_DIRS]{#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%]{#env-APPDATA} โ€” per-user roaming application data
  • [%LOCALAPPDATA%]{#env-LOCALAPPDATA} โ€” per-user local application data
  • [%PROGRAMDATA%]{#env-PROGRAMDATA} โ€” system-wide application data

๐ŸŒธ Examples ๐ŸŒธ

okay now the fun part, let's see this in action:

To show installed packages:

$ nix-env --query
bison-1.875c
docbook-xml-4.2
firefox-1.0.4
MPlayer-1.0pre7
ORBit2-2.8.3
โ€ฆ

To show available packages:

$ nix-env --query --available
firefox-1.0.7
GConf-2.4.0.1
MPlayer-1.0pre7
ORBit2-2.8.3
โ€ฆ

To show the status of available packages:

$ nix-env --query --available --status
-P- firefox-1.0.7   (not installed but present)
--S GConf-2.4.0.1   (not present, but there is a substitute for fast installation)
--S MPlayer-1.0pre3 (i.e., this is not the installed MPlayer, even though the version is the same!)
IP- ORBit2-2.8.3    (installed and by definition present)
โ€ฆ

To show available packages in the Nix expression foo.nix:

$ nix-env --file ./foo.nix --query --available
foo-1.2.3

To compare installed versions to what's available:

$ nix-env --query --compare-versions
...
acrobat-reader-7.0 - ?      (package is not available at all)
autoconf-2.59      = 2.59   (same version)
firefox-1.0.4      < 1.0.7  (a more recent version is available)
...

To show all packages with โ€œzipโ€ in the name:

$ nix-env --query --available '.*zip.*'
bzip2-1.0.6
gzip-1.6
zip-3.0
โ€ฆ

To show all packages with โ€œfirefoxโ€ or โ€œchromiumโ€ in the name:

$ nix-env --query --available '.*(firefox|chromium).*'
chromium-37.0.2062.94
chromium-beta-38.0.2125.24
firefox-32.0.3
firefox-with-plugins-13.0.1
โ€ฆ

To show all packages in the latest revision of the Nixpkgs repository:

$ nix-env --file https://github.com/NixOS/nixpkgs/archive/master.tar.gz --query --available

That's the whole page bestie! ๐Ÿ’… source/version: nix-env --query(1). the date is stamped in the footer too, go give it a peek. you ate that. periodt. ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from