2014-10-19 18:48:07 +01:00
|
|
|
{ stdenv, python, buildEnv, makeWrapper
|
|
|
|
, extraLibs ? []
|
2017-09-16 16:07:49 +01:00
|
|
|
, extraOutputsToInstall ? []
|
2014-10-19 18:48:07 +01:00
|
|
|
, postBuild ? ""
|
2017-05-28 08:20:47 +01:00
|
|
|
, ignoreCollisions ? false
|
2018-12-06 23:18:59 +00:00
|
|
|
, permitUserSite ? false
|
2017-05-28 08:20:47 +01:00
|
|
|
, requiredPythonModules
|
2018-10-13 09:56:51 +01:00
|
|
|
# Wrap executables with the given argument.
|
|
|
|
, makeWrapperArgs ? []
|
2017-05-28 08:20:47 +01:00
|
|
|
, }:
|
2012-02-26 17:23:22 +00:00
|
|
|
|
2013-08-15 08:52:25 +01:00
|
|
|
# Create a python executable that knows about additional packages.
|
2014-10-19 18:48:07 +01:00
|
|
|
let
|
2017-08-01 15:25:53 +01:00
|
|
|
env = let
|
2017-05-28 08:20:47 +01:00
|
|
|
paths = requiredPythonModules (extraLibs ++ [ python ] ) ;
|
2019-07-09 13:34:15 +01:00
|
|
|
pythonPath = "${placeholder "out"}/${python.sitePackages}";
|
2019-07-27 08:30:34 +01:00
|
|
|
pythonExecutable = "${placeholder "out"}/bin/${python.executable}";
|
2015-11-19 12:16:15 +00:00
|
|
|
in buildEnv {
|
|
|
|
name = "${python.name}-env";
|
2014-02-24 11:41:12 +00:00
|
|
|
|
2015-11-19 12:16:15 +00:00
|
|
|
inherit paths;
|
2017-12-10 14:21:02 +00:00
|
|
|
inherit ignoreCollisions;
|
|
|
|
extraOutputsToInstall = [ "out" ] ++ extraOutputsToInstall;
|
2010-08-16 18:03:35 +01:00
|
|
|
|
2015-08-14 12:06:15 +01:00
|
|
|
postBuild = ''
|
|
|
|
. "${makeWrapper}/nix-support/setup-hook"
|
2013-11-07 12:59:44 +00:00
|
|
|
|
2015-08-14 12:06:15 +01:00
|
|
|
if [ -L "$out/bin" ]; then
|
|
|
|
unlink "$out/bin"
|
|
|
|
fi
|
|
|
|
mkdir -p "$out/bin"
|
2013-11-07 12:59:44 +00:00
|
|
|
|
2015-11-19 12:16:15 +00:00
|
|
|
for path in ${stdenv.lib.concatStringsSep " " paths}; do
|
|
|
|
if [ -d "$path/bin" ]; then
|
|
|
|
cd "$path/bin"
|
|
|
|
for prg in *; do
|
|
|
|
if [ -f "$prg" ]; then
|
|
|
|
rm -f "$out/bin/$prg"
|
2017-08-09 14:06:03 +01:00
|
|
|
if [ -x "$prg" ]; then
|
Python: introduce NIX_PYTHONPREFIX in order to set site.PREFIXES
This is needed in case of `python.buildEnv` to make sure site.PREFIXES
does not only point to the unwrapped executable prefix.
--------------------------------------------------------------------------------
This PR is a story where your valiant hero sets out on a very simple adventure but ends up having to slay dragons, starts questioning his own sanity and finally manages to gain enough knowledge to slay the evil dragon and finally win the proverbial price.
It all started out on sunny spring day with trying to tackle the Nixops plugin infrastructure and make that nice enough to work with.
Our story begins in the shanty town of [NixOps-AWS](https://github.com/nixos/nixops-aws) where [mypy](http://mypy-lang.org/) type checking has not yet been seen.
As our deuteragonist (@grahamc) has made great strides in the capital city of [NixOps](https://github.com/nixos/nixops) our hero wanted to bring this out into the land and let the people rejoice in reliability and a wonderful development experience.
The plugin work itself was straight forward and our hero quickly slayed the first small dragon, at this point things felt good and our hero thought he was going to reach the town of NixOps-AWS very quickly.
But alas! Mypy did not want to go, it said:
`Cannot find implementation or library stub for module named 'nixops'`
Our hero felt a small sliver of life escape from his body. Things were not going to be so easy.
After some frustration our hero discovered there was a [rule of the land of Python](https://www.python.org/dev/peps/pep-0561/) that governed the import of types into the kingdom, more specificaly a very special document (file) called `py.typed`.
Things were looking good.
But no, what the law said did not seem to match reality. How could things be so?
After some frustrating debugging our valiant hero thought to himself "Hmm, I wonder if this is simply a Nix idiosyncrasy", and it turns out indeed it was.
Things that were working in the blessed way of the land of Python (inside a `virtualenv`) were not working the way they were from his home town of Nix (`nix-shell` + `python.withPackages`).
After even more frustrating attempts at reading the mypy documentation and trying to understand how things were supposed to work our hero started questioning his sanity.
This is where things started to get truly interesting.
Our hero started to use a number of powerful weapons, both forged in the land of Python (pdb) & by the mages of UNIX (printf-style-debugging & strace).
After first trying to slay the dragon simply by `strace` and a keen eye our hero did not spot any weak points.
Time to break out a more powerful sword (`pdb`) which also did not divulge any secrets about what was wrong.
Our hero went back to the `strace` output and after a fair bit of thought and analysis a pattern started to emerge. Mypy was looking in the wrong place (i.e. not in in the environment created by `python.withPackages` but in the interpreter store path) and our princess was in another castle!
Our hero went to the pub full of old grumpy men giving out the inner workings of the open source universe (Github) and acquired a copy of Mypy.
He littered the code with print statements & break points.
After a fierce battle full of blood, sweat & tears he ended up in https://github.com/python/mypy/blob/20f7f2dd71c21bde4d3d99f9ab69bf6670c7fa03/mypy/sitepkgs.py and realised that everything came down to the Python `site` module and more specifically https://docs.python.org/3.7/library/site.html#site.getsitepackages which in turn relies on https://docs.python.org/3.7/library/site.html#site.PREFIXES .
Our hero created a copy of the environment created by `python.withPackages` and manually modified it to confirm his findings, and it turned out it was indeed the case.
Our hero had damaged the dragon and it was time for a celebration.
He went out and acquired some mead which he ingested while he typed up his story and waited for the dragon to finally die (the commit caused a mass-rebuild, I had to wait for my repro).
In the end all was good in [NixOps-AWS](https://github.com/nixos/nixops-aws)-town and type checks could run. (PR for that incoming tomorrow).
2020-03-12 23:29:09 +00:00
|
|
|
makeWrapper "$path/bin/$prg" "$out/bin/$prg" --set NIX_PYTHONPREFIX "$out" --set NIX_PYTHONEXECUTABLE ${pythonExecutable} --set NIX_PYTHONPATH ${pythonPath} ${if permitUserSite then "" else ''--set PYTHONNOUSERSITE "true"''} ${stdenv.lib.concatStringsSep " " makeWrapperArgs}
|
2017-08-09 14:06:03 +01:00
|
|
|
fi
|
2015-11-19 12:16:15 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2015-08-14 12:06:15 +01:00
|
|
|
done
|
|
|
|
'' + postBuild;
|
|
|
|
|
2017-08-01 15:25:53 +01:00
|
|
|
inherit (python) meta;
|
|
|
|
|
|
|
|
passthru = python.passthru // {
|
|
|
|
interpreter = "${env}/bin/${python.executable}";
|
2017-08-02 14:05:00 +01:00
|
|
|
inherit python;
|
2017-08-01 15:25:53 +01:00
|
|
|
env = stdenv.mkDerivation {
|
|
|
|
name = "interactive-${python.name}-environment";
|
|
|
|
nativeBuildInputs = [ env ];
|
2015-08-14 12:06:15 +01:00
|
|
|
|
2017-08-01 15:25:53 +01:00
|
|
|
buildCommand = ''
|
|
|
|
echo >&2 ""
|
|
|
|
echo >&2 "*** Python 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
|
|
|
|
echo >&2 ""
|
|
|
|
exit 1
|
|
|
|
'';
|
|
|
|
};
|
2015-08-14 12:06:15 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
in env
|