Nix 101 Course / Section 7: Advanced packaging topics
Premium

7.3 Dynamic patching with substituteInPlace

Lesson 49 of 68 · 7:15

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Why patching is needed in Nix
  2. 1:08 Using substitute and substituteInPlace
  3. 3:06 substituteAll variants and practical examples
  4. 5:09 Why replace fail is safer than sed
Transcript

Why patching is needed in Nix

0:00 So let's talk more about some other helpers that are useful for patching. Very often you will end up patching code from third parties in your Nix expressions. Sometimes because the code is not portable, maybe you have a build system where they bake in the build user, the build host, the git commit, or the date, or whatever, and you really don't want that in your Nix expressions because they are an impediment to the reproducibility. Also, maybe sometimes you have an application that you run from somewhere else, like a script or a binary or whatever, and then they access user bin something something, and that doesn't exist in your sandbox or also not even in your NixOS systems that you deploy later.

0:48 So you sometimes end up patching applications. And of course, ideally you would upstream a change to an open source project out there because you can discuss with them what's the best way to implement that, what's portable and so on. But sometimes there is no time for that, or sometimes people don't want it. I think the number of people who have not heard about Nix is shrinking, but at the same

Using substitute and substituteInPlace

1:11 time sometimes people are saying, oh yeah, another Nixer who wants to patch stuff to make stuff more portable while everything already worked. Examples, examples, in the sandbox, when you run mkDerivation, there's always installed helpers that are called substitute, substituteInPlace, and so on. And I want to show them to you. The substitute script here, for example, is a bash function that always comes with your standard environment, is an application that can accept an input file and writes an output file from that while replacing several things.

The full transcript comes with the lesson.

About this lesson

Third-party code often hard-codes paths like /usr/bin/... or bakes the build user and date into binaries, and both break builds in the Nix sandbox and hurt reproducibility. This lesson covers dynamic patching with substituteInPlace and its sibling helpers, the stdenv tools that rewrite source files during the build without maintaining brittle patch files.

In this video

  • The four helpers stdenv always ships: substitute, substituteInPlace, substituteAll, and substituteAllInPlace, and when each one fits
  • Replacing string patterns and injecting environment variables through placeholder patterns in your sources
  • A live demo: rewriting a C file in the patch phase and watching the program's output change
  • Why this approach beats classic patch files when upstream sources keep moving
  • The sed trap: the failure sed swallows silently that one specific flag turns into a loud build error, exactly when someone upgrades the package

After this lesson

You can fix unportable third-party code right inside your derivation, and set it up so a future version bump fails loudly instead of shipping a silently unpatched build.

Builds on: Build phases (5.4)