Skip to content

Instantly share code, notes, and snippets.

@zimbatm
Created January 17, 2020 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zimbatm/77bfa0268b5a2a540c4d0d9189bb1ae2 to your computer and use it in GitHub Desktop.
Save zimbatm/77bfa0268b5a2a540c4d0d9189bb1ae2 to your computer and use it in GitHub Desktop.
# Usage: use nix_shell
#
# Works like use_nix, except that it's only rebuilt if the shell.nix or
# default.nix file changes. This avoids scenarios where the nix-channel is
# being updated and all the projects now need to be re-built.
#
# To force the reload the derivation, run `touch shell.nix`
use_nix() {
local shellfile=shell.nix
local wd=$PWD/.direnv/nix
local drvfile=$wd/shell.drv
# same heuristic as nix-shell
if [[ ! -f $shellfile ]]; then
shellfile=default.nix
fi
if [[ ! -f $shellfile ]]; then
fail "use nix_shell: shell.nix or default.nix not found in the folder"
fi
if [[ -f $drvfile && $(stat -c %Y "$shellfile") -gt $(stat -c %Y "$drvfile") ]]; then
log_status "use nix_shell: removing stale drv"
rm "$drvfile"
fi
if [[ ! -f $drvfile ]]; then
mkdir -p "$wd"
# instanciate the drv like it was in a nix-shell
IN_NIX_SHELL=1 nix-instantiate \
--show-trace \
--add-root "$drvfile" --indirect \
"$shellfile" >/dev/null
fi
direnv_load nix-shell "$drvfile" --run "$(join_args "$direnv" dump)"
watch_file "$shellfile"
}
@zupo
Copy link

zupo commented Jan 19, 2020

@zupo
Copy link

zupo commented Aug 8, 2020

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