Nixpkgs phases: which shell language + how to overwrite phases?

The Nixpkgs-documentation says, that Each phase can be overridden ... to a string containing some shell commands. (NixOS - Nixpkgs 21.05 manual)


  • Which shell language? sh, bash, …?
  • How can I set variables?
    E.g. read -a array <<< $srcs or ${var% *} results in an error.
  • Which commands can I use? E.g. echo, mkdir, cd, mv, chmod all do not work; but that may be caused by the next:
  • How can I overwrite a phase?
    When I e.g. use unpackPhase = ''mkdir test'', then run nix-shell test.nix, and then enter unpackPhase, the original unpackPhase-function is called, and my definition is completely ignored…


  • it is bash
  • every attribute in your derivation will map to an env variable
  • some programs are part of the stdenv but if you need more, you simply add the corresponding packages to nativeBuildInputs
  • hum strange I don’t see any other way to override a phase, but I never used nix-shell to debug this way

Regarding the overriding: You’re doing it right. If you want to execute the overridden phase manually, you have to eval $unpackPhase. See this wiki page and rfc 32 (which proposes to change this behavior).


Since you seem new I will suggest looking into trivial builders, writeScriptBin where you write text into a script and then your install phase would be, say, ${pkgs.python3}/bin/python3 ${script_you_just_wrote}


  • I hope RFC32 gets merged; this would improve usability and probably prevented my post. :slight_smile:
  • I guess “bash is used for the phases” should be added to the documentation.
    (I guess I would have to open a bug-report fort this.)

Remaining question: Is there a way to make the following work in the phases-“scripts”?

  • a=${src% *} – currently triggers error: syntax error, unexpected $undefined, expecting '}',
  • read -a array <<< $srcs; ${array[0]} – currently triggers error: undefined variable 'array'

Both works when manually entered into the nix-shell, but not when included into the nixpkgs-file.

Yes, you have to escape the ${} with two single-quotes: ''${}. Normally ${var} inserts the nix variable var.

See Introduction

1 Like