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

4.3 The derivation is just a recipe

Lesson 26 of 68 · 8:46

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Inspecting Derivation Files
  2. 1:14 Converting Derivations to Readable JSON
  3. 2:50 Inputs, Dependencies, and the Nix Store
  4. 5:09 How Nix Maps to execve
  5. 7:22 Build Execution and Dependency Trees
Transcript

Inspecting Derivation Files

0:00 Okay, now that we kind of played around with the sandbox and got a feeling like how it looks like inside, how can we make applications available, and what can we even assume, kind of, right? We did not have a look yet how the derivation files look like. So in the slides we had this here that we kind of translate a Nix expression into a derivation and then build that or realize that into a store path.

0:32 We can now use the Nix-instantiate command to just create a derivation file and then look inside what it's in there, right? So let's say instead of running Nix-build, which without any parameters basically does the same like this here, right? As already mentioned. You can also use Nix-instantiate. Nix-instantiate does not build. And then we get this derivation path out here. And when we run simply cat on that to print the content, we can see like, okay, so this is like not really obvious what's in there, but it's somehow structured stuff.

1:09 This format here is called ATerm or something like that.

The full transcript comes with the lesson.

About this lesson

Every Nix build runs off a .drv file in the Nix store. In this lesson we create one with nix-instantiate, convert it to JSON, and read it line by line. It turns out a Nix derivation is a short, fully machine readable recipe, and the Nix build engine underneath is a much simpler machine than it looks from the outside.

In this video

  • Producing a derivation file without building anything, and pretty printing it with nix derivation show and jq
  • What a derivation actually contains: a builder, its arguments, an environment, input derivations and sources, and not much else
  • How referencing dependencies by derivation path turns Nix into a build scheduler that pauses your sandbox, fetches or builds what is missing, then continues
  • The one Linux system call the whole data structure maps onto, field by field
  • The two checks Nix runs at the end to decide whether your build counts as a success

After this lesson

Real world derivation files are longer, but not more complicated: more environment variables, more inputs, same shape. You can open any .drv file on your system and know what you are looking at.