Nix 101 Course / Section 5: Packaging real software with stdenv.mkDerivation
Premium

5.4 Nix build phases: enable, disable, and override them

Lesson 33 of 68 · 9:58

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Overview of customizing mkDerivation phases
  2. 1:24 Standard build phases and what each one does
  3. 4:46 Enabling the check phase in hello
  4. 8:55 Wrap-up and next exercise
Transcript

Overview of customizing mkDerivation phases

0:00 OK, so after we had a look how Standard and mkDerivation practically works without any parameters, when everything works just automatically, and then we learned what is already part of the standard environment, now we're going to look at how to customize it. Because sometimes a package has a standard unpack phase and configure phase. In the build phase, you need to do something slightly different than normal, and then the rest of the phases work automatically again. We can now compose those phases wherever we need to, and leave the phases alone doing the defaults where they want to or can, kind of.

0:40 So we're going to go through all the phases that exist now, then we learn how to enable, disable and override them, and have that a big exercise, where you will see all of this in action. But let's first stay at the whiteboard and go through the theory. So as we have seen earlier, there's an unpack phase, in which of course something is, for example, downloaded, unpacked, and then we kind of go into the source folder. That should be like revealed for any package.

1:10 The patch phase, we didn't mention that yet, but in next packages there are many packages which are patched before they are built, and you will most likely sometimes do that too, to change the behavior of your package or to inject the security patch, whatever.

The full transcript comes with the lesson.

About this lesson

Every stdenv.mkDerivation build runs through fixed phases: unpack, patch, configure, build, check, install, fixup, and two more. This lesson maps out all nine Nix build phases, which seven matter, which two you can forget, and how each one can be enabled, disabled, overridden, or extended. It also clears up a small mystery: why your tests silently never ran.

In this video

  • All nine build phases in order, and what each one is responsible for
  • The switches that turn phases off, and the one phase that is off by default
  • Why doCheck breaks the naming pattern of the other flags, and a memory trick so you never mix it up
  • Live demo: the same package built with and without its test suite, and the one line that makes the difference
  • Hooks like postInstall: keeping the default behavior and running your own snippet around it

After this lesson

You can read the phase overrides in any nixpkgs package fluently, and you know exactly which switch to flip when a build does too much or too little.