ruff: Installing ruff via pip on NixOs does not work

Installing ruff via pip on NixOs systems does not work:

$ cd /tmp
$ python3 -m venv venv
$ source venv/bin/activate
(venv)
$ pip install ruff
Collecting ruff
  Using cached ruff-0.0.212-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB)
Installing collected packages: ruff
Successfully installed ruff-0.0.212

[notice] A new release of pip available: 22.2.2 -> 22.3.1
[notice] To update, run: pip install --upgrade pip
(venv)
$ ruff
-bash: /tmp/venv/bin/ruff: No such file or directory
(venv)

$ file /tmp/venv/bin/ruff
/tmp/venv/bin/ruff: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=1ec33df614fc5020de42d625366e4684ae45a0b6, for GNU/Linux 2.6.32, stripped
(venv)

The dynamic loader (“ELF interpreter”) is not in the path hardcoded in the binary, although ldd shows the right path:

$ ldd /tmp/venv/bin/ruff
        linux-vdso.so.1 (0x00007f8623e51000)
        libgcc_s.so.1 => /nix/store/hsk71z8admvgykn7vzjy11dfnar9f4r1-glibc-2.35-163/lib/libgcc_s.so.1 (0x00007f86233eb000)
        librt.so.1 => /nix/store/hsk71z8admvgykn7vzjy11dfnar9f4r1-glibc-2.35-163/lib/librt.so.1 (0x00007f86233e6000)
        libpthread.so.0 => /nix/store/hsk71z8admvgykn7vzjy11dfnar9f4r1-glibc-2.35-163/lib/libpthread.so.0 (0x00007f86233e1000)
        libm.so.6 => /nix/store/hsk71z8admvgykn7vzjy11dfnar9f4r1-glibc-2.35-163/lib/libm.so.6 (0x00007f8623301000)
        libdl.so.2 => /nix/store/hsk71z8admvgykn7vzjy11dfnar9f4r1-glibc-2.35-163/lib/libdl.so.2 (0x00007f86232fc000)
        libc.so.6 => /nix/store/hsk71z8admvgykn7vzjy11dfnar9f4r1-glibc-2.35-163/lib/libc.so.6 (0x00007f86230f1000)
        /lib64/ld-linux-x86-64.so.2 => /nix/store/hsk71z8admvgykn7vzjy11dfnar9f4r1-glibc-2.35-163/lib64/ld-linux-x86-64.so.2 (0x00007f8623e53000)
(venv)

I was able to get it working using patchelf, but it is not a good workflow when using a Python package manager (e.g. hatch, pdm, poetry, etc) to handle all the dev dependencies of the project.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 16 (9 by maintainers)

Commits related to this issue

Most upvoted comments

The best way I found to make it work is using nix-ld.

Just add these two lines to your configuration, rebuild, reboot and you are all set.

programs.nix-ld.enable = true;
programs.nix-ld.libraries = options.programs.nix-ld.libraries.default;

@messense,

Can you run it with LD_DEBUG=libs,files env var to see what’s going on?

The binary does not run, due to the different path of the Dynamic Loader.

@charliermarsh,

ruff does work if I install it on my machine via the OS package manager, but that is not a solution. The issue is that many Python projects use ruff as a linter defined in pyproject.toml/requirements.txt, and that gets installed via pip/pdm/hatch/poetry/etc in a local environment (a.k.a. virtual env). And then running the linter/test/format scripts simply does not work.

As an example:

$ git clone git@github.com:tiangolo/fastapi.git
$ cd fastapi/
$ python -m venv env
$ source ./env/bin/activate
$ pip install -e ."[dev,doc,test]"
$ bash ./scripts/format.sh
+ ruff fastapi tests docs_src scripts --fix
scripts/format.sh: line 4: /home/h/projects/fastapi/venv/bin/ruff: No such file or directory
+ black fastapi tests docs_src scripts
All done! ✨ 🍰 ✨
804 files left unchanged.
+ isort fastapi tests docs_src scripts

As you can see, ruff is just erroring out 😦

In some cases, we can install ruff via the OS package manager, but some Python projects “embed” it, or use/require/pin a specific version.

@anadon if shutil.which("nix") would do the trick to figure out whether nix is available at all, if that’s the idea…

@snowdrop4 something like patchelf path/to/bin/ruff. See Packaging/Binaries.

But I recommend to set the env var PIP_NO_BINARY=ruff and have cargo and rustc available to compile Ruff instaled via Pip. I use this Flake for this:

{
  description = "A Python flake";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
      let
        pkgs = import nixpkgs { inherit system; };
      in
      {
        # For nix develop
        devShell = pkgs.mkShell {
          shellHook = ''
            export PIP_NO_BINARY="ruff"
          '';

          packages = with pkgs; [
            (python311.withPackages (p: with p; [
              pip
              python-lsp-server
              python-lsp-ruff
            ]))

            rustc
            cargo
          ];
        };
      });
}