From 344e5221664a8c232c3b2b0c7667dcd4c229834f Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Fri, 14 Aug 2015 14:06:15 +0300 Subject: [PATCH] python: add .env for convenient nix-shell's --- doc/language-support.xml | 21 ++++++++ .../interpreters/python/wrapper.nix | 54 +++++++++++-------- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/doc/language-support.xml b/doc/language-support.xml index 9117af864ad..a969111ed77 100644 --- a/doc/language-support.xml +++ b/doc/language-support.xml @@ -464,6 +464,27 @@ python.buildEnv.override { with wrapped binaries in bin/. + + You can also use env attribute to create local + environments with needed packages installed (somewhat comparable to + virtualenv). For example, with the following + shell.nix: + + + {}; + +(python3.buildEnv.override { + extraLibs = with python3Packages; + [ numpy + requests + ]; +}).env]]> + + + Running nix-shell will drop you into a shell where + python will have specified packages in its path. + + <function>python.buildEnv</function> arguments diff --git a/pkgs/development/interpreters/python/wrapper.nix b/pkgs/development/interpreters/python/wrapper.nix index 163e8d7194b..f757147b047 100644 --- a/pkgs/development/interpreters/python/wrapper.nix +++ b/pkgs/development/interpreters/python/wrapper.nix @@ -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