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

6.7 Import from derivation (IFD) and why teams ban it

Lesson 46 of 68 · 13:45

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 What IFD Means in Nix
  2. 1:44 Toy Example: Generating and Re-importing a Nix Expression
  3. 4:20 Why IFD Hurts Performance and Evaluation
  4. 8:20 Organizational Risks and Real-World Use Cases
  5. 12:00 Alternatives, Cross-Compilation Issues, and Wrap-Up
Transcript

What IFD Means in Nix

0:00 Okay, now that we're handling anti-patterns, IFD is the biggest and most important one because it's kind of a little bit controversial if you want to call it an anti-pattern or not. It's just that it's problematic in many organizations. Why is that? Let's first understand what IFD really means and how it works. So IFD stands for import from derivation. It means that we import from an output path of another Nix expression, so to say.

0:37 And to build that, kind of build a little example for that, which looks like it's perfectly fine, but it actually produces problems on a small scale, of course, because it's a little example. So when we import nixpkgs, nothing wrong here, then we callPackage another Nix expression, which is fine, and the Nix expression that we're calling here is this one. What it does is it runs command, it runs runCommand, and that basically just runs the shell script and creates some output folder.

1:14 What is happening here is we simulate slowness by sleeping for five seconds, then we cat for output, hello.nix file into $out, and that just contains the hello world Nix expression where we just point into the source code of hello and run mkDerivation on it, basically. So you should be able to see what this alone does.

The full transcript comes with the lesson.

About this lesson

Import from derivation (IFD) is the anti-pattern people keep hearing about without knowing what it means. In this lesson we build the smallest possible IFD example: a Nix expression that generates another Nix expression, imports it, and builds it. Then you watch exactly how it stalls evaluation, and why many organizations forbid it outright.

In this video

  • A minimal IFD demo you can reproduce: runCommand writing a hello.nix, then callPackage on its output
  • The caching behavior: which edit forces the slow wait again and which one comes straight from the cache
  • Why IFD does not parallelize: the evaluator blocks on every generated expression, one after another
  • How one second per project turns into a silent terminal for 10 or 40 minutes on the system image team's machine
  • Crane vs naersk for Rust: two answers to the same problem, only one of them uses IFD, plus the cross-compilation trap

After this lesson

You can recognize IFD in your own code, predict what it costs downstream, and demonstrate the problem to a colleague who does not believe it yet.

Links: