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

6.4 override vs overrideAttrs

Lesson 43 of 68 · 13:00

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Introduction to override and overrideAttrs
  2. 1:35 How callPackage and package overrides work
  3. 4:20 Using overrideAttrs to change derivation attributes
  4. 7:10 release.nix variants and building specific outputs
  5. 10:10 Refactoring hello into release.nix and overriding builds
Transcript

Introduction to override and overrideAttrs

0:00 After we learned how to create a package function from our build recipe, we also had a quick look into how to override it. In this chapter we'll now have a closer look at it, how to override, how to overrideAttrs, what is the difference and so on. Let's have a look at our standard release.nix file to be able to look at. We generally, similar to Flakes actually, so what we're doing here right now is very similar to what you can use in Flakes too.

0:31 Of course we import nixpkgs somehow again and here we call our package function with callPackage which automatically fills out all the parameters. But also another feature of callPackage is that it creates this package with an attribute override inside. So we can call that to call the package function again and this time exchange some of the parameters. In this example we're just inheriting A from up there so that we can kind of reuse it without the rec anti-pattern inside this attribute set.

1:08 And here of course package_name_a is a bad name, but for this example it works. And here we imagine that our package function has an input openssl and also a flag static_build=true. We have seen earlier on the slides that we can not only provide the package dependencies that we have as a parameter, but also flags which are not packages but then information like do we want to enable feature x or y or z or whatever.

The full transcript comes with the lesson.

About this lesson

Both override and overrideAttrs change a package, so which one do you use when? This lesson draws the line precisely: one re-calls your package function with different arguments, the other changes what goes into mkDerivation itself. We prove the difference on GNU hello with a compiler swap and a patch list.

In this video

  • The one picture that shows what override can touch and what only overrideAttrs can reach
  • Building single attributes from a release.nix with nix build -A, and what your CI should build
  • Swapping stdenv for clangStdenv with override and confirming it in the configure output
  • Changing the patches list with overrideAttrs, and why override cannot do that here
  • Reading the old attributes before you change them, the part that confuses everyone at first

After this lesson

For any change you want to make to a package, you can name the right function in seconds and write the override without guessing. This is the exact question we get in every training, answered for good.