NIX-BUILD(1) | Command Reference | NIX-BUILD(1) |
nix-build - build a Nix expression
nix-build [--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]
[--arg name value]
[--argstr name value]
[{--attr | -A} attrPath]
[--no-out-link] [--dry-run]
[{--out-link | -o} outlink]
paths...
The nix-build command builds the derivations described by the Nix expressions in paths. If the build succeeds, it places a symlink to the result in the current directory. The symlink is called result. If there are multiple Nix expressions, or the Nix expressions evaluate to multiple derivations, multiple sequentially numbered symlinks are created (result, result-2, and so on).
If no paths are specified, then nix-build will use default.nix in the current directory, if it exists.
If an element of paths 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 include a single top-level directory containing at least a file named default.nix.
nix-build is essentially a wrapper around nix-instantiate (to translate a high-level Nix expression to a low-level store derivation) and nix-store --realise (to build the store derivation).
The result of the build is automatically registered as a root of the Nix garbage collector. This root disappears automatically when the result symlink is deleted or renamed. So don’t rename the symlink.
All options not listed here are passed to nix-store --realise, except for --arg and --attr / -A which are passed to nix-instantiate.
--no-out-link
--dry-run
--out-link / -o outlink
The following common options are supported:
--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-build '<nixpkgs>' -A firefox store derivation is /nix/store/qybprl8sz2lc...-firefox-1.5.0.7.drv /nix/store/d18hyl92g30l...-firefox-1.5.0.7 $ ls -l result lrwxrwxrwx ... result -> /nix/store/d18hyl92g30l...-firefox-1.5.0.7 $ ls ./result/bin/ firefox firefox-config
If a derivation has multiple outputs, nix-build will build the default (first) output. You can also build all outputs:
$ nix-build '<nixpkgs>' -A openssl.all
This will create a symlink for each output named result-outputname. The suffix is omitted if the output name is out. So if openssl has outputs out, bin and man, nix-build will create symlinks result, result-bin and result-man. It’s also possible to build a specific output:
$ nix-build '<nixpkgs>' -A openssl.man
This will create a symlink result-man.
Build a Nix expression given on the command line:
$ nix-build -E 'with import <nixpkgs> { }; runCommand "foo" { } "echo bar > $out"' $ cat ./result bar
Build the GNU Hello package from the latest revision of the master branch of Nixpkgs:
$ nix-build https://github.com/NixOS/nixpkgs/archive/master.tar.gz -A hello
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 |