Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
direnvrc nix cache
use_nix() {
local shell_file=$(test -f shell.nix && echo shell.nix || echo default.nix)
local cache_key=$(nix-instantiate "$shell_file" 2> /dev/null | shasum -a 1 | cut -d ' ' -f 1)
# 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
local cachedir="${tmpdir}/direnv-nix"
mkdir -p $cachedir
local cache="$cachedir/$cache_key"
if [[ ! -e "$cache" ]] || \
[[ "$HOME/.direnvrc" -nt "$cache" ]] || \
[[ ".envrc" -nt "$cache" ]] || \
[[ "default.nix" -nt "$cache" ]] || \
[[ "shell.nix" -nt "$cache" ]];
then
local tmp="$(mktemp "${cache_key}.tmp-XXXXXXXX")"
trap "rm -rf '$tmp'" EXIT
nix-instantiate ./shell.nix --indirect --add-root ~/.direnv-gcroots/"$cache_key"
nix-shell --show-trace "$@" --run 'direnv dump' > "$tmp" && \
mv "$tmp" "$cache"
fi
direnv_load cat "$cache"
if [[ $# = 0 ]]; then
watch_file default.nix
watch_file shell.nix
fi
}
@lionello
Copy link

lionello commented Sep 6, 2018

Made some small changes here https://gist.github.com/lionello/3cb29d9eea83938622eec9ff3bfb3b69

Mostly, line 28 needs to use $shell_file because that's what gets used on line 3. I also removed the default.nix/shell.nix timestamp checking (cache_key should take care of it) but instead ensure the timestamp of the .envrc file is identical to the cache file.

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