Skip to content

Commit

Permalink
Merge pull request #552 from bobvanderlinden/pr-python-poetry-test-leak
Browse files Browse the repository at this point in the history
python: avoid using existing virtual env or poetry binary
  • Loading branch information
domenkozar authored Apr 26, 2023
2 parents b454e31 + d11d8db commit 9465ba9
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/modules/languages/python.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ let
venvPath = "${config.env.DEVENV_STATE}/venv";

initVenvScript = pkgs.writeShellScript "init-venv.sh" ''
# Make sure any tools are not attempting to use the python interpreter from any
# existing virtual environment. For instance if devenv was started within an venv.
unset VIRTUAL_ENV
if [ ! -L ${venvPath}/devenv-profile ] \
|| [ "$(${pkgs.coreutils}/bin/readlink ${venvPath}/devenv-profile)" != "${config.env.DEVENV_PROFILE}" ]
then
Expand All @@ -17,24 +21,28 @@ let
${lib.optionalString cfg.poetry.enable ''
[ -f "${config.env.DEVENV_STATE}/poetry.lock.checksum" ] && rm ${config.env.DEVENV_STATE}/poetry.lock.checksum
''}
python -m venv ${venvPath}
ln -sf ${config.env.DEVENV_PROFILE} ${venvPath}/devenv-profile
${cfg.package.interpreter} -m venv ${venvPath}
${pkgs.coreutils}/bin/ln -sf ${config.env.DEVENV_PROFILE} ${venvPath}/devenv-profile
fi
source ${venvPath}/bin/activate
'';

initPoetryScript = pkgs.writeShellScript "init-poetry.sh" ''
function _devenv-init-poetry-venv()
{
# Make sure any tools are not attempting to use the python interpreter from any
# existing virtual environment. For instance if devenv was started within an venv.
unset VIRTUAL_ENV
if [ ! -L ${config.env.DEVENV_ROOT}/.venv ]
then
ln -sf ${venvPath} ${config.env.DEVENV_ROOT}/.venv
${pkgs.coreutils}/bin/ln --symbolic --no-target-directory --force ${venvPath} ${config.env.DEVENV_ROOT}/.venv
fi
if [ ! -d ${venvPath} ] \
|| [ ! "$(readlink ${venvPath}/bin/python)" -ef "${cfg.package.interpreter}" ]
|| [ ! "$(${pkgs.coreutils}/bin/readlink ${venvPath}/bin/python)" -ef "${cfg.package.interpreter}" ]
then
poetry env use --no-interaction ${cfg.package.interpreter}
${cfg.poetry.package}/bin/poetry env use --no-interaction ${cfg.package.interpreter}
fi
}
Expand Down

0 comments on commit 9465ba9

Please sign in to comment.