Skip to content

Commit

Permalink
python: Enter directory when creating venv
Browse files Browse the repository at this point in the history
This commit does several things in one go:

1. Fixes `libstdc++.so.6` issues due to poetry using unwrapped Python
   when used in combination with `languages.python.directory`. This was
   because the `poetry env use` wasn't given the source directory.

2. Fixes poetry creating incorrect shebangs because
   `languages.python.directory` is a relative path. This is a known
   issue: python-poetry/poetry#9415

3. Cleans up the bash code by using pushd/popd.
  • Loading branch information
pinkwah committed Jul 22, 2024
1 parent 1d848fc commit 7f9f3ea
Show file tree
Hide file tree
Showing 5 changed files with 1,103 additions and 6 deletions.
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="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

0 comments on commit 7f9f3ea

Please sign in to comment.