LinuxCommandLibrary

nix-build.2

Build Nix packages

TLDR

Build a Nix expression

$ nix-build '<nixpkgs>' [[-A|--attr]] [firefox]
copy

Build a sandboxed Nix expression (on non-NixOS)
$ nix-build '<nixpkgs>' [[-A|--attr]] [firefox] --option sandbox true
copy

SYNOPSIS

nix-build [options] [path]

PARAMETERS

--help
    Show help message and exit.

-A
    Build the attribute of the top-level Nix expression in path.

-E
    Build the Nix expression .

-f
    Use as a Nix expression (defaults to ./default.nix).

-o
    Symlink the output to (defaults to ./result).

--dry-run
    Show what would be built without actually building anything.

--no-out-link
    Don't create the ./result symlink.

--keep-going
    Continue building even if some builds fail.

--arg
    Pass an argument to the Nix expression.

--argstr
    Pass a string argument to the Nix expression.

--attr
    Select an attribute from the top-level Nix expression.

--check
    Run the tests defined in the derivation.

--option
    Set a Nix configuration option.

--show-trace
    Show a stack trace in case of errors.

DESCRIPTION

nix-build is a fundamental command in the Nix package manager. It builds Nix expressions and derivations, resulting in the realization of packages, software, or other artifacts defined within the Nix expression language. It's the workhorse for translating Nix code into tangible outputs. It can build from a Nix expression directly, a Nix file, or a Nix derivation. The command figures out the dependencies of the expression, fetches them, and then executes the build steps. By default, the result is placed in the ./result directory, but this can be modified.
nix-build is crucial for achieving reproducible builds, meaning that the same input always leads to the same output. This is achieved through strict dependency management, where all dependencies are explicitly specified and tracked, ensuring a consistent build environment.
nix-build also supports various options to control the build process such as overriding attributes of derivations, specifying system architecture, and enabling sandboxing for enhanced security. The command is a vital part of Nix's core functionality, making it essential for packaging, software deployment, and system configuration.

CAVEATS

Building large derivations can consume a significant amount of disk space and processing power. Make sure you have enough resources. Sandboxing may require appropriate kernel features and configurations.

UNDERSTANDING DERIVATIONS

A Nix derivation is a special type of Nix expression that describes how to build a package. It specifies inputs, build commands, and outputs. nix-build takes these derivations as input and executes the build process.

REPRODUCIBILITY

The key benefit of nix-build is reproducibility. Because all dependencies are explicitly tracked and managed, the same build instructions should always produce the same output, regardless of the environment in which it's built.

COMMON USAGE

A common use case is building a package defined in a default.nix file. In this case, you would simply run nix-build in the directory containing the file. The output will be placed in the ./result directory.

HISTORY

nix-build has been a core component of Nix since its inception. Its development has tracked the evolution of Nix itself, with features added to improve reproducibility, sandboxing, and dependency management. It's central to Nix's goal of providing a robust and reproducible package management system. Over time improvements have focused on performance optimizations, enhanced security features, and better integration with other Nix tools.

SEE ALSO

nix-shell(1), nix-env(1), nix(1)

Copied to clipboard