Python package works in a virtualenv, but can't install centrally

Hello all - could you help a newbie?
I’m using nix-darwin and have python3 in my systemPackages. Excerpt from my darwin-configuration.nix:

   (python3.withPackages(ps: with ps; [
        black
        pip
        virtualenv
      ]))

This works fine.

If I try to add the ‘img2pdf’ package,to the above list, I get errors (from the tests) as it tries to build it (details below). If, on the other hand, I create a virtualenv and pip install img2pdf, it installs just fine from the wheel. But I don’t want it tucked away in a virtualenv, I want it installed centrally.

I’m trying to work out whether this is an issue with the img2pdf package (that it doesn’t build cleanly on my M1 Mac), or with the way it’s installed by Nix, or if there’s a way I can install it that better imitates my pip install and uses the wheel rather than trying to rebuild it. Can anyone help?

There’s also a top-level img2pdf in nixpkgs, but it fails in exactly the same way.

Many thanks!
Quentin

Here’s the log when I try a darwin-rebuild switch with img2pdf included:


$ darwin-rebuild switch
building the system configuration...
these 5 derivations will be built:
  /nix/store/j6l987bi5ibzjp7838gmcrqnbp19123g-python3.11-img2pdf-0.5.1.drv
  /nix/store/bp29x144r64k3adzzd21f68l9zkgmzlx-python3-3.11.6-env.drv
  /nix/store/idr9z10khbg68vifjsich335yx2b75dc-system-applications.drv
  /nix/store/qi0zaiv6qhx8gyjg9q89wqv5y4q5mbq6-system-path.drv
  /nix/store/mrilk469npixvnkw8papf804lk1af4sp-darwin-system-24.05pre565199.63143ac2c918+darwin4.drv
building '/nix/store/j6l987bi5ibzjp7838gmcrqnbp19123g-python3.11-img2pdf-0.5.1.drv'...
Sourcing python-remove-tests-dir-hook
Sourcing python-catch-conflicts-hook.sh
Sourcing python-remove-bin-bytecode-hook.sh
Sourcing pypa-build-hook
Using pypaBuildPhase
Sourcing pypa-install-hook
Using pypaInstallPhase
Sourcing python-imports-check-hook.sh
Using pythonImportsCheckPhase
Sourcing python-namespaces-hook
Sourcing python-catch-conflicts-hook.sh
Sourcing pytest-check-hook
Using pytestCheckPhase
Running phase: unpackPhase
unpacking source archive /nix/store/y8h81m89lw4ajvhzw06jk5fd2rjaail6-source
source root is source
setting SOURCE_DATE_EPOCH to timestamp 315619200 of file source/tox.ini
Running phase: patchPhase
applying patch /nix/store/falnjid8jpwqpvvisj1rh6y9k0lr71x8-default-icc-profile.patch
patching file src/img2pdf.py
applying patch /nix/store/mizqi7msrjvbw68rks0ir4bnr6privn0-fix_tests.patch
patching file src/img2pdf_test.py
Running phase: updateAutotoolsGnuConfigScriptsPhase
Running phase: configurePhase
no configure script, doing nothing
Running phase: buildPhase
Executing pypaBuildPhase
Creating a wheel...
* Getting build dependencies for wheel...
running egg_info
creating src/img2pdf.egg-info
writing src/img2pdf.egg-info/PKG-INFO
writing dependency_links to src/img2pdf.egg-info/dependency_links.txt
writing entry points to src/img2pdf.egg-info/entry_points.txt
writing requirements to src/img2pdf.egg-info/requires.txt
writing top-level names to src/img2pdf.egg-info/top_level.txt
writing manifest file 'src/img2pdf.egg-info/SOURCES.txt'
reading manifest file 'src/img2pdf.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'test.sh'
warning: no files found matching 'magick.py'
adding license file 'LICENSE'
writing manifest file 'src/img2pdf.egg-info/SOURCES.txt'
* Building wheel...
running bdist_wheel
running build
running build_py
creating build
creating build/lib
copying src/img2pdf.py -> build/lib
copying src/jp2.py -> build/lib
running egg_info
writing src/img2pdf.egg-info/PKG-INFO
writing dependency_links to src/img2pdf.egg-info/dependency_links.txt
writing entry points to src/img2pdf.egg-info/entry_points.txt
writing requirements to src/img2pdf.egg-info/requires.txt
writing top-level names to src/img2pdf.egg-info/top_level.txt
reading manifest file 'src/img2pdf.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'test.sh'
warning: no files found matching 'magick.py'
adding license file 'LICENSE'
writing manifest file 'src/img2pdf.egg-info/SOURCES.txt'
installing to build/bdist.macosx-11.0-arm64/wheel
running install
running install_lib
creating build/bdist.macosx-11.0-arm64
creating build/bdist.macosx-11.0-arm64/wheel
copying build/lib/jp2.py -> build/bdist.macosx-11.0-arm64/wheel
copying build/lib/img2pdf.py -> build/bdist.macosx-11.0-arm64/wheel
running install_egg_info
Copying src/img2pdf.egg-info to build/bdist.macosx-11.0-arm64/wheel/img2pdf-0.5.1-py3.11.egg-info
running install_scripts
creating build/bdist.macosx-11.0-arm64/wheel/img2pdf-0.5.1.dist-info/WHEEL
creating '/private/tmp/nix-build-python3.11-img2pdf-0.5.1.drv-0/source/dist/.tmp-gpdqj5cq/img2pdf-0.5.1-py3-none-any.whl' and adding 'build/bdist.macosx-11.0-arm64/wheel' to it
adding 'img2pdf.py'
adding 'jp2.py'
adding 'img2pdf-0.5.1.dist-info/LICENSE'
adding 'img2pdf-0.5.1.dist-info/METADATA'
adding 'img2pdf-0.5.1.dist-info/WHEEL'
adding 'img2pdf-0.5.1.dist-info/entry_points.txt'
adding 'img2pdf-0.5.1.dist-info/top_level.txt'
adding 'img2pdf-0.5.1.dist-info/zip-safe'
adding 'img2pdf-0.5.1.dist-info/RECORD'
removing build/bdist.macosx-11.0-arm64/wheel
Successfully built img2pdf-0.5.1-py3-none-any.whl
Finished creating a wheel...
Finished executing pypaBuildPhase
Running phase: glibPreInstallPhase
Running phase: installPhase
Executing pypaInstallPhase
Successfully installed img2pdf-0.5.1-py3-none-any.whl
Finished executing pypaInstallPhase
Running phase: pythonOutputDistPhase
Executing pythonOutputDistPhase
Finished executing pythonOutputDistPhase
Running phase: glibPreFixupPhase
Running phase: fixupPhase
checking for references to /private/tmp/nix-build-python3.11-img2pdf-0.5.1.drv-0/ in /nix/store/7xp3a3dwjlbjg9nryvgfk4w1aav6dw09-python3.11-img2pdf-0.5.1...
patching script interpreter paths in /nix/store/7xp3a3dwjlbjg9nryvgfk4w1aav6dw09-python3.11-img2pdf-0.5.1
stripping (with command strip and flags -S) in  /nix/store/7xp3a3dwjlbjg9nryvgfk4w1aav6dw09-python3.11-img2pdf-0.5.1/lib /nix/store/7xp3a3dwjlbjg9nryvgfk4w1aav6dw09-python3.11-img2pdf-0.5.1/bin
checking for references to /private/tmp/nix-build-python3.11-img2pdf-0.5.1.drv-0/ in /nix/store/71jpxvlsjhp7j3h5dsb1w7k7gg90ybbl-python3.11-img2pdf-0.5.1-dist...
patching script interpreter paths in /nix/store/71jpxvlsjhp7j3h5dsb1w7k7gg90ybbl-python3.11-img2pdf-0.5.1-dist
Rewriting #!/nix/store/z0nnwdh4p692yvf3qgwgj3bn78w99q6y-python3-3.11.6/bin/python3.11 to #!/nix/store/z0nnwdh4p692yvf3qgwgj3bn78w99q6y-python3-3.11.6
wrapping `/nix/store/7xp3a3dwjlbjg9nryvgfk4w1aav6dw09-python3.11-img2pdf-0.5.1/bin/img2pdf-gui'...
Rewriting #!/nix/store/z0nnwdh4p692yvf3qgwgj3bn78w99q6y-python3-3.11.6/bin/python3.11 to #!/nix/store/z0nnwdh4p692yvf3qgwgj3bn78w99q6y-python3-3.11.6
wrapping `/nix/store/7xp3a3dwjlbjg9nryvgfk4w1aav6dw09-python3.11-img2pdf-0.5.1/bin/img2pdf'...
Executing pythonRemoveTestsDir
Finished executing pythonRemoveTestsDir
Running phase: installCheckPhase
no Makefile or custom installCheckPhase, doing nothing
Running phase: pythonCatchConflictsPhase
Running phase: pythonRemoveBinBytecodePhase
Running phase: pythonImportsCheckPhase
Executing pythonImportsCheckPhase
Check whether the following modules can be imported: img2pdf
Running phase: pytestCheckPhase
Executing pytestCheckPhase
============================= test session starts ==============================
platform darwin -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0
rootdir: /private/tmp/nix-build-python3.11-img2pdf-0.5.1.drv-0/source
collected 0 items / 1 error

==================================== ERRORS ====================================
_____________________ ERROR collecting src/img2pdf_test.py _____________________
/nix/store/jqhhxyh2450cb2ppg389d2psrqp7zdjd-python3.11-pytest-7.4.3/lib/python3.11/site-packages/_pytest/runner.py:341: in from_call
    result: Optional[TResult] = func()
/nix/store/jqhhxyh2450cb2ppg389d2psrqp7zdjd-python3.11-pytest-7.4.3/lib/python3.11/site-packages/_pytest/runner.py:372: in <lambda>
    call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
/nix/store/jqhhxyh2450cb2ppg389d2psrqp7zdjd-python3.11-pytest-7.4.3/lib/python3.11/site-packages/_pytest/python.py:531: in collect
    self._inject_setup_module_fixture()
/nix/store/jqhhxyh2450cb2ppg389d2psrqp7zdjd-python3.11-pytest-7.4.3/lib/python3.11/site-packages/_pytest/python.py:545: in _inject_setup_module_fixture
    self.obj, ("setUpModule", "setup_module")
/nix/store/jqhhxyh2450cb2ppg389d2psrqp7zdjd-python3.11-pytest-7.4.3/lib/python3.11/site-packages/_pytest/python.py:310: in obj
    self._obj = obj = self._getobj()
/nix/store/jqhhxyh2450cb2ppg389d2psrqp7zdjd-python3.11-pytest-7.4.3/lib/python3.11/site-packages/_pytest/python.py:528: in _getobj
    return self._importtestmodule()
/nix/store/jqhhxyh2450cb2ppg389d2psrqp7zdjd-python3.11-pytest-7.4.3/lib/python3.11/site-packages/_pytest/python.py:617: in _importtestmodule
    mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
/nix/store/jqhhxyh2450cb2ppg389d2psrqp7zdjd-python3.11-pytest-7.4.3/lib/python3.11/site-packages/_pytest/pathlib.py:567: in import_path
    importlib.import_module(module_name)
/nix/store/z0nnwdh4p692yvf3qgwgj3bn78w99q6y-python3-3.11.6/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1204: in _gcd_import
    ???
<frozen importlib._bootstrap>:1176: in _find_and_load
    ???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:690: in _load_unlocked
    ???
/nix/store/jqhhxyh2450cb2ppg389d2psrqp7zdjd-python3.11-pytest-7.4.3/lib/python3.11/site-packages/_pytest/assertion/rewrite.py:186: in exec_module
    exec(co, module.__dict__)
src/img2pdf_test.py:52: in <module>
    ver = subprocess.check_output(["mutool", "-v"], stderr=subprocess.STDOUT)
/nix/store/z0nnwdh4p692yvf3qgwgj3bn78w99q6y-python3-3.11.6/lib/python3.11/subprocess.py:466: in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
/nix/store/z0nnwdh4p692yvf3qgwgj3bn78w99q6y-python3-3.11.6/lib/python3.11/subprocess.py:571: in run
    raise CalledProcessError(retcode, process.args,
E   subprocess.CalledProcessError: Command '['mutool', '-v']' died with <Signals.SIGABRT: 6>.
=========================== short test summary info ============================
ERROR src/img2pdf_test.py - subprocess.CalledProcessError: Command '['mutool', '-v']' died with <Signal...
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 1.61s ===============================
/nix/store/sf52i9wcklk5i5f2w15p0kng8dq3qqwx-stdenv-darwin/setup: line 1562: pop_var_context: head of shell_variables not a function context
error: builder for '/nix/store/j6l987bi5ibzjp7838gmcrqnbp19123g-python3.11-img2pdf-0.5.1.drv' failed with exit code 2;
       last 10 log lines:
       > /nix/store/z0nnwdh4p692yvf3qgwgj3bn78w99q6y-python3-3.11.6/lib/python3.11/subprocess.py:466: in check_output
       >     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
       > /nix/store/z0nnwdh4p692yvf3qgwgj3bn78w99q6y-python3-3.11.6/lib/python3.11/subprocess.py:571: in run
       >     raise CalledProcessError(retcode, process.args,
       > E   subprocess.CalledProcessError: Command '['mutool', '-v']' died with <Signals.SIGABRT: 6>.
       > =========================== short test summary info ============================
       > ERROR src/img2pdf_test.py - subprocess.CalledProcessError: Command '['mutool', '-v']' died with <Signal...
       > !!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
       > =============================== 1 error in 1.61s ===============================
       > /nix/store/sf52i9wcklk5i5f2w15p0kng8dq3qqwx-stdenv-darwin/setup: line 1562: pop_var_context: head of shell_variables not a function context
       For full logs, run 'nix log /nix/store/j6l987bi5ibzjp7838gmcrqnbp19123g-python3.11-img2pdf-0.5.1.drv'.
error: 1 dependencies of derivation '/nix/store/bp29x144r64k3adzzd21f68l9zkgmzlx-python3-3.11.6-env.drv' failed to build
error: 1 dependencies of derivation '/nix/store/idr9z10khbg68vifjsich335yx2b75dc-system-applications.drv' failed to build
error: 1 dependencies of derivation '/nix/store/mrilk469npixvnkw8papf804lk1af4sp-darwin-system-24.05pre565199.63143ac2c918+darwin4.drv' failed to build