Home Explore Blog Models CI



nixpkgs

13th chunk of `doc/stdenv/stdenv.chapter.md`
3d60203d20c86146faad7e62936e5b1920a8e6f9d487eba80000000100000fb2
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}

##### `dontPatch` {#var-stdenv-dontPatch}

Set to true to skip the patch phase.

##### `patches` {#var-stdenv-patches}

The list of patches. They must be in the format accepted by the `patch` command, and may optionally be compressed using `gzip` (`.gz`), `bzip2` (`.bz2`) or `xz` (`.xz`).

##### `patchFlags` {#var-stdenv-patchFlags}

Flags to be passed to `patch`. If not set, the argument `-p1` is used, which causes the leading directory component to be stripped from the file names in each patch.

##### `prePatch` {#var-stdenv-prePatch}

Title: Nix `stdenv` Unpack and Patch Phase Customization
Summary
This text covers customization of the Nix `stdenv` `unpackPhase` and introduces the `patchPhase`. For `unpackPhase`, `unzip` must be added to `nativeBuildInputs` for zip files, and Nix store directories are copied. Key variables include `srcs`/`src` for specifying source files (with a note on FLOSS licensing and metadata). `sourceRoot` sets the working directory post-unpack, useful for multiple or nested source directories, while `setSourceRoot` offers a dynamic shell command alternative. Other controls are `preUnpack` and `postUnpack` hooks, `dontUnpack` to skip the phase, `dontMakeSourcesWritable` for source writability, and `unpackCmd` for custom file types. The `patchPhase` is introduced with variables like `dontPatch` to skip it, `patches` for listing (potentially compressed) patch files, and `patchFlags` for `patch` command arguments (defaulting to `-p1`). `prePatch` is mentioned as a hook.