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

4.5 How Nix finds your runtime dependencies

Lesson 28 of 68 · 14:56

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Runtime vs build-time dependencies
  2. 2:00 Inspecting dependency trees with nix-store
  3. 5:00 How Nix derives runtime closures
  4. 8:00 Using nix-tree and why-depends
  5. 11:00 Copying closures and practical use cases
Transcript

Runtime vs build-time dependencies

0:00 OK, so now we built the Mini Hello application. And what I want to analyze with you now is how does Nix kind of determine the actual runtime dependencies now. Because we have seen that to kind of make GNU Make and GCC and coreutils available are inside the sandbox. We need to name them or mention them kind of in the derivation or in the recipe that goes into the derivation. And then they will be in the sandbox.

0:31 Now, to run the Mini Hello application, it would be crucial if you want to copy the output to some other computer, what would it need to run? We can already see that result in Mini Hello, right? When you run ldd on that, we see that we will at least need the glibc, the C library for that. And that would be kind of the dependency. If that was not on the target computer, after we copied the Mini Hello executable somewhere else, it would crash, right, because it would need that.

1:06 Now, of course, we kind of already mentioned the C library, because it comes at least with packages.gcc. Now, we wouldn't need GNU Make and coreutils on the target computer, right? The question is now, how does Nix calculate from the dependencies that we were able to describe for the build recipe for runtime now? We have some tools for that. So there's the command nix-store query, and then we can print the tree of the dependencies.

The full transcript comes with the lesson.

About this lesson

Building mini-hello needed a compiler, GNU Make and coreutils. Running the finished binary needs almost none of that. This lesson shows how the Nix store keeps two different dependency trees for every package, build time and runtime, and how Nix computes the runtime closure without you declaring it anywhere.

In this video

  • nix-store --query --tree on the output path versus the derivation path: two very different trees for the same package, and what each one is for
  • The trick Nix uses to detect runtime dependencies inside your binaries, and why a store path can never end up there by accident
  • Exploring what makes a package big with nix-tree, and tracing who pulled in an unwanted dependency with nix-why-depends
  • Comparing two builds with nix-diff when an image suddenly grew
  • Copying a runtime closure to another machine with nix copy, and why that covers most of what people use Docker images for

After this lesson

You can list exactly what a package needs to build and what it needs to run, answer CVE and image size questions straight from the dependency tree, and deploy an application to another machine without containers.