Skip to content

Instantly share code, notes, and snippets.

@adisbladis
Created May 7, 2018 05:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adisbladis/f0b21860b59931d70c9183e4ec077c92 to your computer and use it in GitHub Desktop.
Save adisbladis/f0b21860b59931d70c9183e4ec077c92 to your computer and use it in GitHub Desktop.
Better python dev experience with direnv + nix + pipenv
use nix
layout_nix_pipenv() {
if [[ ! -f Pipfile ]]; then
log_error 'No Pipfile found. Use `pipenv` to create a Pipfile first.'
exit 2
fi
# Use ram as virtualenv storage
local tmpdir
case $(uname -s) in
Linux*) tmpdir=$XDG_RUNTIME_DIR;;
Darwin*) tmpdir_SDK=$TMPDIR;;
*) tmpdir=/tmp
esac
if test "$tmpdir" = ""; then
tmpdir="/tmp"
fi
# Create a directory to hold all virtual envs
# We dont want $tmpdir to be too messy
local venv_base=$tmpdir/direnv-venvs
mkdir -p $venv_base
# Use nix-instantiate to figure out all the inputs for this env
# This makes sure that the virtual env has consistent dynamic linking
local env_hash=$(nix-instantiate ./shell.nix 2> /dev/null | shasum -a 256 | cut -d ' ' -f 1 | cut -c-7)
local venv_dir=$venv_base/$(basename $(pwd))-$env_hash
local venv_exists=1
local VENV=$venv_dir
if [[ -z $VENV || ! -d $VENV ]]; then
venv_exists=0
python -m venv $VENV
fi
source $VENV/bin/activate
if [ $venv_exists -eq 0 ]; then
pipenv install --dev
fi
export VIRTUAL_ENV=$(pipenv --venv)
PATH_add "$VIRTUAL_ENV/bin"
# Work around issue https://github.com/NixOS/nixpkgs/issues/39558
export PYTHONPATH=$(pipenv --venv)/lib/python*/site-packages/:$PYTHONPATH
}
layout_nix_pipenv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment