nix-build(1)
๐ nix-build โ the manual, but make it make sense
okay bestie, welcome to nix-build(1) from the General Commands Manual ๐ this is the one that builds a Nix expression and honestly? iconic behavior.
โจ NAME โจ
nix-build โ build a Nix expression ๐ ๏ธ
๐ธ SYNOPSIS ๐ธ
nix-build [pathsโฆ] [--arg name value] [--argstr name value] [{--attr |
-A} attrPath] [--no-out-link] [--dry-run] [{--out-link | -o} outlink]
๐ฆ DISAMBIGUATION ๐ฆ
so like. real talk. this man page is about the command nix-build. it is not the same thing as nix build, okay? they are two different girls. do not confuse them. ๐
if you want the docs for the other one, run nix build --help or go read man nix3-build. periodt.
๐ DESCRIPTION ๐
nix-build builds the derivations described by the Nix expressions in paths. when the build succeeds it drops a symlink to the result right there in the current directory, and that symlink is called result. and if you've got multiple Nix expressions, or your Nix expressions evaluate to multiple derivations, you get a whole little lineup of sequentially numbered symlinks โ result, result-2, and so on. it's giving collect them all. ๐
if you don't pass any paths at all, then nix-build will use default.nix in the current directory, if it exists. (if it doesn't existโฆ girl. it needs it.) lowkey convenient when it's there tho.
and if an element of paths starts with http:// or https://, it gets interpreted as the URL of a tarball that will be downloaded and unpacked to a temporary location. the tarball has to include a single top-level directory containing at least a file named default.nix. no cap, that's the rule. ๐ฆ
okay so under the hood, nix-build is essentially a wrapper around nix-instantiate (which translates the high-level Nix expression into a low-level store derivation) and nix-store --realise (which actually builds that store derivation). so it's not doing magic, it's just doing delegation, which is honestly main character energy. ๐
โ ๏ธ Warning โ ๏ธ the result of the build gets automatically registered as a root of the Nix garbage collector. that root disappears automatically when the result symlink is deleted or renamed. so don't rename the symlink, bestie. i'm so serious. i know you want to. don't. ๐ญ
๐ OPTIONS ๐
every option that isn't listed right here gets passed to nix-store --realise, except for --arg and --attr / -A, which get passed to nix-instantiate. iykyk. ๐ซ
--no-out-linkโ do not create a symlink to the output path. heads up though: as a result the output does not become a root of the garbage collector, and so it might get deleted bynix-store --gc. so likeโฆ it's giving "fragile." handle with care. ๐--dry-runโ show what store paths would be built or downloaded. a little preview, a little sneak peek, no commitment. snoop but make it responsible. ๐--out-link,-ooutlink โ change the name of the symlink to the output path created fromresultto outlink. rename the baby, basically. ๐ผ
โญ SPECIAL EXIT CODES FOR BUILD FAILURE โญ
1xx status codes are used when requested builds failed. these are the ones in use, bestie, write them down:
100Generic build failure โ the builder process returned with a non-zero exit code.101Build timeout โ the build was aborted because it did not complete within the specified timeout. she ran out of time. ๐102Hash mismatch โ the build output was rejected because it does not match theoutputHashattribute of the derivation.104Not deterministic โ the build succeeded in check mode but the resulting output is not binary reproducible. it did the thing but it did it differently, which is a whole vibe. ๐
and with the --keep-going flag it's possible for multiple failures to occur. in that case the 1xx status codes are or combined using bitwise OR. and here's the lil diagram, verbatim, because she's a work of art ๐จ:
0b1100100
^^^^
|||`- timeout
||`-- output hash mismatch
|`--- build failure
`---- not deterministic
okay that's part one, keep reading, we're not done yet ๐ซถ
โจ COMMON OPTIONS โจ
Most Nix commands accept the following command-line options, bestie โ and yes, they're all important, no skipping ๐
--helpโ prints out a summary of the command syntax and exits. Simple, iconic ๐--versionโ prints out the Nix version number on standard output and exits. slay ๐--verbose/-vโ increases the level of verbosity of diagnostic messages printed on standard error. For each Nix operation, the information printed on standard output is well-defined; any diagnostic information is printed on standard error, never on standard output.
This option may be specified repeatedly. Currently, the following verbosity levels exist:
0โErrors onlyโ โ only print messages explaining why the Nix invocation failed.1โInformationalโ โ print useful messages about what Nix is doing. This is the default.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.
--quietโ decreases the level of verbosity of diagnostic messages printed on standard error. This is the inverse option to-v/--verbose.
This option may be specified repeatedly. See the previous verbosity levels list.
--log-format formatโ this option can be used to change the output of the log format, with format being one of:rawโ this is the raw format, as outputted bynix-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.
barโ only display a progress bar during the builds.bar-with-logsโ display the raw logs, with the progress bar at the bottom.
--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 this behaviour. Note that the builderโs standard output and error are always written to a log file inprefix/nix/var/log/nix.--max-jobs/-j numberโ sets the maximum number of build jobs that Nix will perform in parallel to the specified number. Specifyautoto use the number of CPUs in the system. The default is specified by themax-jobsconfiguration setting, which itself defaults to1. 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.
--coresโ sets the value of theNIX_BUILD_CORESenvironment 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 attributeenableParallelBuildingis set totrue, the builder passes the-jNflag to GNU Make. It defaults to the value of thecoresconfiguration setting, if set, or1otherwise. The value0means that the builder should use all available CPU cores in the system.--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 themax-silent-timeconfiguration setting.0means no time-out.--timeoutโ sets the maximum number of seconds that a builder can run. The default is specified by thetimeoutconfiguration setting.0means 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).--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.--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).
--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.--arg name valueโ this option is accepted bynix-env,nix-instantiate,nix-shellandnix-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.
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.)
--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.--attr/-A attrPathโ select an attribute from the top-level Nix expression being evaluated. (nix-env,nix-instantiate,nix-buildandnix-shellonly.) The attribute path attrPath is a sequence of attribute names separated by dots. For instance, given a top-level Nix expression e, the attribute pathxorg.xorgserverwould cause the expressione.xorg.xorgserverto be used. Seenix-env --installfor 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.
--eval-store store-urlโ the URL to the Nix store to use for evaluation, i.e. where to store derivations (.drvfiles) 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-buildandnix-shellonly.)
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.
-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.
--impureโ allow access to mutable paths and repositories.--option name valueโ set the Nix configuration option name to value. This overrides settings in the Nix configuration file (seenix.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 undernix-store --repair-path.
๐ Note
See man nix.conf for overriding configuration settings with command line flags.
โจ Common Environment Variables โจ
okay bestie, most Nix commands read these environment variables, so let's get into it ๐
๐ ๏ธ Configuration environment variables
These ones decide how Nix loads its configuration. Full deets live in the configuration file documentation, but here's the tea:
NIX_CONF_DIRโ overrides the system configuration directory.NIX_USER_CONF_FILESโ overrides the user configuration file locations.NIX_CONFIGโ provides configuration settings inline. very main character of it.
๐๏ธ 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, and thus builds on different machines (with/nix/storeresolving to different locations) could yield different results. this is generally not a problem, except when builds are deployed to machines where/nix/storeresolves differently. if you are sure that you're not going to do that, you can setNIX_IGNORE_SYMLINK_STOREto1.
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โ overrides the location of the Nix store. on Unix, the default is/nix/store, set at compile time by thelibstore:store-dirbuild 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 thelog-dirbuild 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, wherelocalstatediris 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 too, fr fr.
NIX_DAEMON_SOCKET_PATHโ overrides the path to the Unix domain socket used to communicate with the Nix daemon. defaults todaemon-socket/socketwithin the state directory (seeNIX_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 to1, Nix will print some evaluation statistics, such as the number of values allocated. numbers go brrr โจNIX_COUNT_CALLSโ if set to1, Nix will print how often functions were called during Nix expression evaluation. super useful for profiling your Nix expressions ๐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 to384 MiB. setting it to a low value reduces memory consumption, but will increase runtime due to the overhead of garbage collection โป๏ธ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:
NIX_CONFIG_HOME๐ - user state directory:
NIX_STATE_HOME๐ฆ - user cache directory:
NIX_CACHE_HOME๐ธ
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:
$NIX_CONFIG_HOME, if it is defined- Otherwise, the platform default (e.g.
$XDG_CONFIG_HOME/nixon Unix)
likewise for the state and cache directories. consistency icon behavior โจ
๐ฒ Miscellaneous environment variables
IN_NIX_SHELLโ indicator that tells if the current environment was set up bynix-shell. it can have the valuespureorimpure.
๐ Appendix: OS-specific conventions
this information is not Nix-specific, but is referenced above. just here for the lore, yk? ๐ง
๐ง 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) โ colon-separated list of additional configuration base directories, searched afterXDG_CONFIG_HOME
๐ช Windows: Known Folders
on Windows, Known Folders provide standard locations for application data on Windows.
the relevant folders are:
%APPDATA%โ per-user roaming application data%LOCALAPPDATA%โ per-user local application data%PROGRAMDATA%โ system-wide application data
๐ซ Examples
okay bestie, time for the fun part, actual commands ๐
$ nix-build '<nixpkgs>' --attr 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>' --attr 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>' --attr openssl.man
this will create a symlink result-man โจ
build a Nix expression given on the command line:
$ nix-build --expr '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 --attr hello
and that's a wrap on the whole page, bestie ๐ซถ you just read the whole nix-build(1) manual page and honestly? iconic behavior, no cap ๐ this chapter was served straight from the nix-build(1) manual itself, so if you ever need to cite it, that's your source. go build something slay now ๐
โจ๐ธ
๐ฆ where this page comes from
- fedora 44: nix-legacy 2.35.3-2.fc44 ยท LGPL-2.1-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms