Nix 101 Course / Section 5: Packaging real software with stdenv.mkDerivation
Premium

5.10 Build helpers: runCommand, writeShellApplication, and symlinkJoin

Lesson 39 of 68 · 17:52

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Introduction to Build Helpers
  2. 1:58 mkShell with Multiple Python Versions
  3. 5:43 runCommand for Generating Output Files
  4. 10:25 writeShellApplication for Reproducible Scripts
  5. 14:38 symlinkJoin and Composing Package Trees
Transcript

Introduction to Build Helpers

0:00 So this video is about build helpers. Very often you just want to have a Nix expression that says okay in the sandbox run one command and create some output files. For example, if you want to open some open API JSON spec and create JavaScript client code against that or something and it would be just one command where your input is the JSON file and the output is your library and then you would want to consume that further somewhere else and then you can nicely compose this into individual package like derivations so to say but mkDerivation doesn't work like this it wants the different phases and then you would have to disable them and then write code that's kind of very repetitive and there are also other kinds of situations where you practically want the same thing again and again in a very simple way and for that we have the build helpers. So let's have a quick look. In general these were also formerly called trivial builders and you will also still see in the nixpkgs repository trivial builders folders where these tools that we are going to look at are defined but nowadays they are more formally called like build helpers. This is also the wording and documentation. These are typically wrapper functions around mk Derivation that make it easier to use. So we have already seen mkShell in the shell section earlier. We can also use runCommand to simply run some quick package output. We

1:31 can prepackage standalone scripts and so on. The thing is here that we would create a series of many boring videos if you would just show all of them so the idea is to show you the most important ones that I also personally use a lot like every week so that you have seen them once and recognize later when you are also in a situation where this might be useful. Apart from that if you find this somehow inspiring you can go into the nixpkgs

mkShell with Multiple Python Versions

1:59 manual and look at the build helpers there and also look into the source code. So what's slightly disappointing here about the documentation is that when we look into the docs let's go back to the nixpkgs reference manual and we go to the trivial build helpers here or build helpers in general. There's a lot of text again, fixed point stuff and so on. We'll do fixed point later. Of course Fetchers are also build helpers. Here the trivial build helpers runCommand, concatTextFile, writeShellApplication and symlinkJoin and stuff like that. There are some nice examples how to use these things that you can unfold and then these kind of are actually pretty good or well documented. It's just not the whole story or the full story. So let's go through some examples and then we know more. So we have already played with mkShell. What's cool about mkShell is that we can also like on the one hand fill it with packages that can then be used there or get the dependencies from other packages, right? So in a repository with like five different sub projects you can have a Nix shell that combines all of them and we have played with that already.

The full transcript comes with the lesson.

About this lesson

Sometimes a package is just one command that turns an input file into an output file, and writing full phase boilerplate for that would be silly. This lesson covers the nixpkgs build helpers worth using every week: runCommand, writeShellApplication, symlinkJoin, and Python environments in mkShell. All of them are small wrappers around mkDerivation that shrink everyday tasks to a few lines.

In this video

  • A dev shell that mixes three different Python versions, each tool wrapped to run on its own interpreter
  • python3.withPackages: interpreter plus libraries in one step, no venv, no pip
  • runCommand: a complete package from a single shell snippet, demonstrated on a cowsay template gallery that stands in for real config and code generation
  • writeShellApplication: turning a bash snippet into a pinned, shellcheck-linted application that runs the same on every colleague's machine
  • symlinkJoin: merging many packages into one tree, plus a hashes.txt trick that administrators love for download servers

After this lesson

You can generate files, bundle scripts, and compose package trees with a few lines each, the glue work that makes Nix useful far beyond compiling software, especially for DevOps and admin tasks.

Builds on: Minimal mkDerivation (5.2)

Links: