Demystifying Nix: declaring build inputs
Published: 2026-10-23
This is article is a follow-up to my last one covering how to declare Nix build sources for building software. This post aims to clarify what build inputs are within a Nix derivation (a software build) and the different options you have for declaring them.
In addition to an empty build context, a Nix build “hermetically seals” the build process. Once the build starts, nothing can get in and nothing can get out. The host system, and any of the libraries sitting on it, can’t affect the build. This isolation is how Nix ensures reproducibility, but also begs the question: how does one even build software that relies on system libraries?
The answer is that you need to tell Nix what tools it needs to construct the software and what tools it needs to run the final output. This is where Nix’s concepts of buildInputs and nativeBuildInput come into play. Both relate to declaring dependencies, but both serve distinctly different purposes during the build process.
nativeBuildInputs: The tools for building
The nativeBuildInputs attribute specifies the packages that are required only to perform the build process. They’re tools, compilers, or helper utilities that do not get included in the final package. For example, in a gRPC server, the package being built should not include the protobuf compiler in its final runtime dependencies, but it absolutely needs the compiler while it is being built.
Here is an example for a Rust based gRPC server:
{ pkgs, ...}: let
myProtoRust = pkgs.buildRustPackage {
pname = "my-proto-rust";
version = "1.0";
src = ./.
# This tool is required ONLY to generate code during the build phase
nativeBuildInputs = [ pkgs.protobuf ];
# No external libraries are needed by the final running code itself
buildInputs = [ ];
};
in {...}
The dependencies in nativeBuildInputs are made available within the isolated build environment. Once the build process is complete and the final binary is created, that specific environment, including the nativeBuildInputs, is discarded.
buildInputs: The runtime dependencies
The buildInputs, on the other hand, is an attribute that specifies runtime dependencies needed for the package to function. For example, if you’re building a Python application, and that application relies on the pyGame library, pyGame should be declared in buildInputs. So now, not only does Nix now use the declared library to build your package, but includes it in the final package to ensure its presence for runtime.
{pkgs, ...}: let
myPyGameGame = stdenv.mkDerivation {
pname = "my-pygame-game";
version = "1.0";
src = ./.;
# PyGame is a library needed by the final executable.
# It must be available at runtime.
buildInputs = [
pkgs.python3Packages.python # The interpreter itself
pkgs.python3Packages.pygame # The game library
];
# No specific build tools are needed (like compilers or generators).
nativeBuildInputs = [ ];
};
in {...}
It’s important to note that buildInputs add to the closure (a kind of means of defining the entire scope and build history for the program). When you ask Nix to build something, it creates a unique, hashed path for that entire closure, and paths for build inputs are retained. Depending on the size and complexity of your dependencies’ closures, it can balloon the size of your software; that is the price for maintaining reproducibility.
Final thoughts
By correctly placing a dependency in buildInputs, you ensure the final product is self-contained and runnable. By placing a dependency in nativeBuildInputs, you ensure the build process has the necessary helper tools without cluttering the final package with unnecessary build-time artifacts.
It’s also worth mentioning that there are plenty of language specific functions and libraries out there that already do the work of collecting common build inputs for you! So when writing a new derivation, have a look around to see if there’s one that fits your needs.
Mastering this distinction is a significant step towards understanding how to build with Nix! In the next installment, I’ll be going over Nix build instructions itself and the different phases involved.