From eba1f794184f6bc08558a6506ed62eada008f37e Mon Sep 17 00:00:00 2001 From: Frederik Rietdijk Date: Mon, 13 Jan 2020 21:45:24 +0100 Subject: pythonPackages.venvShellHook: init This is a hook that loads a virtualenv from the specified `venvDir` location. If the virtualenv does not exist, it is created. --- doc/languages-frameworks/python.section.md | 1 + .../interpreters/python/hooks/default.nix | 12 ++++++++++ .../interpreters/python/hooks/venv-shell-hook.sh | 26 ++++++++++++++++++++++ pkgs/top-level/python-packages.nix | 2 +- 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 pkgs/development/interpreters/python/hooks/venv-shell-hook.sh diff --git a/doc/languages-frameworks/python.section.md b/doc/languages-frameworks/python.section.md index 199e678bc29..bbcf82f7ed6 100644 --- a/doc/languages-frameworks/python.section.md +++ b/doc/languages-frameworks/python.section.md @@ -833,6 +833,7 @@ used in `buildPythonPackage`. - `pythonRemoveBinBytecode` to remove bytecode from the `/bin` folder. - `setuptoolsBuildHook` to build a wheel using `setuptools`. - `setuptoolsCheckHook` to run tests with `python setup.py test`. +- `venvShellHook` to source a Python 3 `venv` at the `venvDir` location. A `venv` is created if it does not yet exist. - `wheelUnpackHook` to move a wheel to the correct folder so it can be installed with the `pipInstallHook`. ### Development mode diff --git a/pkgs/development/interpreters/python/hooks/default.nix b/pkgs/development/interpreters/python/hooks/default.nix index f05b3b1eec6..159637ae9d5 100644 --- a/pkgs/development/interpreters/python/hooks/default.nix +++ b/pkgs/development/interpreters/python/hooks/default.nix @@ -2,6 +2,9 @@ { python , callPackage , makeSetupHook +, disabledIf +, isPy3k +, ensureNewerSourcesForZipFilesHook }: let @@ -109,6 +112,15 @@ in rec { }; } ./setuptools-check-hook.sh) {}; + venvShellHook = disabledIf (!isPy3k) (callPackage ({ }: + makeSetupHook { + name = "venv-shell-hook"; + deps = [ ensureNewerSourcesForZipFilesHook ]; + substitutions = { + inherit pythonInterpreter; + }; + } ./venv-shell-hook.sh) {}); + wheelUnpackHook = callPackage ({ wheel }: makeSetupHook { name = "wheel-unpack-hook.sh"; diff --git a/pkgs/development/interpreters/python/hooks/venv-shell-hook.sh b/pkgs/development/interpreters/python/hooks/venv-shell-hook.sh new file mode 100644 index 00000000000..3185b1f9fae --- /dev/null +++ b/pkgs/development/interpreters/python/hooks/venv-shell-hook.sh @@ -0,0 +1,26 @@ +venvShellHook() { + echo "Executing venvHook" + runHook preShellHook + + if [ -d "${venvDir}" ]; then + echo "Skipping venv creation, '${venvDir}' already exists" + else + echo "Creating new venv environment in path: '${venvDir}'" + @pythonInterpreter@ -m venv "${venvDir}" + fi + + source "${venvDir}/bin/activate" + + runHook postShellHook + echo "Finished executing venvShellHook" +} + +if [ -z "${dontUseVenvShellHook:-}" ] && [ -z "${shellHook-}" ]; then + echo "Using venvShellHook" + if [ -z "${venvDir-}" ]; then + echo "Error: \`venvDir\` should be set when using \`venvShellHook\`." + exit 1 + else + shellHook=venvShellHook + fi +fi diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix index 5b148263388..a3ba61e27d5 100644 --- a/pkgs/top-level/python-packages.nix +++ b/pkgs/top-level/python-packages.nix @@ -108,7 +108,7 @@ in { inherit buildSetupcfg; inherit (callPackage ../development/interpreters/python/hooks { }) - eggUnpackHook eggBuildHook eggInstallHook flitBuildHook pipBuildHook pipInstallHook pytestCheckHook pythonCatchConflictsHook pythonImportsCheckHook pythonRemoveBinBytecodeHook setuptoolsBuildHook setuptoolsCheckHook wheelUnpackHook; + eggUnpackHook eggBuildHook eggInstallHook flitBuildHook pipBuildHook pipInstallHook pytestCheckHook pythonCatchConflictsHook pythonImportsCheckHook pythonRemoveBinBytecodeHook setuptoolsBuildHook setuptoolsCheckHook venvShellHook wheelUnpackHook; # helpers -- cgit 1.4.1