1# This function provides specific bits for building a setuptools-based Python package.
2
3{ lib
4, python
5, bootstrapped-pip
6}:
7
8{
9# passed to "python setup.py build_ext"
10# https://github.com/pypa/pip/issues/881
11 setupPyBuildFlags ? []
12# Execute before shell hook
13, preShellHook ? ""
14# Execute after shell hook
15, postShellHook ? ""
16, ... } @ attrs:
17
18let
19 # use setuptools shim (so that setuptools is imported before distutils)
20 # pip does the same thing: https://github.com/pypa/pip/pull/3265
21 setuppy = ./run_setup.py;
22
23in attrs // {
24 # we copy nix_run_setup.py over so it's executed relative to the root of the source
25 # many project make that assumption
26 buildPhase = attrs.buildPhase or ''
27 runHook preBuild
28 cp ${setuppy} nix_run_setup.py
29 ${python.interpreter} nix_run_setup.py ${lib.optionalString (setupPyBuildFlags != []) ("build_ext " + (lib.concatStringsSep " " setupPyBuildFlags))} bdist_wheel
30 runHook postBuild
31 '';
32
33 installCheckPhase = attrs.checkPhase or ''
34 runHook preCheck
35 ${python.interpreter} nix_run_setup.py test
36 runHook postCheck
37 '';
38
39 # Python packages that are installed with setuptools
40 # are typically distributed with tests.
41 # With Python it's a common idiom to run the tests
42 # after the software has been installed.
43 doCheck = attrs.doCheck or true;
44
45 shellHook = attrs.shellHook or ''
46 ${preShellHook}
47 if test -e setup.py; then
48 tmp_path=$(mktemp -d)
49 export PATH="$tmp_path/bin:$PATH"
50 export PYTHONPATH="$tmp_path/${python.sitePackages}:$PYTHONPATH"
51 mkdir -p $tmp_path/${python.sitePackages}
52 ${bootstrapped-pip}/bin/pip install -e . --prefix $tmp_path >&2
53 fi
54 ${postShellHook}
55 '';
56}