Skip to content

Instantly share code, notes, and snippets.

@sveitser
Last active Apr 11, 2018
Embed
What would you like to do?
#!/bin/bash
#
# Cache nix-shell environment
#
# - watch all *.nix files, ~/.direnvrc and .envrc
# - based on https://github.com/direnv/direnv/wiki/Nix
#
use_nix() {
local cache_dir="$HOME/.cache/direnv/$(pwd)"
mkdir -p "$cache_dir"
local files="$HOME/.direnvrc .envrc *.nix"
local cache="$cache_dir/direnv.$(nix-instantiate *.nix | shasum -a 256 | head -c 16)"
local redo=false
if ! test -e "$cache"; then
echo "nix-instantiate returned new value."
redo=true
else
for f in $files; do
if test $f -nt "$cache"; then
echo "File $f is newer than cache."
redo=true
continue
fi
done
fi
if $redo; then
local tmp="$(mktemp "${cache}.tmp-XXXXXXXX")"
trap "rm -rf '$tmp'" EXIT
nix-shell --show-trace "$@" --run 'direnv dump' > "$tmp" && \
mv "$tmp" "$cache"
else
echo "Using cached direnv."
fi
direnv_load cat "$cache"
if [[ $# = 0 ]]; then
for f in $files; do
watch_file $f
done
fi
}
# GistId: 0ac310801f23e329d9714c505d91cfb3
@lionello
Copy link

lionello commented Apr 11, 2018

Fix for picking up unwanted *.nix files (build tools, etc) https://gist.github.com/lionello/4ba29a03c128fefdf3ed52d5e7d5ac4c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment