Pip OSError: [Errno 30] Read-only file system

I’m running NixOS unstable, and I get [Errno 30] Read-only file system errors whenever I try to use pip. Here’s a minimal example.

$ cat shell.nix
with (import <nixpkgs> {});
mkShell {
  buildInputs = [
    python39
    python39Packages.pip
    python39Packages.setuptools
  ];
}
$ nix-shell

[nix-shell:~/pip-test]$ python -m pip install sampleproject
Collecting sampleproject
  Using cached sampleproject-2.0.0-py3-none-any.whl (4.2 kB)
Collecting peppercorn
  Using cached peppercorn-0.6-py3-none-any.whl (4.8 kB)
Installing collected packages: peppercorn, sampleproject
ERROR: Could not install packages due to an OSError: [Errno 30] Read-only file system: '/nix/store/5bh6rpya1ar6l49vrhx1rg58dsa42906-python3-3.9.6/lib/python3.9/site-packages/peppercorn'

WARNING: You are using pip version 21.1.3; however, version 21.3.1 is available.
You should consider upgrading via the '/nix/store/5bh6rpya1ar6l49vrhx1rg58dsa42906-python3-3.9.6/bin/python -m pip install --upgrade pip' command.

I haven’t used pip on a NixOS system before, so I want to make sure I’m not doing something fundamentally wrong before I report this as a bug!

python packages are a bit odd in that you have to install them as part of an interpreter environment, not as an interpreter and separate packages for it to use. in this case you’d use

with (import <nixpkgs> {});
mkShell {
  buildInputs = [
    (python39.withPackages (p: with p; [ pip setuptools ]))
  ];
}

this is explained in more detail on the wiki page about python, and also holds for other interpreted languages like perl and ruby.

1 Like

Thank you for the help! I tried your suggestion, but I get the same error. So I read the sources you linked to, and tried this…

with (import <nixpkgs> {});
{ pkgs ? import <nixpkgs> {} }:
let
  python-with-my-packages = pkgs.python3.withPackages (p: with p; [
    pip
    setuptools
  ]);
in
python-with-my-packages.env # replacement for pkgs.mkShell

…and got the same error. Then I tried …

with (import <nixpkgs> {});
stdenv.mkDerivation {
  name = "pip-env";
  buildInputs = [
    # System requirements.
    readline

    # Python requirements (enough to get a virtualenv going).
    python39Full
    python39Packages.virtualenv
    python39Packages.pip
    python39Packages.setuptools
  ];
  src = null;
  shellHook = ''
    # Allow the use of wheels.
    SOURCE_DATE_EPOCH=$(date +%s)

    # Augment the dynamic linker path
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${R}/lib/R/lib:${readline}/lib
  '';
}

…and still got that error.

OK, got it working now. This is what worked:

~/pip-test $ cat shell4.nix
with (import <nixpkgs> {});
stdenv.mkDerivation {
  name = "pip-env";
  buildInputs = [
    # System requirements.
    readline

    # Python requirements (enough to get a virtualenv going).
    python39Full
    python39Packages.virtualenv
    python39Packages.pip
    python39Packages.setuptools
  ];
  src = null;
  shellHook = ''
    # Allow the use of wheels.
    SOURCE_DATE_EPOCH=$(date +%s)

    # Augment the dynamic linker path
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${R}/lib/R/lib:${readline}/lib
  '';
}

~/pip-test $ nix-shell shell4.nix

[nix-shell:~/pip-test]$ virtualenv venv
created virtual environment CPython3.9.6.final.0-64 in 131ms
  creator CPython3Posix(dest=/home/amy/pip-test/venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/amy/.local/share/virtualenv)
    added seed packages: peppercorn==0.6, pip==21.3.1, sampleproject==2.0.0, setuptools==58.3.0, wheel==0.37.0
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

[nix-shell:~/pip-test]$ source venv/bin/activate
(venv)
[nix-shell:~/pip-test]$ python -m pip install sampleproject
Requirement already satisfied: sampleproject in ./venv/lib/python3.9/site-packages (2.0.0)
Requirement already satisfied: peppercorn in ./venv/lib/python3.9/site-packages (from sampleproject) (0.6)
WARNING: You are using pip version 21.1.3; however, version 21.3.1 is available.
You should consider upgrading via the '/home/amy/pip-test/venv/bin/python -m pip install --upgrade pip' command.
(venv)

good that you got it working!

seems i was a little hasty and imprecise with the reply, sorry about that.

nix python is built around the idea to not use pip. everything should be a derivation that’s included in a python environment. there are ways around that, the most common these days seems to be to use poetry and poetry2nix. while it might seem convoluted there is a reason to that, using pip introduces many reproducibility issues into the packaging process and by default (as you’ve experienced) wants to write to global locations in the system.

that’s not to say you’re doing it wrong. if it works, it works! but the “clean”, nixy way would be to not use pip/venv.

1 Like

@pennae, your reply pointed me to the solution, thank you!

Yeah, the only reason that I’m using pip is that I’m trying to diagnose a problem with a NixOS package. I’m hoping that if I can build the software the way the developer intended (using pip), I can rule out an upstream issue. Then I can figure out what the (rather complex) default.nix in NixOS/nixpkgs is doing differently, and maybe fix it.

1 Like