Skip to content

Commit

Permalink
Improve direnv and poetry interop
Browse files Browse the repository at this point in the history
 - Adds global `.direnvrc` which defines `layout poetry` that
 initialized poetry environment in a directory with `pyproject.toml`
 - Adds utility script `poetry-here` that creates local `.envrc` and
   sets up direnv
 - Makes appropriate symlinks of these files into `$HOME`
  • Loading branch information
matyama committed Mar 12, 2022
1 parent 75476ce commit 345e64e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .direnvrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Define `layout poetry` for local `.envrc`
# - https://github.com/direnv/direnv/wiki/Python#poetry
# - https://medium.com/analytics-vidhya/best-practice-for-using-poetry-608ab6feaaf
layout_poetry() {

if [[ ! -f pyproject.toml ]]; then
log_status 'No pyproject.toml found. Will initialize poetry in no-interactive mode'

# Initialize poetry in no-interactive mode
poetry init -n -q
fi

# Create venv if it doesn't exist
poetry run true

# Setup VIRTUAL_ENV and add it to PATH
export VIRTUAL_ENV=$(poetry env info --path)
export POETRY_ACTIVE=1
PATH_add "$VIRTUAL_ENV/bin"

# Make symlink to `.venv` for easy IDE environment setup
if [ ! -L .venv ]; then
ln -ns $VIRTUAL_ENV .venv
fi
}
10 changes: 10 additions & 0 deletions .local/bin/poetry-here
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

# Allows to run `poetry-here` anywhere one wants to have poetry setup.
# - Works even if new project with poetry has not yet been set up
# - https://medium.com/analytics-vidhya/best-practice-for-using-poetry-608ab6feaaf

echo "layout poetry" > .envrc

direnv allow .
direnv exec . true
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ links: $(ALACRITTY_CONFIG_DIR) $(BYOBU_CONFIG_DIR) ~/.config/nvim/vim-plug ~/.co
$(CFG_DIR)/.xsession \
$(CFG_DIR)/.gitconfig \
$(CFG_DIR)/.pam_environment \
$(CFG_DIR)/.direnvrc \
$(CFG_DIR)/.zsh* \
$(CFG_DIR)/.p10k.zsh
@{ \
Expand All @@ -200,6 +201,7 @@ links: $(ALACRITTY_CONFIG_DIR) $(BYOBU_CONFIG_DIR) ~/.config/nvim/vim-plug ~/.co
$(CFG_DIR)/.local/bin/alacritty_toggle.sh \
$(CFG_DIR)/.local/bin/increase_swap.sh \
$(CFG_DIR)/.local/bin/init_ubuntu.sh \
$(CFG_DIR)/.local/bin/poetry-here \
$(CFG_DIR)/.local/bin/upgrade_kernel.sh
@[ "$$(grep 'user_readenv=1' /etc/pam.d/login)" ] || \
echo "Finish pam env setup by manually updating '/etc/pam.d/login' - see https://askubuntu.com/a/636544"
Expand Down

0 comments on commit 345e64e

Please sign in to comment.