Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: avoid using existing virtual env or poetry binary #552

Merged
merged 4 commits into from
Apr 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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