Nix 101 Course / Section 9: Flakes
Premium

9.9 Anti-patterns: why you do not need flake-utils or flake-parts

Lesson 67 of 68 · 13:37

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Anti-patterns in flake helpers
  2. 2:06 Replacing flake-utils with mapAttrs
  3. 5:55 Why flake-parts is overkill here
Transcript

Anti-patterns in flake helpers

0:00 Let's discuss some potential anti-patterns now. We have already talked about flake utils and flake parts, but in this video I want to really show you what's the actual problem with those. So this one here is a simplified example where you basically just create a flake that emits a packages hello as the default package output and has a dev shell. Of course, every project is more complex than that, but most of the time you just have packages and at least one dev shell, right?

0:32 And what we do here is we don't have this system architecture thing in between. Normally, a flake would say packages.architecture.packageName and here we can simply comfortably omit the part in the middle. That is because we're using the eachDefaultSystem function from the useful flake utils library. It has been invented to save you the gymnastics of providing the same package calls for each architecture again and again.

1:03 However, I would argue that you don't need that, especially as we have seen that it's simpler to use this mapAttrs trick. Let's have a quick look. So I practically have the same example here and what we now get is, for example, nix flake show. We see that we support four different architectures, x86_64 Darwin has now been dropped already, but that is not being reflected by the current flake utils things.

The full transcript comes with the lesson.

About this lesson

Almost every flakes tutorial reaches for flake-utils or flake-parts to handle multiple architectures. This lesson makes the case that you usually need neither, and shows live how removing them leaves your flake.nix shorter, supporting more platforms, and with fewer inputs to drag around.

In this video

  • What eachDefaultSystem really gives you, and the platforms it silently locks out
  • Watching transitive flake inputs pile up with flake-du, and why fifteen copies of the same helper is a normal sight in the wild
  • The two-line replacement pattern, no extra input required
  • flake-parts and its nixpkgs-lib input: when the module system is worth it, and when it is far too much for a list of systems
  • The Nixcademy article behind this lesson, about a thousand instances of flake-utils

After this lesson

You can strip helper libraries out of your flakes with confidence, and justify the decision to your team with measurements instead of taste.

Builds on: Starting a flake from scratch with nix flake init (9.4)

Links: