7.3 Dynamic patching with substituteInPlace
Lesson 49 of 68 · 7:15
Up next in 5s…
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, andsubstituteAllInPlace, 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
sedtrap: the failuresedswallows 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)