NIX-STORE(1) | Command Reference | NIX-STORE(1) |
nix-store - manipulate or query the Nix store
nix-store [--help] [--version]
[{--verbose | -v}...] [--quiet]
[--no-build-output | -Q]
[{--max-jobs | -j} number]
[--cores number]
[--max-silent-time number]
[--timeout number]
[--keep-going | -k]
[--keep-failed | -K] [--fallback]
[--readonly-mode] [-I path]
[--option name value]
[--add-root path] [--indirect] operation
[options...] [arguments...]
The command nix-store performs primitive operations on the Nix store. You generally do not need to run this command manually.
nix-store takes exactly one operation flag which indicates the subcommand to be performed. These are documented below.
This section lists the options that are common to all operations. These options are allowed for every subcommand, though they may not always have an effect.
--add-root path
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).
--indirect
The --indirect flag causes a uniquely named symlink to path to be stored in /nix/var/nix/gcroots/auto/. For instance,
$ nix-store --add-root /home/eelco/bla/result --indirect -r ... $ 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.
--help
--version
--verbose / -v
This option may be specified repeatedly. Currently, the following verbosity levels exist:
0
1
2
3
4
5
--quiet
This option may be specified repeatedly. See the previous verbosity levels list.
--no-build-output / -Q
--max-jobs / -j number
Setting it to 0 disallows building on the local machine, which is useful when you want builds to happen only on remote builders.
--cores
--max-silent-time
--timeout
--keep-going / -k
--keep-failed / -K
--fallback
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).
--no-build-hook
It's useful in cases where the bandwidth between the client and the remote builder is too low. In that case it can take more time to upload the sources to the remote builder and fetch back the result than to do the computation locally.
--readonly-mode
--arg name 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 -i 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 -i pkgname --arg system \"i686-freebsd\". (Note that since the argument is a Nix string literal, you have to escape the quotes.)
--argstr name value
--attr / -A attrPath
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.
--expr / -E
-I path
--option name value
--repair
nix-store {--realise | -r} paths... [--dry-run]
The operation --realise essentially “builds” the specified store paths. Realisation is a somewhat overloaded term:
The output path of each derivation is printed on standard output. (For non-derivations argument, the argument itself is printed.)
The following flags are available:
--dry-run
--ignore-unknown
--check
See also the build-repeat configuration option, which repeats a derivation a number of times and prevents its outputs from being registered as “valid” in the Nix store unless they are identical.
Special exit codes:
100
101
102
104
With the --keep-going flag it's possible for multiple failures to occur, in this case the 1xx status codes are or combined using binary or.
1100100
^^^^
|||`- timeout
||`-- output hash mismatch
|`--- build failure
`---- not deterministic
This operation is typically used to build store derivations produced by nix-instantiate:
$ nix-store -r $(nix-instantiate ./test.nix) /nix/store/31axcgrlbfsxzmfff1gyj1bf62hvkby2-aterm-2.3.1
This is essentially what nix-build does.
To test whether a previously-built derivation is deterministic:
$ nix-build '<nixpkgs>' -A hello --check -K
nix-store --serve [--write]
The operation --serve provides access to the Nix store over stdin and stdout, and is intended to be used as a means of providing Nix store access to a restricted ssh user.
The following flags are available:
--write
To turn a host into a build server, the authorized_keys file can be used to provide build access to a given SSH public key:
$ cat <<EOF >>/root/.ssh/authorized_keys command="nice -n20 nix-store --serve --write" ssh-rsa AAAAB3NzaC1yc2EAAAA... EOF
nix-store --gc [--print-roots | --print-live | --print-dead | --delete] [--max-freed bytes]
Without additional flags, the operation --gc performs a garbage collection on the Nix store. That is, all paths in the Nix store not reachable via file system references from a set of “roots”, are deleted.
The following suboperations may be specified:
--print-roots
--print-live
--print-dead
--delete
By default, all unreachable paths are deleted. The following options control what gets deleted and in what order:
--max-freed bytes
The behaviour of the collector is also influenced by the keep-outputs and keep-derivations variables in the Nix configuration file.
With --delete, the collector prints the total number of freed bytes when it finishes (or when it is interrupted). With --print-dead, it prints the number of bytes that would be freed.
To delete all unreachable paths, just do:
$ nix-store --gc deleting `/nix/store/kq82idx6g0nyzsp2s14gfsc38npai7lf-cairo-1.0.4.tar.gz.drv' ... 8825586 bytes freed (8.42 MiB)
To delete at least 100 MiBs of unreachable paths:
$ nix-store --gc --max-freed $((100 * 1024 * 1024))
nix-store --delete [--ignore-liveness] paths...
The operation --delete deletes the store paths paths from the Nix store, but only if it is safe to do so; that is, when the path is not reachable from a root of the garbage collector. This means that you can only delete paths that would also be deleted by nix-store --gc. Thus, --delete is a more targeted version of --gc.
With the option --ignore-liveness, reachability from the roots is ignored. However, the path still won’t be deleted if there are other paths in the store that refer to it (i.e., depend on it).
$ nix-store --delete /nix/store/zq0h41l75vlb4z45kzgjjmsjxvcv1qk7-mesa-6.4 0 bytes freed (0.00 MiB) error: cannot delete path `/nix/store/zq0h41l75vlb4z45kzgjjmsjxvcv1qk7-mesa-6.4' since it is still alive
nix-store {--query | -q} {--outputs | --requisites | -R | --references | --referrers | --referrers-closure | --deriver | -d | --graph | --tree | --binding name | -b name | --hash | --size | --roots} [--use-output] [-u] [--force-realise] [-f] paths...
The operation --query displays various bits of information about the store paths . The queries are described below. At most one query can be specified. The default query is --outputs.
The paths 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.
--use-output, -u
--force-realise, -f
--outputs
--requisites, -R
This query has one option:
--include-outputs
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.
--references
--referrers
--referrers-closure
--deriver, -d
--graph
--tree
--graphml
--binding name, -b name
--hash
--size
--roots
Print the closure (runtime dependencies) of the svn program in the current user environment:
$ nix-store -qR $(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 -qR $(nix-store -qd $(which svn)) /nix/store/02iizgn86m42q905rddvg4ja975bk2i4-grep-2.5.1.tar.bz2.drv /nix/store/07a2bzxmzwz5hp58nf03pahrv2ygwgs3-gcc-wrapper.sh /nix/store/0ma7c9wsbaxahwwl04gbw3fcd806ski4-glibc-2.3.4.drv ... lots of other paths ...
The difference with the previous example is that we ask the closure of the derivation (-qd), not the closure of the output path that contains svn.
Show the build-time dependencies as a tree:
$ nix-store -q --tree $(nix-store -qd $(which svn)) /nix/store/7i5082kfb6yjbqdbiwdhhza0am2xvh6c-subversion-1.1.4.drv +---/nix/store/d8afh10z72n8l1cr5w42366abiblgn54-builder.sh +---/nix/store/fmzxmpjx2lh849ph0l36snfj9zdibw67-bash-3.0.drv | +---/nix/store/570hmhmx3v57605cqg9yfvvyh0nnb8k8-bash | +---/nix/store/p3srsbd8dx44v2pg6nbnszab5mcwx03v-builder.sh ...
Show all paths that depend on the same OpenSSL library as svn:
$ nix-store -q --referrers $(nix-store -q --binding openssl $(nix-store -qd $(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 -q --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 ...
Note that 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 -q --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 -q --roots $(which svn) /nix/var/nix/profiles/default-81-link /nix/var/nix/profiles/default-82-link /nix/var/nix/profiles/per-user/eelco/profile-97-link
nix-store --add paths...
The operation --add adds the specified paths to the Nix store. It prints the resulting paths in the Nix store on standard output.
$ nix-store --add ./foo.c /nix/store/m7lrha58ph6rcnv109yzx1nk1cj7k7zf-foo.c
nix-store [--recursive] --add-fixed algorithm paths...
The operation --add-fixed adds the specified paths to the Nix store. Unlike --add paths are registered using the specified hashing algorithm, resulting in the same output path as a fixed output derivation. This can be used for sources that are not available from a public url or broke since the download expression was written.
This operation has the following options:
--recursive
$ nix-store --add-fixed sha256 ./hello-2.10.tar.gz /nix/store/3x7dwzq014bblazs7kq20p9hyzz0qh8g-hello-2.10.tar.gz
nix-store --verify [--check-contents] [--repair]
The operation --verify verifies the internal consistency of the Nix database, and the consistency between the Nix database and the Nix store. Any inconsistencies encountered are automatically repaired. Inconsistencies are generally the result of the Nix store or database being modified by non-Nix tools, or of bugs in Nix itself.
This operation has the following options:
--check-contents
--repair
nix-store --verify-path paths...
The operation --verify-path compares the contents of the given store paths to their cryptographic hashes stored in Nix’s database. For every changed path, it prints a warning message. The exit status is 0 if no path has changed, and 1 otherwise.
To verify the integrity of the svn command and all its dependencies:
$ nix-store --verify-path $(nix-store -qR $(which svn))
nix-store --repair-path paths...
The operation --repair-path attempts to “repair” the specified paths by redownloading them using the available substituters. If no substitutes are available, then repair is not possible.
During repair, there is a very small time window during which the old path (if it exists) is moved out of the way and replaced with the new path. If repair is interrupted in between, then the system may be left in a broken state (e.g., if the path contains a critical system component like the GNU C Library).
$ nix-store --verify-path /nix/store/dj7a81wsm1ijwwpkks3725661h3263p5-glibc-2.13 path `/nix/store/dj7a81wsm1ijwwpkks3725661h3263p5-glibc-2.13' was modified!
expected hash `2db57715ae90b7e31ff1f2ecb8c12ec1cc43da920efcbe3b22763f36a1861588',
got `481c5aa5483ebc97c20457bb8bca24deea56550d3985cda0027f67fe54b808e4' $ nix-store --repair-path /nix/store/dj7a81wsm1ijwwpkks3725661h3263p5-glibc-2.13 fetching path `/nix/store/d7a81wsm1ijwwpkks3725661h3263p5-glibc-2.13'... ...
nix-store --dump path
The operation --dump produces a NAR (Nix ARchive) file containing the contents of the file system tree rooted at path. The archive is written to standard output.
A NAR archive is like a TAR or Zip archive, but it contains only the information that Nix considers important. For instance, timestamps are elided because all files in the Nix store have their timestamp set to 0 anyway. Likewise, all permissions are left out except for the execute bit, because all files in the Nix store have 644 or 755 permission.
Also, a NAR archive is canonical, meaning that “equal” paths always produce the same NAR archive. For instance, directory entries are always sorted so that the actual on-disk order doesn’t influence the result. This means that the cryptographic hash of a NAR dump of a path is usable as a fingerprint of the contents of the path. Indeed, the hashes of store paths stored in Nix’s database (see nix-store -q --hash) are SHA-256 hashes of the NAR dump of each store path.
NAR archives support filenames of unlimited length and 64-bit file sizes. They can contain regular files, directories, and symbolic links, but not other types of files (such as device nodes).
A Nix archive can be unpacked using nix-store --restore.
nix-store --restore path
The operation --restore unpacks a NAR archive to path, which must not already exist. The archive is read from standard input.
nix-store --export paths...
The operation --export writes a serialisation of the specified store paths to standard output in a format that can be imported into another Nix store with nix-store --import. This is like nix-store --dump, except that the NAR archive produced by that command doesn’t contain the necessary meta-information to allow it to be imported into another Nix store (namely, the set of references of the path).
This command does not produce a closure of the specified paths, so if a store path references other store paths that are missing in the target Nix store, the import will fail. To copy a whole closure, do something like:
$ nix-store --export $(nix-store -qR paths) > out
To import the whole closure again, run:
$ nix-store --import < out
nix-store --import
The operation --import reads a serialisation of a set of store paths produced by nix-store --export from standard input and adds those store paths to the Nix store. Paths that already exist in the Nix store are ignored. If a path refers to another path that doesn’t exist in the Nix store, the import fails.
nix-store --optimise
The operation --optimise reduces Nix store disk space usage by finding identical files in the store and hard-linking them to each other. It typically reduces the size of the store by something like 25-35%. Only regular files and symlinks are hard-linked in this manner. Files are considered identical when they have the same NAR archive serialisation: that is, regular files must have the same contents and permission (executable or non-executable), and symlinks must have the same contents.
After completion, or when the command is interrupted, a report on the achieved savings is printed on standard error.
Use -vv or -vvv to get some progress indication.
$ nix-store --optimise hashing files in `/nix/store/qhqx7l2f1kmwihc9bnxs7rc159hsxnf3-gcc-4.1.1' ... 541838819 bytes (516.74 MiB) freed by hard-linking 54143 files; there are 114486 files with equal contents out of 215894 files in total
nix-store {--read-log | -l} paths...
The operation --read-log prints the build log of the specified store paths on standard output. The build log is whatever the builder of a derivation wrote to standard output and standard error. If a store path is not a derivation, the deriver of the store path is used.
Build logs are kept in /nix/var/log/nix/drvs. However, there is no guarantee that a build log is available for any particular store path. For instance, if the path was downloaded as a pre-built binary through a substitute, then the log is unavailable.
$ nix-store -l $(which ktorrent) building /nix/store/dhc73pvzpnzxhdgpimsd9sw39di66ph1-ktorrent-2.2.1 unpacking sources unpacking source archive /nix/store/p8n1jpqs27mgkjw07pb5269717nzf5f8-ktorrent-2.2.1.tar.gz ktorrent-2.2.1/ ktorrent-2.2.1/NEWS ...
nix-store --dump-db [paths...]
The operation --dump-db writes a dump of the Nix database to standard output. It can be loaded into an empty Nix store using --load-db. This is useful for making backups and when migrating to different database schemas.
By default, --dump-db will dump the entire Nix database. When one or more store paths is passed, only the subset of the Nix database for those store paths is dumped. As with --export, the user is responsible for passing all the store paths for a closure. See --export for an example.
nix-store --load-db
The operation --load-db reads a dump of the Nix database created by --dump-db from standard input and loads it into the Nix database.
nix-store --print-env drvpath
The operation --print-env prints out the environment of a derivation in a format that can be evaluated by a shell. The command line arguments of the builder are placed in the variable _args.
$ 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'
nix-store --generate-binary-cache-key key-name secret-key-file public-key-file
This command generates an Ed25519 key pair[3] that can be used to create a signed binary cache. It takes three mandatory parameters:
IN_NIX_SHELL
NIX_PATH
/home/eelco/Dev:/etc/nixos
will cause Nix to look for paths relative to /home/eelco/Dev and /etc/nixos, in that order. It is also possible to match paths against a prefix. For example, the value
nixpkgs=/home/eelco/Dev/nixpkgs-branch:/etc/nixos
will cause Nix to search for <nixpkgs/path> in /home/eelco/Dev/nixpkgs-branch/path and /etc/nixos/nixpkgs/path.
If a path in the Nix search path starts with http:// or https://, it is interpreted as the URL of a tarball that will be downloaded and unpacked to a temporary location. The tarball must consist of a single top-level directory. For example, setting NIX_PATH to
nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/nixos-15.09.tar.gz
tells Nix to download the latest revision in the Nixpkgs/NixOS 15.09 channel.
A following shorthand can be used to refer to the official channels:
nixpkgs=channel:nixos-15.09
The search path can be extended using the -I option, which takes precedence over NIX_PATH.
NIX_IGNORE_SYMLINK_STORE
Note that if you’re symlinking the Nix store so that you can put it on another file system than the root file system, on Linux you’re better off using bind mount points, e.g.,
$ mkdir /nix $ mount -o bind /mnt/otherdisk/nix /nix
Consult the mount(8) manual page for details.
NIX_STORE_DIR
NIX_DATA_DIR
NIX_LOG_DIR
NIX_STATE_DIR
NIX_CONF_DIR
TMPDIR
NIX_REMOTE
NIX_SHOW_STATS
NIX_COUNT_CALLS
GC_INITIAL_HEAP_SIZE
Eelco Dolstra
Copyright © 2004-2018 Eelco Dolstra
12/12/2020 | Nix 2.3.7 |