How to package a Python wheel with a native extension

I have a derivation A that builds a python extension (using CMake and pybind11). I have another derivation B that uses buildPythonPackage to build a python pacakage.

How do I:

  1. Just create an output .whl file for B
  2. Make sure that the .whl file contains the native extension (shared library) from A?

A python package is basically a module installed into a site-packages directory. For packages in nixpkgs this means copying the resulting python module below $out/${python.sitePackages}/.

The buildPythonPackage function in its build step crease a wheel, only to unpack it in the next phase to install it into a nix store output. This means you can likely skip the build step (dontBuild) and overwrite the installPhase.

An example would be the openvino python package: nixpkgs/pkgs/development/python-modules/openvino/default.nix at 5e2119f033fee2e9f7ad30ff965cb1193ad3c095 · NixOS/nixpkgs · GitHub

That helps a bit, but using the installPhase that way the resulting nix package contains the files installed, not in a wheel. I can’t seem to figure out how to have buildPythonPackage output a wheel with the native extension.

The buildPythonPackage function does not generally output a wheel. It just creates one as an intermediate step.

But surely you can try locating that wheel e.g. in postBuild and copy it to $out in a custom installPhase.