How to build Python package *for use in non-Nix environments?*

There are plenty of ways to create Nix derivations for Python packages, but I haven’t seen any yet which create something equivalent to ./setup.py sdist bdist_wheel or poetry build which can then be uploaded to PyPI.

2 Likes

With buildPython*: store dist (wheel/sdist) in dist output by FRidh · Pull Request #190487 · NixOS/nixpkgs · GitHub a package build will output a dist folder containing a wheel. Note however, that if it contains extension modules, that these will have absolute paths to nix libraries.

I suppose one could make an FHS builder.

2 Likes

Fantastic! Based on the code it looks like that expects there to already be a dist directory. What generates that, and can I do that easily (aka without waiting for your PR to reach stable)? When running nix-build in my project the result directory doesn’t have any dist folder.

pip builds a wheel. The wheel we then install in $out, and without that PR we discard the wheel.

That makes sense; thanks for the explanation! Do you know what the situation for creating sdist releases is like? I don’t really mind only releasing the wheel, but it seems to be a Python norm to release both source and wheel packages.

Yes, it’s good to also release an sdist. I use the following in case I am using flit as backend

Excellent, but the sdist doesn’t end up in the result folder with this approach:

$ ls -R result/
result/:
bash-completion  bin  lib  nix-support

result/bash-completion:
vcard

result/bin:
vcard

result/lib:
python3.10

result/lib/python3.10:
site-packages

result/lib/python3.10/site-packages:
vcard  vcard-0.15.0.dist-info

result/lib/python3.10/site-packages/vcard:
__init__.py  __pycache__  vcard_definitions.py  vcard_errors.py  vcard_property.py  vcard.py  vcard_utils.py  vcard_validator.py  vcard_validators.py

result/lib/python3.10/site-packages/vcard/__pycache__:
__init__.cpython-310.pyc  vcard_definitions.cpython-310.pyc  vcard_property.cpython-310.pyc  vcard_validator.cpython-310.pyc
vcard.cpython-310.pyc     vcard_errors.cpython-310.pyc       vcard_utils.cpython-310.pyc     vcard_validators.cpython-310.pyc

result/lib/python3.10/site-packages/vcard-0.15.0.dist-info:
direct_url.json  entry_points.txt  INSTALLER  LICENSE.txt  METADATA  RECORD  REQUESTED  top_level.txt  WHEEL

result/nix-support:
propagated-build-inputs

(buildPythonPackage also does something weird, including a whole bunch of files not in the source tarball (and explicitly excluded in MANIFEST.in) in the result/lib/python3.10/site-packages/vcard folder. It’s as if it’s ignoring the src completely.)