Home Explore Blog CI



nixpkgs

12th chunk of `doc/stdenv/stdenv.chapter.md`
0853f96dfab3233c35514a50fb8da3bb75acb70d7fb7901f0000000100000fdd
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).

#### Variables controlling the unpack phase {#variables-controlling-the-unpack-phase}

##### `srcs` / `src` {#var-stdenv-src}

The list of source files or directories to be unpacked or copied. One of these must be set. Note that if you use `srcs`, you should also set `sourceRoot` or `setSourceRoot`.

These should ideally actually be sources and licensed under a FLOSS license.  If you have to use a binary upstream release or package non-free software, make sure you correctly mark your derivation as such in the [`sourceProvenance`](#var-meta-sourceProvenance) and [`license`](#sec-meta-license) fields of the [`meta`](#chap-meta) section.

##### `sourceRoot` {#var-stdenv-sourceRoot}

After unpacking all of `src` and `srcs`, if neither of `sourceRoot` and `setSourceRoot` are set, `unpackPhase` of the generic builder checks that the unpacking produced a single directory and moves the current working directory into it.

If `unpackPhase` produces multiple source directories, you should set `sourceRoot` to the name of the intended directory.
You can also set `sourceRoot = ".";` if you want to control it yourself in a later phase.

For example, if you want your build to start in a sub-directory inside your sources, and you are using `fetchzip`-derived `src` (like `fetchFromGitHub` or similar), you need to set `sourceRoot = "${src.name}/my-sub-directory"`.

##### `setSourceRoot` {#var-stdenv-setSourceRoot}

Alternatively to setting `sourceRoot`, you can set `setSourceRoot` to a shell command to be evaluated by the unpack phase after the sources have been unpacked. This command must set `sourceRoot`.

For example, if you are using `fetchurl` on an archive file that gets unpacked into a single directory the name of which changes between package versions, and you want your build to start in its sub-directory, you need to set `setSourceRoot = "sourceRoot=$(echo */my-sub-directory)";`, or in the case of multiple sources, you could use something more specific, like `setSourceRoot = "sourceRoot=$(echo ${pname}-*/my-sub-directory)";`.

##### `preUnpack` {#var-stdenv-preUnpack}

Hook executed at the start of the unpack phase.

##### `postUnpack` {#var-stdenv-postUnpack}

Hook executed at the end of the unpack phase.

##### `dontUnpack` {#var-stdenv-dontUnpack}

Set to true to skip the unpack phase.

##### `dontMakeSourcesWritable` {#var-stdenv-dontMakeSourcesWritable}

If set to `1`, the unpacked sources are *not* made writable. By default, they are made writable to prevent problems with read-only sources. For example, copied store directories would be read-only without this.

##### `unpackCmd` {#var-stdenv-unpackCmd}

The unpack phase evaluates the string `$unpackCmd` for any unrecognised file. The path to the current source file is contained in the `curSrc` variable.

### The patch phase {#ssec-patch-phase}

The patch phase applies the list of patches defined in the `patches` variable.

#### Variables controlling the patch phase {#variables-controlling-the-patch-phase}

Title: Unpack and Patch Phases in Nix Builds
Summary
This section describes the unpack and patch phases in Nix builds. It elaborates on the unpack phase, covering variables like `srcs`/`src`, `sourceRoot`, `setSourceRoot`, `preUnpack`, `postUnpack`, `dontUnpack`, `dontMakeSourcesWritable`, and `unpackCmd`. It explains how these variables control source unpacking and handling. It also describes the patch phase, which applies patches defined in the `patches` variable.