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

4.4 Exercise: package mini-hello with builtins.derivation

Lesson 27 of 68 · 12:08

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 From Toy Derivations to Real Packages
  2. 1:28 Inspecting the Mini Hello Source and Manual Build
  3. 3:40 Building Inside the Nix Sandbox
  4. 7:10 Creating the Output Package and Installing It
Transcript

From Toy Derivations to Real Packages

0:00 In the previous videos, we just made a toy derivation that just produced a text file, which is relatively pointless, but it helped us to understand how the Nix sandbox constructs like from nothing to an environment where we can actually run stuff, then how do we change what's available inside the sandbox and so on. Now let's use that all to build like some real life like software. For that, I've prepared this exercise where we can basically just download the zip file that contains a Hello World application, and then what we do is we use the builtins derivation primitive again and maybe for the last time in our life to build the application mini hello which comes from there.

0:42 The idea is to not use stdenv.mkDerivation just yet. After that, we can inspect the derivation again to have a look like how it works. And the other thing that I want to show you is we want to build a first class installable package with that. So we built the mini hello application here, and we put the mini hello executable inside the bin subfolder that we need to create in that package.

1:13 And then we can actually run nix profile add to add this application there. And the packages that we're going to need are gcc, gnumake, and coreutils. So that's the compiler right the package as we will see is based on gnumake, the build

The full transcript comes with the lesson.

About this lesson

Time to build real software in the sandbox with your own hands. In this exercise you package mini-hello, a small C program with a Makefile, using nothing but builtins.derivation, gcc, GNU Make and coreutils. No mkDerivation, no helpers, no safety net. Working through this once is what later makes failing builds readable instead of mysterious.

In this video

  • Your mission: compile mini-hello inside the Nix build sandbox and ship the binary in $out/bin
  • The three steps you have to figure out, starting from a build folder that is completely empty, source code included
  • Hints with pause points along the way, so you get exactly as much help as you want
  • Turning your output into a first class package: install it with nix profile add, run it from anywhere, remove it again
  • Why this exercise takes people up to two hours in live trainings, and why that fight is the point

After this lesson

You have built and installed a working package with a hand written builder script. When a high level builder fails later and something is missing from the environment, you recognize the situation, because you assembled that environment yourself once.

Links: