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

4.2 Inside the Nix build sandbox

Lesson 25 of 68 · 28:38

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Introduction to the Nix sandbox
  2. 1:35 Building a minimal derivation
  3. 4:10 Inspecting the sandbox environment
  4. 10:20 Adding tools and paths to the build
  5. 16:20 Filesystem and process isolation
  6. 22:05 Network restrictions and reproducible inputs
  7. 26:05 Using makeBinPath for cleaner environments
Transcript

Introduction to the Nix sandbox

0:00 Okay, so in this video, we're going to look into the Nix sandbox to understand what's really going on in there. And that will help us understanding problems and debugging them when we build like real derivations later. So what is our simplistic minimal derivation look like? The idea is that we basically run a builder shell script. So we will have a shell script file that basically just contains this one line for the start. It enters like two bar as a string into a file. What file is that?

0:32 It's the output file. As we remember from the slides earlier, each package finally goes into some output path. And this path can be either a folder with all your binaries and main pages or whatever, or it can be just a file. In this simplistic example, we just created as a file and we'll see later that inside the sandbox, we will always have the dollar out environment variable that contains the pre-calculated output path.

1:05 And now the Nix expression that sets it all up so that inside the sandbox, this script will be run is that here. We basically quickly import Nix packages from somewhere. So that's kind of not the important part here. The important part is that we run the derivation function. So we go through all the parameters here, why they exist, and then we build it. At first, we'll just build it and see what the result is. And later we'll learn how to run nix-instantiate to look into the derivation file that we actually created.

The full transcript comes with the lesson.

About this lesson

Every Nix build runs in a sealed off mini world: no internet, no home folder, almost no files. In this lesson we break into that world and look around. Afterwards you know why "works on my machine" problems cannot happen with Nix.

In this video

  • Building the smallest possible derivation: four parameters and one shell script
  • The environment inside the sandbox: a handful of variables with strange values like HOME=/homeless-shelter, and why that is a feature
  • A tour of the sandbox file system: what is in /bin, /dev, /proc and /etc, and what is missing
  • Watching internet access fail on purpose, and what that buys you
  • The contract between Nix and your build script, and the one variable everything depends on

After this lesson

When a real build fails later, you know exactly what world it failed in. This is the lesson that turns build debugging from guessing into inspection.