Nix 101 Course / Section 6: Composing nixpkgs: callPackage, override, project layout
Premium

6.2 The callPackage pattern

Lesson 41 of 68 · 10:49

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Introduction to callPackage
  2. 1:22 Writing package functions
  3. 3:26 Using callPackage to fill dependencies
  4. 5:18 Overriding packages with different compilers
  5. 8:23 Cross compilation with callPackage
Transcript

Introduction to callPackage

0:00 Let's go with the callPackage pattern now. The thing is that this pattern looks like a useless indirection at first and the explanation how it works is halfway academic. However, it unlocks the override feature on packages and after you've seen what is possible with it you don't want to live without it any longer. So let's start. Look what it is. In general, we will now change our mkDerivation calls in our packages in a specific way. What you can see here is that we don't write packages.stdenv here any longer. We just have a function where in the first line we pile up all the packages and things that we expect to exist.

0:48 So it's not packages.stdenv but just stdenv because you have a parameter here and here in this just synthetic example mkDerivation call we practically consume the inputs packages boost and packages openssl like any normal package right and of course we also don't use packages here but instead we use boost and openssl as parameters. So what is apparent now we would need to call this function and then we get a mkDerivation call. For that reason this is called package function and package functions are a very

Writing package functions

1:23 normal thing in nixpkgs where all packages are defined as such a function. You will never see in within nixpkgs a package that actually uses packages.stdenv packages boost packages this packages that that doesn't exist. What you will also see which we don't really use in this example but you will see that package functions in nixpkgs and in other big projects they not only list all the dependencies which is nice to see because you would just read the first line of a package and see what it depends on. Of course it could be split up into multiple lines for readability too. However you cannot only define functions there but it has become kind of the default to also define features. So let's say that this feature of this application here like our example application can run with an x-window server support and also without and then you would just add this as a flag where we would consume this variable lower below there and would say okay in the configure phase if which features x equals true then add a few configure flags or something like that. So we would add packages here in this list and also parameters that configure how our package would be built.

The full transcript comes with the lesson.

About this lesson

The callPackage pattern looks like a useless indirection at first. Then you see what it enables and you never want to write a package any other way. In this lesson we turn a plain mkDerivation file into a package function, call it with pkgs.callPackage, and get the override feature that all of nixpkgs is built on.

In this video

  • Rewriting default.nix into a normalized package function that names all its dependencies in the first line
  • Feature flags as function parameters, the same way nixpkgs packages do it
  • Wiring the package up from a release.nix with callPackage and watching Nix fill in the arguments by itself
  • Building the same recipe with GCC and with Clang, and why C++ teams put exactly this into their CI
  • The mechanism vs policy split, and one short pkgsCross line that produces an ARM binary on an Intel machine without touching the build system

After this lesson

You can write package functions the way nixpkgs does, and swap a compiler, a dependency, or even the target platform without forking the recipe.