How can I fix “[Errno 13] Permission denied: ‘_cmp.pyi'” in my Python nix flake?

I’m trying to install jupyter-book on NixOS. I have this flake:

{
  description = "Introduction to Computational Literary Analysis, a Textbook";

  outputs = { self, nixpkgs }: let
    system = "x86_64-linux";
    pkgs = import nixpkgs { inherit system; };
  in {
    jupyterBook = pkgs.callPackage ./jupyter-book.nix {};

    myPython = (nixpkgs.legacyPackages.${system}.python3.withPackages
      (ps: with ps; [
        # pip
        jupyter
        jupyterlab
        self.jupyterBook
        pandas
        nltk
        # spacy
      ]));

    defaultPackage.${system} = self.myPython;
  };
}

Which imports jupyter-book.nix, containing this expression:

{ python3Packages }:

let
  inherit (python3Packages) buildPythonPackage fetchPypi;
  attrs = buildPythonPackage rec {
    pname = "attrs";
    version = "20.3.0";
    src = fetchPypi {
      inherit pname; inherit version;
      sha256 = "007pchhxk2nh6m2rgflkkij9xjwysq3fl7xr72cy8i4pw76s6al3";
    };
    propagatedBuildInputs = with python3Packages; [
      pytest
    ];
  };
  click = buildPythonPackage rec {
    pname = "click";
    version = "7.1";
    src = fetchPypi {
      inherit pname; inherit version;
      sha256 = "1qk0x1bh6pmn2al9kq6cbgvm7pchnzl6ahf4zzpbjljl5lpmabs8";

    };
  };
  jupytext = buildPythonPackage rec {
    pname = "jupytext";
    version = "1.10.3";
    src = fetchPypi {
      inherit pname; inherit version;
      sha256 = "09f0ra3ndq4sxb0q3pjx713pfyc731y6fkzx93481mc7qm2cym5x";
    };
    propagatedBuildInputs = with python3Packages; [
      toml
      pyyaml
      markdown-it-py
    ];
  };
  markdown-it-py = buildPythonPackage rec {
    pname = "markdown-it-py";
    version = "0.6.0";
    src = fetchPypi {
      inherit pname; inherit version;
      sha256 = "0nb6i1hqlipbcpdd7kad26sfhwjxaqnd6md7piaslyzg77gi650w";
    };
    propagatedBuildInputs = with python3Packages; [
      attrs
    ];
  };
  sphinxExternalTOC = buildPythonPackage rec {
    # https://pypi.org/project/sphinx-comments/
    pname = "sphinx-external-toc";
    version = "0.2.2";
    src = fetchPypi {
      pname = "sphinx_external_toc";
      inherit version;
      sha256 = "a72c5861f670f1c7a1b92f2159fc9c7c5370e079cad1e3a7be4b269fa8048e8a";
    };
    propagatedBuildInputs = with python3Packages; [
      sphinx
      attrs
      click
      pyyaml
    ];
  };
  sphinxComments = buildPythonPackage rec {
    # https://pypi.org/project/sphinx-comments/
    pname = "sphinx-comments";
    version = "0.0.3";
    src = fetchPypi {
      inherit version; inherit pname;
      sha256 = "00170afff27019fad08e421da1ae49c681831fb2759786f07c826e89ac94cf21";
    };
    propagatedBuildInputs = with python3Packages; [
      sphinx
    ];
  };
in buildPythonPackage rec {
  pname = "jupyter-book";
  version = "0.11.2";
  src = fetchPypi {
    inherit version; inherit pname;
    sha256 = "e32298e03c19f514c745062891143693c5e001a098bae9d59d2e4434b2099c54";
  };
  propagatedBuildInputs = with python3Packages; [
    # click
    linkify-it-py
    sphinx
    sphinxComments
    sphinxExternalTOC
    jupytext
  ];
}

However, when I try to build it using nix build, or get a shell with it with nix shell, or use nix develop, I get this error:

❯ nix build
warning: Git tree '/home/jon/Code/book-computational-literary-analysis' is dirty
error: builder for '/nix/store/pp9074i7bmmvfmff4lkdmnj2wkj8vdaj-python3.8-attrs-20.3.0.drv' failed with exit code 1;
       last 10 log lines:
       > Executing pipInstallPhase
       > /build/attrs-20.3.0/dist /build/attrs-20.3.0
       > Processing ./attrs-20.3.0-py2.py3-none-any.whl
       > Installing collected packages: attrs
       >   Attempting uninstall: attrs
       >     Found existing installation: attrs 21.2.0
       >     Uninstalling attrs-21.2.0:
       > ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '_cmp.pyi'
       > Consider using the `--user` option or check the permissions.
       > 
       For full logs, run 'nix log /nix/store/pp9074i7bmmvfmff4lkdmnj2wkj8vdaj-python3.8-attrs-20.3.0.drv'.
error: 1 dependencies of derivation '/nix/store/liza7vix65mk26yc8lfxcsb1xl65ljrs-python3-3.8.9-env.drv' failed to build               

What’s happening here, and how can I fix it?

Answer

It looks like your attrs derivation wants to uninstall the attrs library it found via pytest dependencies. Maybe you can build it by removing pytest from your attrs inputs and disabling checks.

  attrs = buildPythonPackage rec {
    pname = "attrs";
    version = "20.3.0";
    src = fetchPypi {
      inherit pname; inherit version;
      sha256 = "007pchhxk2nh6m2rgflkkij9xjwysq3fl7xr72cy8i4pw76s6al3";
    };
    doCheck = false;
  };

The attrs package in Nixpkgs goes a step further by adding a tests.pytest attribute allowing to run the tests separately. Such complexity doesn’t seem worthwhile here.