Created
June 6, 2018 09:44
-
-
Save adisbladis/b7ab21ab67497ce20a6b1dfd2a56bc5a to your computer and use it in GitHub Desktop.
direnvrc nix cache
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.