nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 buildEnv,
5 runCommand,
6 makeBinaryWrapper,
7
8 # manually pased
9 python,
10 requiredPythonModules,
11
12 # extra opts
13 extraLibs ? [ ],
14 extraOutputsToInstall ? [ ],
15 postBuild ? "",
16 ignoreCollisions ? false,
17 permitUserSite ? false,
18 # Wrap executables with the given argument.
19 makeWrapperArgs ? [ ],
20}:
21
22# Create a python executable that knows about additional packages.
23let
24 env =
25 let
26 paths = requiredPythonModules (extraLibs ++ [ python ]) ++ [
27 (runCommand "bin" { } ''
28 mkdir -p $out/bin
29 '')
30 ];
31 pythonPath = "${placeholder "out"}/${python.sitePackages}";
32 pythonExecutable = "${placeholder "out"}/bin/${python.executable}";
33 in
34 buildEnv {
35 name = "${python.name}-env";
36
37 inherit paths;
38 inherit ignoreCollisions;
39 extraOutputsToInstall = [ "out" ] ++ extraOutputsToInstall;
40
41 nativeBuildInputs = [ makeBinaryWrapper ];
42
43 postBuild = ''
44 for path in ${lib.concatStringsSep " " paths}; do
45 if [ -d "$path/bin" ]; then
46 cd "$path/bin"
47 for prg in *; do
48 if [ -f "$prg" ] && [ -x "$prg" ]; then
49 rm -f "$out/bin/$prg"
50 if [ "$prg" = "${python.executable}" ]; then
51 makeWrapper "${python.interpreter}" "$out/bin/$prg" \
52 --inherit-argv0 \
53 --resolve-argv0 \
54 ${lib.optionalString (!permitUserSite) ''--set PYTHONNOUSERSITE "true"''} \
55 ${lib.concatStringsSep " " makeWrapperArgs}
56 elif [ "$(readlink "$prg")" = "${python.executable}" ]; then
57 ln -s "${python.executable}" "$out/bin/$prg"
58 else
59 makeWrapper "$path/bin/$prg" "$out/bin/$prg" \
60 --set NIX_PYTHONPREFIX "$out" \
61 --set NIX_PYTHONEXECUTABLE ${pythonExecutable} \
62 --set NIX_PYTHONPATH ${pythonPath} \
63 ${lib.optionalString (!permitUserSite) ''--set PYTHONNOUSERSITE "true"''} \
64 ${lib.concatStringsSep " " makeWrapperArgs}
65 fi
66 fi
67 done
68 fi
69 done
70 ''
71 + postBuild;
72
73 inherit (python) meta;
74
75 passthru = python.passthru // {
76 interpreter = "${env}/bin/${python.executable}";
77 inherit python;
78 env = stdenv.mkDerivation {
79 name = "interactive-${python.name}-environment";
80 nativeBuildInputs = [ env ];
81
82 buildCommand = ''
83 echo >&2 ""
84 echo >&2 "*** Python 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
85 echo >&2 ""
86 exit 1
87 '';
88 };
89 };
90 };
91in
92env