Use autoPatchelf inside mkShell

Is it possible to use autoPatchelf inside a mkShell? I want to create a virtualenv inside my project folder and use autoPatchelf to fix the problems with shared libs. I have the following shell.nix:

with import <nixpkgs> { };

mkShell {
  name = "test";

  buildInputs = [
    python3Packages.venvShellHook
    autoPatchelfHook
  ];
  venvDir = "my_env";
  postShellHook = ''
    pip install numpy
    autopatchelf my_env
  '';
}

It’s creating the virtualenv but autopachelf is not being found:

❯ nix-shell
Using venvShellHook
Executing venvHook
Creating new venv environment in path: 'my_env'
Collecting numpy
  Using cached numpy-1.22.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.8 MB)
Installing collected packages: numpy
Successfully installed numpy-1.22.3
autopatchelf: command not found
Finished executing venvShellHook

1 Like

I don’t have an exact answer, but both of these are hooks. I’d wager that the functions explicitly don’t have each other in scope; you could probably source the script directly instead though. I’d love some detailed docs on the hook mechanism in general, the nixpkgs manual doesn’t explain much, and the setup.sh script is as easy to understand as you’d expect a bash script with hidden dependency scripts to be…

Anyway, I’m commenting because this topic basically does what you want, and comments go over alternatives: https://discourse.nixos.org/t/how-to-use-python-on-nixos-without-nix/

Could it be a simple typo? From what I see the function is called autoPatchelf (with capital P) whereas your code refers to all lower-case autopatchelf.

3 Likes

You are correct, it’s autoPatchelf not autopatchelf. Now it’s working! Many thanks!