python: add .env for convenient nix-shell's

This commit is contained in:
Nikolay Amiantov 2015-08-14 14:06:15 +03:00
parent 0ccf8eae2f
commit 344e522166
2 changed files with 54 additions and 21 deletions

View File

@ -464,6 +464,27 @@ python.buildEnv.override {
with wrapped binaries in <filename>bin/</filename>.
</para>
<para>
You can also use <varname>env</varname> attribute to create local
environments with needed packages installed (somewhat comparable to
<literal>virtualenv</literal>). For example, with the following
<filename>shell.nix</filename>:
<programlisting language="nix">
<![CDATA[with import <nixpkgs> {};
(python3.buildEnv.override {
extraLibs = with python3Packages;
[ numpy
requests
];
}).env]]>
</programlisting>
Running <command>nix-shell</command> will drop you into a shell where
<command>python</command> will have specified packages in its path.
</para>
<variablelist>
<title>
<function>python.buildEnv</function> arguments

View File

@ -6,28 +6,40 @@
# Create a python executable that knows about additional packages.
let
recursivePthLoader = import ../../python-modules/recursive-pth-loader/default.nix { stdenv = stdenv; python = python; };
in
(buildEnv {
name = "${python.name}-env";
paths = stdenv.lib.filter (x : x ? pythonPath) (stdenv.lib.closePropagation extraLibs) ++ [ python recursivePthLoader ];
env = (buildEnv {
name = "${python.name}-env";
paths = stdenv.lib.filter (x : x ? pythonPath) (stdenv.lib.closePropagation extraLibs) ++ [ python recursivePthLoader ];
inherit ignoreCollisions;
inherit ignoreCollisions;
postBuild = ''
. "${makeWrapper}/nix-support/setup-hook"
postBuild = ''
. "${makeWrapper}/nix-support/setup-hook"
if [ -L "$out/bin" ]; then
unlink "$out/bin"
fi
mkdir -p "$out/bin"
if [ -L "$out/bin" ]; then
unlink "$out/bin"
fi
mkdir -p "$out/bin"
cd "${python}/bin"
for prg in *; do
rm -f "$out/bin/$prg"
makeWrapper "${python}/bin/$prg" "$out/bin/$prg" --set PYTHONHOME "$out"
done
'' + postBuild;
}) // {
inherit python;
inherit (python) meta;
}
cd "${python}/bin"
for prg in *; do
rm -f "$out/bin/$prg"
makeWrapper "${python}/bin/$prg" "$out/bin/$prg" --set PYTHONHOME "$out"
done
'' + postBuild;
passthru.env = stdenv.mkDerivation {
name = "interactive-${python.name}-environment";
nativeBuildInputs = [ env ];
buildCommand = ''
echo >&2 ""
echo >&2 "*** Python 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
echo >&2 ""
exit 1
'';
};
}) // {
inherit python;
inherit (python) meta;
};
in env