Home Explore Blog CI



nixpkgs

11th chunk of `doc/stdenv/stdenv.chapter.md`
32839a20259e67d59f6da56b885de73c76431c659e16110e0000000100000feb
Each phase can be overridden in its entirety either by setting the environment variable `namePhase` to a string containing some shell commands to be executed, or by redefining the shell function `namePhase`. The former is convenient to override a phase from the derivation, while the latter is convenient from a build script. However, typically one only wants to *add* some commands to a phase, e.g. by defining `postInstall` or `preFixup`, as skipping some of the default actions may have unexpected consequences. The default script for each phase is defined in the file `pkgs/stdenv/generic/setup.sh`.

When overriding a phase, for example `installPhase`, it is important to start with `runHook preInstall` and end it with `runHook postInstall`, otherwise `preInstall` and `postInstall` will not be run. Even if you don't use them directly, it is good practice to do so anyways for downstream users who would want to add a `postInstall` by overriding your derivation.

While inside an interactive `nix-shell`, if you wanted to run all phases in the order they would be run in an actual build, you can invoke `genericBuild` yourself.

### Controlling phases {#ssec-controlling-phases}

There are a number of variables that control what phases are executed and in what order:

#### Variables affecting phase control {#variables-affecting-phase-control}

##### `phases` {#var-stdenv-phases}

Specifies the phases. You can change the order in which phases are executed, or add new phases, by setting this variable. If it’s not set, the default value is used, which is `$prePhases unpackPhase patchPhase $preConfigurePhases configurePhase $preBuildPhases buildPhase checkPhase $preInstallPhases installPhase fixupPhase installCheckPhase $preDistPhases distPhase $postPhases`.

The elements of `phases` must not contain spaces. If `phases` is specified as a Nix Language attribute, it should be specified as lists instead of strings. The same rules apply to the `*Phases` variables.

It is discouraged to set this variable, as it is easy to miss some important functionality hidden in some of the less obviously needed phases (like `fixupPhase` which patches the shebang of scripts).
Usually, if you just want to add a few phases, it’s more convenient to set one of the `*Phases` variables below.

##### `prePhases` {#var-stdenv-prePhases}

Additional phases executed before any of the default phases.

##### `preConfigurePhases` {#var-stdenv-preConfigurePhases}

Additional phases executed just before the configure phase.

##### `preBuildPhases` {#var-stdenv-preBuildPhases}

Additional phases executed just before the build phase.

##### `preInstallPhases` {#var-stdenv-preInstallPhases}

Additional phases executed just before the install phase.

##### `preFixupPhases` {#var-stdenv-preFixupPhases}

Additional phases executed just before the fixup phase.

##### `preDistPhases` {#var-stdenv-preDistPhases}

Additional phases executed just before the distribution phase.

##### `postPhases` {#var-stdenv-postPhases}

Additional phases executed after any of the default phases.

### The unpack phase {#ssec-unpack-phase}

The unpack phase is responsible for unpacking the source code of the package. The default implementation of `unpackPhase` unpacks the source files listed in the `src` environment variable to the current directory. It supports the following files by default:

#### Tar files {#tar-files}

These can optionally be compressed using `gzip` (`.tar.gz`, `.tgz` or `.tar.Z`), `bzip2` (`.tar.bz2`, `.tbz2` or `.tbz`) or `xz` (`.tar.xz`, `.tar.lzma` or `.txz`).

#### Zip files {#zip-files}

Zip files are unpacked using `unzip`. However, `unzip` is not in the standard environment, so you should add it to `nativeBuildInputs` yourself.

#### Directories in the Nix store {#directories-in-the-nix-store}

These are copied to the current directory. The hash part of the file name is stripped, e.g. `/nix/store/1wydxgby13cz...-my-sources` would be copied to `my-sources`.

Additional file types can be supported by setting the `unpackCmd` variable (see below).

Title: Controlling and Customizing Build Phases
Summary
This section elaborates on controlling and customizing build phases in Nix derivations. It explains how to override phases, the significance of `runHook preInstall` and `runHook postInstall`, and how to use `genericBuild` in `nix-shell`. It details variables like `phases`, `prePhases`, `postPhases` etc. to control phase execution order. It discourages modifying the main `phases` variable and recommends using `*Phases` variables for adding phases. Finally, it focuses on the `unpackPhase`, detailing how it unpacks source code from `src` and lists supported file types like tar, zip and directories in Nix store.