Nix 101 Course / Section 9: Flakes
Premium

9.2 Anatomy of a flake: inputs, outputs and the lock file

Lesson 60 of 68 · 16:15

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Flake basics and structure
  2. 2:00 Inputs, outputs, and pinning dependencies
  3. 5:00 Multi-platform packages and default outputs
  4. 9:00 Inspecting Flakes with nix commands and the REPL
  5. 13:00 Dev shells, lock files, and next steps
Transcript

Flake basics and structure

0:00 Okay, so how do Flakes work in practice? In general, there's not too much that you can do wrong, but they already contain some unnecessary complexity. In general, you basically want to have a description. I mean, whenever a stranger looks into your Flake, and one of the advantages of Flakes is that they practically don't have to look through the whole repository to see what's there. The Flake Nix file is just a central file that lists whatever your project provides, so make it nice, give the user a nice experience where the Flakes also describes itself and stuff like that, but you don't need it.

0:38 More interesting is the inputs clause and the outputs clause or section. In the inputs, you can describe that we, for example, depend on Nix packages. 99% of the Flakes always depend on Nix packages, and then they build on top. Here you can also list more different Nix packages, not Nix packages, but other Flakes, and then these refer to repositories. In this case, of course, Nix has this abbreviation for GitHub. It also works with GitLab, because most of the community stuff is hosted on GitHub anyway.

1:13 That might change in the next years. However, this notation works like this, that you say, OK, from GitHub, from the org called NixOS, and then the repository called Nix packages, that would already work alone. But after the second slash, you can define the branch. In this case, it's NixOS unstable. Of course, this alone is not a pinning expression yet, because NixOS unstable can be something else every day. So when you first build something for the first time, you will get a new Flake lock file, and that really pins down which commit was used there.

The full transcript comes with the lesson.

About this lesson

Every flake.nix file has the same skeleton: a description, flake inputs, and outputs. This lesson takes a minimal flake apart line by line, so flake.lock, legacyPackages and the packages.system.name attribute path stop being mysterious. Along the way you get the full CLI tour: nix build, nix run, nix develop, nix flake show and nix flake check.

In this video

  • Why outputs are a function of inputs, and what the flake.lock file actually pins
  • The odd rule about architectures in packages, and a two-line trick that supports every platform nixpkgs supports
  • Why nixpkgs hides its packages inside legacyPackages, explored live in the REPL with :lf
  • What default means for nix build and nix run, and how a flake references itself through self
  • Catching a broken output structure with nix flake check before anyone else has to tell you

After this lesson

You can read any flake.nix you find on GitHub, add a dev shell to one, and inspect a flake you never checked out using nix flake show.