Nix 101 Course / Section 4: How Nix actually builds: sandbox → derivation → hash → store
Premium

4.6 Why Nix rebuilds your unchanged package

Lesson 29 of 68 · 5:07

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Unexpected Rebuilds in Nix
  2. 0:53 Source Closure and the Result Symlink Problem
  3. 2:51 Filtering the Source to Restore Caching
Transcript

Unexpected Rebuilds in Nix

0:00 Okay, so I promised that there is a bad surprise in our derivation. We have seen that it works. It compiles, it installs and stuff like that. And let's run it again. Maybe you have noticed that there is a little error message there. It doesn't seem to be critical because the output works anyway. But now, what's the problem that I see here? I run nix build again and it recompiles. I run nix build again and it recompiles.

0:32 And I run nix build again and recompiles. So wasn't Nix supposed to kind of cache packages if we have built them already? Why is that happening now? Isn't that like maybe everything that I wanted to use Nix for? So that's kind of... I mean, we didn't change the package.

Source Closure and the Result Symlink Problem

0:53 I didn't touch the Nix file. I didn't touch the builder. I didn't touch the make file. I didn't touch the C source code, but still it's rebuilding the package. Imagine the package wouldn't build in a second, but an hour. We would waste a lot of build time now. What is the reason for that? So what's interesting is, of course, the line that we have here already hints at that. When we built the Nix expression, we can see that there's this result symlink. And that is useful because we can practically say result /bin/mini-hello to run the application.

The full transcript comes with the lesson.

About this lesson

You run nix build on mini-hello twice in a row. Nothing changed, no file touched, and it compiles again anyway. This lesson finds the mistake in our own derivation that defeats the Nix cache, a mistake that shows up in real companies as whole teams rebuilding three hour product builds for no reason.

In this video

  • Watching an unchanged package rebuild on every nix build, plus the small warning that hinted at it all along
  • The source closure: what actually goes into the sandbox, and how one careless reference drags in files that have no business being there
  • The self feeding loop hiding in plain sight in the project folder
  • A small layout change that makes every following build come straight from the cache
  • Why source filtering matters far more at scale than in this tiny example

After this lesson

You can spot a derivation that pins too much source and fix it before it burns hours of build time. This keeps the caching that you came to Nix for actually working.

Builds on: Exercise: package mini-hello with builtins.derivation (4.4)