Last active
April 11, 2018 03:04
-
-
Save sveitser/0ac310801f23e329d9714c505d91cfb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix for picking up unwanted
*.nix
files (build tools, etc) https://gist.github.com/lionello/4ba29a03c128fefdf3ed52d5e7d5ac4c