Skip to content

Instantly share code, notes, and snippets.

@garthk
Created August 30, 2024 11:46
Show Gist options
  • Save garthk/eea264de03d839ad731a94bba4598e29 to your computer and use it in GitHub Desktop.
Save garthk/eea264de03d839ad731a94bba4598e29 to your computer and use it in GitHub Desktop.
layout_python_hatch: direnv config plugin for python projects using hatch
# shellcheck shell=sh enable=all
# ~/.config/direnv/lib/layout_python_hatch.sh
# direnv config plugin for hatch
# https://direnv.net
fail() {
[ "$#" != "0" ] || set -- FAIL
echo >&2 "$@"
return 1
}
layout_python_hatch() {
unset VIRTUAL_ENV PYTHON
env=${1-default}
[ -e pyproject.toml ] ||
fail "no pyproject.toml"
grep -q '^\[tool\.hatch[].]' pyproject.toml ||
fail "no [tool.hatch] or [tool.hatch.*]"
has hatch ||
fail "hatch not available; uv tool install hatch?"
hatch env create "$env" > /dev/null 2>&1 ||
fail "hatch env create $env: exit $?"
VIRTUAL_ENV=$(hatch env find "$env") ||
fail "hatch env find $env: exit $?"
PYTHON=${VIRTUAL_ENV}/bin/python3
export PYTHON VIRTUAL_ENV
PATH_add "${VIRTUAL_ENV?}/bin"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment