Skip to content

Commit 7b88593

Browse files
committed
build.hooks: Add hook to install dist directory into output
`pyprojectDistHook` is to be used instead of `pyprojectHook` when you want to get the wheel files instead of the installed files.
1 parent 3ee4005 commit 7b88593

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

build/checks/default.nix

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,34 @@ listToAttrs (
250250
cffi = [ ];
251251
};
252252

253+
install-dist =
254+
let
255+
python = pkgs.python3;
256+
257+
pythonSet =
258+
(pkgs.callPackage pyproject-nix.build.packages {
259+
inherit python;
260+
}).overrideScope
261+
buildSystems;
262+
in
263+
pythonSet.build.override {
264+
pyprojectHook = pythonSet.pyprojectDistHook;
265+
};
266+
267+
install-dist-cross =
268+
let
269+
pkgs' = pkgs.pkgsCross.aarch64-multiplatform;
270+
271+
python = pkgs.python3;
272+
273+
pythonSet =
274+
(pkgs'.callPackage pyproject-nix.build.packages {
275+
inherit python;
276+
}).overrideScope
277+
buildSystems;
278+
in
279+
pythonSet.build.override {
280+
pyprojectHook = pythonSet.pyprojectDistHook;
281+
};
282+
253283
}

build/hooks/default.nix

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
stdenv,
88
hooks,
99
runCommand,
10+
substituteAll,
1011
}:
1112
let
1213
inherit (python) pythonOnBuildForHost;
@@ -272,4 +273,38 @@ in
272273
};
273274
} ./pypa-install-hook/pyproject-pypa-install-hook.sh
274275
) { };
276+
277+
/*
278+
Install dist directory with wheels into output
279+
.
280+
*/
281+
pyprojectInstallDistHook =
282+
callPackage
283+
(
284+
{ ugrep }:
285+
makeSetupHook {
286+
name = "pyproject-install-dist-hook";
287+
substitutions = {
288+
inherit pythonInterpreter;
289+
script = substituteAll {
290+
src = ./dist-hook/install-wheels.py;
291+
ugrep = lib.getExe ugrep;
292+
store_dir = builtins.storeDir;
293+
};
294+
};
295+
} ./dist-hook/pyproject-install-dist-hook.sh
296+
)
297+
{
298+
inherit (buildPackages) ugrep;
299+
};
300+
301+
/*
302+
Hook used to build a wheel file and install it into the output directory.
303+
304+
Use instead of pyprojectHook.
305+
*/
306+
pyprojectDistHook = hooks.pyprojectHook.override {
307+
pyprojectInstallHook = hooks.pyprojectInstallDistHook;
308+
pyprojectOutputSetupHook = null;
309+
};
275310
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
import os
3+
import shutil
4+
import subprocess
5+
from pathlib import Path
6+
7+
8+
def main():
9+
cwd = Path(os.getcwd())
10+
dist = cwd.joinpath("dist")
11+
12+
out = Path(os.environ["out"])
13+
14+
check_dist: bool
15+
try:
16+
check_dist = not bool(os.environ["dontUsePyprojectInstallDistCheck"])
17+
except KeyError:
18+
check_dist = True
19+
20+
wheels: list[Path] = [dist_file for dist_file in dist.iterdir() if dist_file.name.endswith(".whl")]
21+
22+
# Verify that wheel is not containing store path
23+
if check_dist:
24+
for wheel in wheels:
25+
p = subprocess.run(
26+
[
27+
"@ugrep@",
28+
# quiet
29+
"-q",
30+
# zip
31+
"-z",
32+
"@store_dir@",
33+
wheel,
34+
]
35+
)
36+
if p.returncode == 0:
37+
raise ValueError(f"""
38+
Built whheel '{wheel.name}' contains a Nix store path reference.
39+
40+
Wheel not usable for distribution.
41+
""")
42+
43+
# Copy wheels to output
44+
out.mkdir()
45+
for wheel in wheels:
46+
shutil.copy(wheel, out.joinpath(wheel.name))
47+
48+
49+
if __name__ == "__main__":
50+
main()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Setup hook for installing built wheels into output
2+
echo "Sourcing pyproject-install-dist-hook"
3+
4+
pyprojectInstallDistPhase() {
5+
echo "Executing pyprojectInstallDistPhase"
6+
runHook preInstall
7+
8+
@pythonInterpreter@ @script@
9+
10+
runHook postInstall
11+
echo "Finished executing pyprojectInstallDistPhase"
12+
}
13+
14+
if [ -z "${dontUsePyprojectInstallDist-}" ] && [ -z "${installPhase-}" ]; then
15+
echo "Using pyprojectInstallDistPhase"
16+
installPhase=pyprojectInstallDistPhase
17+
fi

build/packages.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ let
8686
pyprojectBuildEditableHook
8787
pyprojectFixupEditableHook
8888
pyprojectEditableHook
89+
pyprojectInstallDistHook
90+
pyprojectDistHook
91+
pyprojectPypaInstallHook
8992
;
9093
};
9194

0 commit comments

Comments
 (0)