From 78fa97e5e3aac0a8bfaab265ee140e3a07db097f Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sat, 23 Dec 2023 23:57:17 -0500 Subject: [PATCH 1/2] Recreate Python venv and run pip install less often --- src/modules/languages/python.nix | 42 +++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/modules/languages/python.nix b/src/modules/languages/python.nix index d680255ee..bdf925490 100644 --- a/src/modules/languages/python.nix +++ b/src/modules/languages/python.nix @@ -45,24 +45,42 @@ 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" + ${package.interpreter} -m venv --upgrade-deps "$VENV_PATH" + echo "${package.interpreter}" > "$VENV_PATH/.devenv_interpreter" + if [ -n "$requirements" ] + then + echo "${requirements}" > "$VENV_PATH/.devenv_requirements" + fi 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" '' From 9f4fdf9f59a2220705ce56603745e405ea015d87 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sat, 23 Dec 2023 23:57:17 -0500 Subject: [PATCH 2/2] Recreate Python venv and run pip install less often --- src/modules/languages/python.nix | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/modules/languages/python.nix b/src/modules/languages/python.nix index bdf925490..133d2171e 100644 --- a/src/modules/languages/python.nix +++ b/src/modules/languages/python.nix @@ -61,10 +61,6 @@ let echo ${package.interpreter} -m venv --upgrade-deps "$VENV_PATH" ${package.interpreter} -m venv --upgrade-deps "$VENV_PATH" echo "${package.interpreter}" > "$VENV_PATH/.devenv_interpreter" - if [ -n "$requirements" ] - then - echo "${requirements}" > "$VENV_PATH/.devenv_requirements" - fi fi source "$VENV_PATH"/bin/activate