Demystifying Nix: understanding packaging phases
Published: 2026-10-30
When it comes to executing the actual build process, Nix has a clever way of orchestrating how it all gets put together. Under the hood, the build script separates the build into very distinct and modular “phases”. Every phase serves a unique function in the process.
So, why does this matter? This modularity allows developers, when writing derivations, to fine-tune specific parts of the build without creating cascading effects onto the whole process!
This is the next installment in my series covering topics around demystifying how to build software with Nix the language. I’ll be covering the different phases associated when calling pkgs.stdenv.mkDerivation and how to control and take advantage of the tooling Nix provides.
What are phases and what do they do?
Before we can manipulate the phases, we need to understand what they are and how each one fits into the build execution. Each has its own concern, and Nix runs each one in this specific order:
- Unpack: Extracts the raw source code for the package
- Patch: Applies any critical patches before compilation begins
- Configure: Prepares the source tree for compilation
- Build: Executes the build commands to compile the code
- Check: Executes tests to ensure the software was compiled correctly
- Install: Collects build outputs into a `/nix/store` path
- Fixup: Performs any Nix-specific postprocessing on output files
This default collection of phases, run in this order, provides developers with a sane starting point for many build processes.
For deeper dive into understand the lower-level commands that get run during each phase, take a look at the nixpkgs source code: pkgs/stdenv/generic/setup.sh.
Controlling the phases
The simplest way to change or control what happens in each phase is to include a Makefile for your project. pkgs.stdenv.mkDerivation expects a standard UNIX build environment, and following that standard requires the least amount of developer intervention. Using make, Nix will simply invoke make <PHASE_NAME> for any of the phases.
While that is the simplest way and should work for most projects, to meet any nonstandard needs, Nix still provides ways to customize both what phases get run and in what order.
The standard phases and order can be expressed in the following phases attribute:
{
# Default nix package build phases in the order run
# It should evaluate to a list of strings
phases =
prePhases
++ ["unpackPhase" "patchPhase"]
++ preConfigurePhases
++ ["configurePhase"]
++ preBuildPhases
++ ["buildPhase" "checkPhase"]
++ preInstallPhases
++ ["installPhase" "fixupPhase" "installCheckPhase"]
++ preDistPhases
++ ["distPhase"]
++ postPhases;
}
So what are all the extra phases such as prePhases doing in the expression? And why are they variables instead of strings?
While Nix allows for lower-level control of the phases attribute, the Nix documentation generally discourages doing this. This is done to ensure that any standard processes (like the fixup phase rewriting shebangs in scripts) doesn’t unintentionally get skipped.
To give developers a similar amount of control with more confidence, Nix encourages defining your own phases and binding them to those preset variables. Furthermore, it’s also a common pattern to simply override a default phase with your own definition by declaring an attribute sharing the same name as the target phase.
For example, this is how I pre-fetch content for this blog during the buildPhase:
# Outer perSystem function is elided
{lib, pkgs, self', ...}: let
cms = "https://my-cms.tld";
collections = ["posts?limit=0"];
globals = ["about" "hero" "footer" "nav-bar"];
contentKey = "markdown";
# Using this higher order function, I can pass multiple permutations
# of the same flag to the build command
mkFlagFunc = flag: (s: ''--${flag} "${s}" '');
in {
packages.content = pkgs.stdenv.mkDerivation {
# Other attributes have been elided
# You can use internal packages as native build inputs!
nativeBuildInputs = [self'.packages.prefetch];
dontUnpack = true;
buildPhase = ''
runHook preBuild
mkdir -p $out
prefetch --output $out \
--api ${cms} \
${lib.concatMapStrings (mkFlagFunc "collections") collections} \
${lib.concatMapStrings (mkFlagFunc "globals") globals} \
--content-key ${contentKey}
runHook postBuild
'';
};
}
A keen eye might have noticed that I included a dontUnpack attribute in my code example. In addition to being able to define and override phases, pkgs.stdenv.mkDerivation also includes attributes that let developers skip phases.
Any phase can be skipped by declaring a dont${PHASE_NAME} attribute for your derivation, i.e. dontConfigure, dontUnpack, dontBuild. The only outlier is the doCheck attribute. By default, the checkPhase is skipped (doCheck = false), so the syntax for that phase is a little different.
With these tools available, needing to manually manage the phases attribute should be minimal, but give developers ample control over the build execution!
Final thoughts
I’ll be honest and say that, in practice, directly manipulating any of the phases isn’t necessary. This is probably why it took me a long time to understand what was going on under the hood.
As I mentioned earlier, pkgs.stdenv.mkDerivation by default, is able to handle most codebases that follow the standard UNIX build patterns when given the correct build inputs. To add to that, Nix also has several libraries tailored for common programming languages (as I mentioned in the previous article). Not only do they handle common build inputs for you, but also tailor each of the phases to meet that specific language’s standard environment.
I think, however, understanding the build execution phases is key to understanding Nix. When things do go awry, and the libraries don’t do exactly what’s needed, knowledge of the build phases can empower a developer to confidently makes the changes they do need!
In my next post, I’ll dive into another fundamental Nix concept: fixed-output derivations. We’ll explore why they are vital for guaranteeing reproducibility, why they can be hard to manage, and how to make handling them more ergonomic.