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

Recreate Python venv and run pip install less often #912

Merged
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
38 changes: 26 additions & 12 deletions src/modules/languages/python.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,38 @@ let

VENV_PATH="${config.env.DEVENV_STATE}/venv"

if [ "$(${readlink} "$VENV_PATH"/bin/python)" != "$(${readlink} ${package.interpreter}/bin/python)" ] \
|| [ "$(${readlink} "$VENV_PATH"/requirements.txt)" != "$(${readlink} ${if requirements != null then requirements else "$VENV_PATH/requirements.txt"})" ]
profile_python="$(${readlink} ${package.interpreter})"
devenv_interpreter_path="$(${pkgs.coreutils}/bin/cat "$VENV_PATH/.devenv_interpreter" 2> /dev/null|| false )"
venv_python="$(${readlink} "$devenv_interpreter_path")"
requirements="${lib.optionalString (cfg.venv.requirements != null) ''${requirements}''}"

# recreate venv if necessary
if [ -z $venv_python ] || [ $profile_python != $venv_python ]
then
if [ -d "$VENV_PATH" ]
then
echo "Python interpreter/requirements changed, rebuilding Python venv..."
${pkgs.coreutils}/bin/rm -rf "$VENV_PATH"
fi
echo "Python interpreter changed, rebuilding Python venv..."
${pkgs.coreutils}/bin/rm -rf "$VENV_PATH"
${lib.optionalString cfg.poetry.enable ''
[ -f "${config.env.DEVENV_STATE}/poetry.lock.checksum" ] && rm ${config.env.DEVENV_STATE}/poetry.lock.checksum
''}
echo ${package.interpreter} -m venv "$VENV_PATH"
${package.interpreter} -m venv "$VENV_PATH"
echo ${package.interpreter} -m venv --upgrade-deps "$VENV_PATH"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't --upgrade-deps introduce reproducibility issues? .venv will have different versions of pip depending on when you install it. (running it today will install X, running it tomorrow will install Y)

I'm not 100% sure this is a good idea, whether it should be the default and whether it should be behind an option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with it not happening. I just wanted to get rid of the irritating "your pip is out of date" warnings :)

${package.interpreter} -m venv --upgrade-deps "$VENV_PATH"
echo "${package.interpreter}" > "$VENV_PATH/.devenv_interpreter"
fi

source "$VENV_PATH"/bin/activate
${lib.optionalString (cfg.venv.requirements != null) ''
"$VENV_PATH"/bin/pip install -r ${requirements}
''}

# reinstall requirements if necessary
if [ -n "$requirements" ]
then
devenv_requirements_path="$(${pkgs.coreutils}/bin/cat "$VENV_PATH/.devenv_requirements" 2> /dev/null|| false )"
devenv_requirements="$(${readlink} "$devenv_requirements_path")"
if [ -z $devenv_requirements ] || [ $devenv_requirements != $requirements ]
then
echo "${requirements}" > "$VENV_PATH/.devenv_requirements"
echo "Requirements changed, running pip install -r ${requirements}..."
"$VENV_PATH"/bin/pip install -r ${requirements}
fi
fi
'';

initPoetryScript = pkgs.writeShellScript "init-poetry.sh" ''
Expand Down
Loading