Skip to content

Commit

Permalink
build.hooks: Add hook to install dist directory into output
Browse files Browse the repository at this point in the history
`pyprojectDistHook` is to be used instead of `pyprojectHook` when you want to get the wheel files instead of the installed files.
  • Loading branch information
adisbladis committed Feb 13, 2025
1 parent 3ee4005 commit 7b88593
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
30 changes: 30 additions & 0 deletions build/checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,34 @@ listToAttrs (
cffi = [ ];
};

install-dist =
let
python = pkgs.python3;

pythonSet =
(pkgs.callPackage pyproject-nix.build.packages {
inherit python;
}).overrideScope
buildSystems;
in
pythonSet.build.override {
pyprojectHook = pythonSet.pyprojectDistHook;
};

install-dist-cross =
let
pkgs' = pkgs.pkgsCross.aarch64-multiplatform;

python = pkgs.python3;

pythonSet =
(pkgs'.callPackage pyproject-nix.build.packages {
inherit python;
}).overrideScope
buildSystems;
in
pythonSet.build.override {
pyprojectHook = pythonSet.pyprojectDistHook;
};

}
35 changes: 35 additions & 0 deletions build/hooks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
stdenv,
hooks,
runCommand,
substituteAll,
}:
let
inherit (python) pythonOnBuildForHost;
Expand Down Expand Up @@ -272,4 +273,38 @@ in
};
} ./pypa-install-hook/pyproject-pypa-install-hook.sh
) { };

/*
Install dist directory with wheels into output
.
*/
pyprojectInstallDistHook =
callPackage
(
{ ugrep }:
makeSetupHook {
name = "pyproject-install-dist-hook";
substitutions = {
inherit pythonInterpreter;
script = substituteAll {
src = ./dist-hook/install-wheels.py;
ugrep = lib.getExe ugrep;
store_dir = builtins.storeDir;
};
};
} ./dist-hook/pyproject-install-dist-hook.sh
)
{
inherit (buildPackages) ugrep;
};

/*
Hook used to build a wheel file and install it into the output directory.
Use instead of pyprojectHook.
*/
pyprojectDistHook = hooks.pyprojectHook.override {
pyprojectInstallHook = hooks.pyprojectInstallDistHook;
pyprojectOutputSetupHook = null;
};
}
50 changes: 50 additions & 0 deletions build/hooks/dist-hook/install-wheels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python
import os
import shutil
import subprocess
from pathlib import Path


def main():
cwd = Path(os.getcwd())
dist = cwd.joinpath("dist")

out = Path(os.environ["out"])

check_dist: bool
try:
check_dist = not bool(os.environ["dontUsePyprojectInstallDistCheck"])
except KeyError:
check_dist = True

wheels: list[Path] = [dist_file for dist_file in dist.iterdir() if dist_file.name.endswith(".whl")]

# Verify that wheel is not containing store path
if check_dist:
for wheel in wheels:
p = subprocess.run(
[
"@ugrep@",
# quiet
"-q",
# zip
"-z",
"@store_dir@",
wheel,
]
)
if p.returncode == 0:
raise ValueError(f"""
Built whheel '{wheel.name}' contains a Nix store path reference.
Wheel not usable for distribution.
""")

# Copy wheels to output
out.mkdir()
for wheel in wheels:
shutil.copy(wheel, out.joinpath(wheel.name))


if __name__ == "__main__":
main()
17 changes: 17 additions & 0 deletions build/hooks/dist-hook/pyproject-install-dist-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Setup hook for installing built wheels into output
echo "Sourcing pyproject-install-dist-hook"

pyprojectInstallDistPhase() {
echo "Executing pyprojectInstallDistPhase"
runHook preInstall

@pythonInterpreter@ @script@

runHook postInstall
echo "Finished executing pyprojectInstallDistPhase"
}

if [ -z "${dontUsePyprojectInstallDist-}" ] && [ -z "${installPhase-}" ]; then
echo "Using pyprojectInstallDistPhase"
installPhase=pyprojectInstallDistPhase
fi
3 changes: 3 additions & 0 deletions build/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ let
pyprojectBuildEditableHook
pyprojectFixupEditableHook
pyprojectEditableHook
pyprojectInstallDistHook
pyprojectDistHook
pyprojectPypaInstallHook
;
};

Expand Down

0 comments on commit 7b88593

Please sign in to comment.