Skip to content

Commit

Permalink
build: Introduce & use pyprojectPypaBuildHook for cross builds
Browse files Browse the repository at this point in the history
The way uv invokes it's environment scripts with `-I` (isolated mode) is incompatible with how nixpkgs sets up cross builds.
Nixpkgs relies on sysconfig coming through PYTHONPATH, which is ignored in isolated mode.
  • Loading branch information
adisbladis committed Nov 20, 2024
1 parent 339d745 commit eb94196
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build/checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ in
}).overrideScope
buildSystems;
in
crossSet.pythonPkgsHostHost.mkVirtualEnv "cross-venv" {
crossSet.mkVirtualEnv "cross-venv" {
build = [ ];
cffi = [ ];
};

prebuilt-wheel = pythonSet.pythonPkgsHostHost.callPackage (
Expand Down
27 changes: 27 additions & 0 deletions build/hooks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
lib,
stdenv,
hooks,
resolveBuildSystem,
pythonPkgsBuildHost,
}:
let
inherit (python) pythonOnBuildForHost isPy3k;
Expand Down Expand Up @@ -58,6 +60,30 @@ in
inherit (buildPackages) uv;
};

/*
Build a pyproject.toml/setuptools project.
Used internally by `pyprojectHook`.
*/
pyprojectPypaBuildHook =
callPackage
(
_:
makeSetupHook {
name = "pyproject-pypa-build-hook";
substitutions = {
inherit (pythonPkgsBuildHost) build;
inherit pythonInterpreter;
};
propagatedBuildInputs = pythonPkgsBuildHost.resolveBuildSystem {
build = [ ];
};
} ./pyproject-pypa-build-hook.sh
)
{
inherit (buildPackages) python;
};

/*
Symlink prebuilt wheel sources.
Expand Down Expand Up @@ -198,6 +224,7 @@ in
// (lib.optionalAttrs (stdenv.buildPlatform != stdenv.hostPlatform) {
# Uv is not yet compatible with cross installs, or at least I can't figure out the magic incantation.
# We can use installer for cross, and still use uv for native.
pyprojectBuildHook = hooks.pyprojectPypaBuildHook;
pyprojectInstallHook = hooks.pyprojectPypaInstallHook;
})
);
Expand Down
18 changes: 18 additions & 0 deletions build/hooks/pyproject-pypa-build-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Setup hook to use for PEP-621/setuptools builds
echo "Sourcing pyproject-build-hook"

pyprojectPypaBuildPhase() {
echo "Executing pyprojectPypaBuildPhase"
runHook preBuild

echo "Creating a wheel..."
env PYTHONPATH="${NIX_PYPROJECT_PYTHONPATH}:${PYTHONPATH}" @build@/bin/pyproject-build --no-isolation --outdir dist/ --wheel $pypaBuildFlags

runHook postBuild
echo "Finished executing pyprojectPypaBuildPhase"
}

if [ -z "${dontUsePyprojectPypaBuild-}" ] && [ -z "${buildPhase-}" ]; then
echo "Using pyprojectPypaBuildPhase"
buildPhase=pyprojectPypaBuildPhase
fi

0 comments on commit eb94196

Please sign in to comment.