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

5.5 Exercise: extend GNU hello

Lesson 34 of 68 · 28:25

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Overriding mkDerivation Phases
  2. 9:20 Creating and Applying a Patch
  3. 17:20 Fixing Build Inputs and Test Failures
  4. 22:40 Inspecting the Standard Builder Internals
Transcript

Overriding mkDerivation Phases

0:00 Okay, so after we went through, like, what's inside the standard environment, how to extend it with parameters, where I might have been overwhelming with all the different kinds of input parameters, and then also the different phases, how to enable them, how some phases are disabled by default, some aren't, and how to override those phases, let's put this into practice and play with this, because that's the only way to really see it work. So what we're going to do now is we take the hello derivation that we did earlier, and I invite you to do this yourself before, like, in steps before we show how to do it.

0:39 And what we do is we write the configure phase, the build phase, the check phase, and the install phase ourselves to see, like, how they would have to be defined. And when that works, and please note that we need to enable the check phase before we write it, otherwise it would be just ignored, right? When that works, I want that we use the patches feature. You can, for example, already see in the Nix packages manual, on nixos.org/manual/nixpkgs, there's also documentation about the phases where we see in what order they are typically run, then there's some explanation how they work, and there's even, for example, for the patch phase, we can see that the standard patch phase obeys variables like patches, and that is a list of patch files.

1:32 So we can actually create a patch file that would be applied to our recipe. So here we have a few hints how to create a patch, don't worry, we will unfold this step by step, but we can practically say, okay, we have our hello checkout in one folder and then some copy of that in the second folder where we change the test, sorry, the code files a bit. And then when we run the diff tool on that, we can store the difference in the patch file, and then we can put that into a patches equals list of with this patch file in there.

The full transcript comes with the lesson.

About this lesson

Time to get your hands dirty. In this exercise you rebuild GNU hello, but now you write the configure, build, check, and install phases yourself, then change the program's greeting with your own patch file. Things break in exactly the ways real packages break, and by the end you have followed the trail all the way into the shell script where every stdenv default is defined.

In this video

  • Writing all four core phases by hand, and discovering why the install step suddenly fails once you do
  • Creating a patch with diff, handing it to the patches parameter, and watching it apply before configure runs
  • A tool that goes missing mid-build, and deciding whether it belongs in buildInputs or nativeBuildInputs
  • Tests that start failing for a reason you caused yourself, and the two ways out
  • Following nix derivation show to the roughly 2000 line setup.sh where the unpack, patch, configure, and build logic actually lives

After this lesson

You can take over any phase of a build and patch upstream source code without forking it, and when a default phase behaves strangely you know exactly which file to open to see why.

Links: