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

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)