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: Enter directory when creating venv #1339

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 14 additions & 6 deletions src/modules/languages/python.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ let
};

initVenvScript = pkgs.writeShellScript "init-venv.sh" ''
pushd "${cfg.directory}"

# 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

VENV_PATH="${config.env.DEVENV_STATE}/${lib.optionalString (cfg.directory != config.devenv.root) ''"${cfg.directory}/"''}venv"
VENV_PATH="${config.env.DEVENV_STATE}/venv"

profile_python="$(${readlink} ${package.interpreter})"
devenv_interpreter_path="$(${pkgs.coreutils}/bin/cat "$VENV_PATH/.devenv_interpreter" 2> /dev/null || echo false )"
Expand Down Expand Up @@ -92,9 +94,13 @@ let
}
fi
fi

popd
'';

initPoetryScript = pkgs.writeShellScript "init-poetry.sh" ''
pushd "${cfg.directory}"

function _devenv_init_poetry_venv
{
# Make sure any tools are not attempting to use the Python interpreter from any
Expand All @@ -107,12 +113,12 @@ let

function _devenv_poetry_install
{
local POETRY_INSTALL_COMMAND=(${cfg.poetry.package}/bin/poetry install --no-interaction ${lib.concatStringsSep " " cfg.poetry.install.arguments} ${lib.optionalString (cfg.directory != config.devenv.root) ''--directory=${cfg.directory}''})
local POETRY_INSTALL_COMMAND=(${cfg.poetry.package}/bin/poetry install --no-interaction ${lib.concatStringsSep " " cfg.poetry.install.arguments})
# Avoid running "poetry install" for every shell.
# Only run it when the "poetry.lock" file or Python interpreter has changed.
# We do this by storing the interpreter path and a hash of "poetry.lock" in venv.
local ACTUAL_POETRY_CHECKSUM="${package.interpreter}:$(${pkgs.nix}/bin/nix-hash --type sha256 "$DEVENV_ROOT"/${lib.optionalString (cfg.directory != config.devenv.root) ''${cfg.directory}/''}pyproject.toml):$(${pkgs.nix}/bin/nix-hash --type sha256 "$DEVENV_ROOT"/${lib.optionalString (cfg.directory != config.devenv.root) ''${cfg.directory}/''}poetry.lock):''${POETRY_INSTALL_COMMAND[@]}"
local POETRY_CHECKSUM_FILE="$DEVENV_ROOT"/${lib.optionalString (cfg.directory != config.devenv.root) ''${cfg.directory}/''}.venv/poetry.lock.checksum
local ACTUAL_POETRY_CHECKSUM="${package.interpreter}:$(${pkgs.nix}/bin/nix-hash --type sha256 pyproject.toml):$(${pkgs.nix}/bin/nix-hash --type sha256 poetry.lock):''${POETRY_INSTALL_COMMAND[@]}"
local POETRY_CHECKSUM_FILE=".venv/poetry.lock.checksum"
if [ -f "$POETRY_CHECKSUM_FILE" ]
then
read -r EXPECTED_POETRY_CHECKSUM < "$POETRY_CHECKSUM_FILE"
Expand All @@ -131,7 +137,7 @@ let
fi
}

if [ ! -f "$DEVENV_ROOT"/${lib.optionalString (cfg.directory != config.devenv.root) ''${cfg.directory}/''}pyproject.toml ]
if [ ! -f "pyproject.toml" ]
then
echo "No pyproject.toml found. Run 'poetry init' to create one." >&2
else
Expand All @@ -140,9 +146,11 @@ let
_devenv_poetry_install
''}
${lib.optionalString cfg.poetry.activate.enable ''
source "$DEVENV_ROOT"/${lib.optionalString (cfg.directory != config.devenv.root) ''${cfg.directory}/''}.venv/bin/activate
source .venv/bin/activate
''}
fi

popd
'';
in
{
Expand Down
5 changes: 5 additions & 0 deletions tests/python-native-libs-poetry/.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
python -c "from PIL import Image"
python -c "import grpc_tools.protoc"
python -c "import transformers"

# TODO: invoke a subprocess with an old glibc and assert it doesn't crash
9 changes: 9 additions & 0 deletions tests/python-native-libs-poetry/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ pkgs, lib, ... }: {
packages = [ pkgs.cairo ];

languages.python = {
enable = true;
directory = "subdir";
poetry.enable = true;
};
}
Loading
Loading