Nix 101 Course / Section 7: Advanced packaging topics
Premium

7.4 patchShebangs: running scripts in the Nix sandbox

Lesson 50 of 68 · 8:18

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Why scripts need patchShebangs in Nix
  2. 1:35 Example project: shell and Python scripts
  3. 3:34 Fixing shebangs with build inputs
  4. 5:40 Testing scripts in the sandbox with checkPhase
Transcript

Why scripts need patchShebangs in Nix

0:00 Another important thing that you really want to know about is when you want to run shell scripts or Python scripts or whatever scripts, Perl, Ruby, it doesn't really matter, interpreted script languages inside the sandbox. The thing is that inside the sandbox, we really don't have user_bin_env or user_bin_bash, user_bin_python, user_bin_env, whatever. It just doesn't exist, right? Unless you would link something like that together, but you typically don't. And to run those scripts, you need to use the tool patchShebangs.

0:35 patchShebangs looks into a script folder and then finds all the scripts, or you can point it to a specific script. However, it will patch your shebang line. The shebang line is the first line where you have hashtag, exclamation mark, and then some path. As long as you have Python in your path in the sandbox, because you, for example, added as a build input, as long as that is true, patchShebangs will take that from there and patch it into your shebang line.

1:06 That works with Perl, with Ruby, with Shell, with Python, with whatever. So if you want to run your own scripts inside the sandbox, you're going to use patchShebangs ideally. What happens also, what happens automatically, is after the install phase, when you copy Shell scripts into the $out folder, the fix-up phase will patch them automatically. It will find your scripts in the output and see, oh, are there shebang lines that we could kind of fulfill with packages that we have in our build inputs?

The full transcript comes with the lesson.

About this lesson

Your shell or Python script runs fine on your machine, then dies in the Nix build sandbox with "bad interpreter: No such file or directory". The reason: /usr/bin/env and /usr/bin/bash simply do not exist there. This lesson explains patchShebangs, the stdenv tool that rewrites shebang lines to point at interpreters from your build inputs, and shows when Nix already does it for you automatically.

In this video

  • Why the sandbox has no /usr/bin, and what that means for shell, Python, Perl, and Ruby scripts
  • What the fixup phase patches automatically after installing, and the exact conditions it needs (one of them is a file permission bit)
  • A live demo: a Python script that fails, then works after one line in the derivation
  • Testing scripts in checkPhase before installing them, and why that needs a manual patchShebangs call
  • buildInputs vs nativeBuildInputs for interpreters, and why the difference matters once cross-compilation enters the picture

After this lesson

You can package any collection of scripts so they run inside the sandbox and on every target machine, with a pinned interpreter instead of whatever the host happens to have.

Builds on: The sandbox (4.2)