YOUR JOURNEY
06 / 18
01NIX FUNDAMENTALS 6 MIN READ
LESSON 06 / FROM RECIPE TO PACKAGE

A build recipe.
Made explicit.

A derivation describes a build before that build happens.

Follow a package from expression to build plan to output. Evaluation and building are separate steps.

Explore the lesson
~$ Build your understanding, one idea at a time.
THE NIX MODEL01 / 03
Drag to rotate

Evaluation creates a derivation: a low-level build plan.

Expression → .drv build plan
06.1MEET THE DERIVATION

A recipe is not the meal.

A derivation records a builder, arguments, environment, inputs, and outputs. Evaluating an expression can produce this plan without performing the build itself. Realizing it obtains the output by building or substituting from a configured cache.

Most packages use helpers from Nixpkgs rather than constructing raw derivations. This tiny example uses runCommand to create a file as its output.

default.nix
{ pkgs ? import <nixpkgs> {} }:
pkgs.runCommand "first-output" {} ''
  mkdir -p "$out"
  printf 'Made by Nix\n' > "$out/message.txt"
''
Next section: Meet the result link.
06.2BUILD SOMETHING SMALL

Meet the result link.

Save the file as default.nix and run these commands in that directory. nix-build creates a result symlink to the output. The builder’s $out is the assigned store destination.

This first recipe uses your current <nixpkgs> source. Later we will lock that source with a flake. A sandboxed build reduces accidental dependencies on your machine, but reproducible outputs also require well-behaved build tools.

terminal
nix-build
cat result/message.txt
readlink result
# message.txt contains: Made by Nix
TAKE THIS WITH YOU

Evaluation makes a plan; realization obtains the plan’s outputs.

Go a little deeperPackaging with Nix
UP NEXT

A system in a file

NixOS applies the same model to your operating system.

Next lesson