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

6.6 Nix anti-patterns that bite you later

Lesson 45 of 68 · 15:43

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Introduction to Nix Anti-Patterns
  2. 1:12 Overusing with
  3. 3:39 Using rec and Reusing Attributes Safely
  4. 6:30 Nixpkgs Amnesia and Centralized Imports
  5. 10:12 Overriding Phases Without Losing Hooks
  6. 12:48 callPackage vs callPackages
  7. 15:00 Preview of Import From Derivation
Transcript

Introduction to Nix Anti-Patterns

0:00 OK, so now we learned how to organize a project where we could have multiple variants of the same package. Of course, you will have projects with like multiple packages that are naturally different and then inject them into each other and so on. We'll put overlays on top of that. And that is already enough material to come up with like really complex Nix expressions. And we have seen a few patterns occurring over and over again. So people keep reinventing them. The thing here is that these are like, of course, there is no bad ideas behind them.

0:38 It's just that they don't scale well. So I want to help you avoiding those patterns after we have seen them hundreds of times in multiple organizations. Let's see what they are. So the problem with these anti-patterns is sometimes that we've described very specialized things for like, how would I ever come up with that? But trust me, people do come up with those. And now I want to increase your sensitivity on seeing and detecting them and helping yourself or your colleagues avoid them. First anti-pattern is overusing with. Already in the syntax part

Overusing with

1:18 of the class, we kind of had a look at when do you want to use it when not. And this is one of the particular examples where people are like, oh, yeah, yeah, let's have another import of nixpkgs somewhere. So the pkgs attribute, let's not carry it around. Let's extract all the lib functions because it's annoying to write lib dot this, lib dot that, and then just use it like this. It's super nice. And also I've seen it. So I copy it now in nixpkgs. Very often when we were explaining in our classes and trainings and companies like that, this is bad. And people are asking like, so why do people in nixpkgs do that? They actually did. And that's bad. However, it's also frowned upon there. And I think you won't get new code in that looks like that. However, what's particularly problematic here is that imagine all the examples that we go over here are like huge. Of course, they are relatively pointless because in five lines of code, these are not problematic.

The full transcript comes with the lesson.

About this lesson

None of these Nix anti-patterns look wrong when you write them. They bite months later, in 500 line files, when nobody can tell where a variable comes from or why an overlay is silently ignored. This lesson collects the patterns we have seen cause pain in many organizations: overused with, rec, accidental nixpkgs re-imports, wiped build phases, and misused callPackage.

In this video

  • Why with pkgs; at the top of a file hurts every future reader, and the inherit style that replaces it
  • The rec keyword: easy to write, expensive to read, and what to do instead
  • Reusing version inside a derivation, and the subtle way overrides then stop working as expected
  • nixpkgs amnesia: the forgotten function parameter that quietly throws away your overlays, config and pin
  • Overriding a whole build phase and deleting hooks other people rely on, plus the callPackage misuse that stacks overrides on overrides (and the function that exists for exactly that case)

After this lesson

You can spot these patterns in code review before they land, and explain to a colleague why the fix matters, not just that it does.

Builds on: override vs overrideAttrs (6.4)