Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Created August 24, 2018 07:50
Show Gist options
  • Save CMCDragonkai/9b65cbb1989913555c203f4fa9c23374 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/9b65cbb1989913555c203f4fa9c23374 to your computer and use it in GitHub Desktop.
makeWrapper and wrapProgram #nix

makeWrapper and wrapProgram

Nix generally assumes run-time dependencies is a subset of the build-time dependencies.

This means many Nix builder functions try to automatically scan the output for runtime dependencies and "rewrite" them for runtime usage.

However shell scripts which are often exported by packages do not get this automatic scanning treatment.

This means you have to use the makeWrapper package and use either the makeWrapper or wrapProgram utility functions.

You may use them in the postFixup phase of a derivation:

postFixup = ''
  wrapProgram $out/bin/some-script \
    --set PATH ${lib.makeBinPath [
      coreutils
      findutils
      gnumake
      gnused
      gnugrep
    ]}
'';

We use the lib.makeBinPath to compose paths from a number of derivation outputs.

One should always try to use --set instead of --prefix because you shouldn't rely on the user profile environment variables.

@MikaelFangel
Copy link

Thank you for sharing! ❤️

@s1n7ax
Copy link

s1n7ax commented Oct 11, 2023

lifesaver

@snaeil
Copy link

snaeil commented Jun 7, 2024

Do you have a full example of a default.nix file, that is used to build a derivation?
How would some-script have to look like?

@milahu
Copy link

milahu commented Jun 7, 2024

How would some-script have to look like?

the some-script in wrapProgram $out/bin/some-script can be any executable

wrapProgram will move some-script to .some-script-wrapped
and create the wrapper some-script

#!/bin/sh
export PATH=/nix/store/xxx-coreutils/bin:/nix/store/xxx-findutils/bin:...
exec /nix/store/xxx-some-script/bin/.some-script-wrapped "$@"

@snaeil
Copy link

snaeil commented Jun 7, 2024

Thanks @milahu <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment