Skip to content

Instantly share code, notes, and snippets.

@zimbatm
Created October 29, 2020 11:00
Show Gist options
  • Save zimbatm/fb29358a7d64d3d19b3af02211f4b42f to your computer and use it in GitHub Desktop.
Save zimbatm/fb29358a7d64d3d19b3af02211f4b42f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# One of the common issue in Linux, is that new path entries don't necessarily
# appear after installing a new package. This is because Bash maintains an
# internal cache of available binaries. On NixOS, Bash has been compiled to
# remove that mechanism.
#
# This script combines knowledge of Nix with Bash to automate that cache
# invalidation. Just source this file in your ~/.bashrc or other startup
# script.
# The location where the user's packages get installed
_nix_profile_path=/nix/var/nix/profiles/per-user/${USER:-$(id -nu)}/profile
# Memo of the previous store path
_nix_profile_hash=
# This function is invoked before every prompt
_nix_auto_reshash() {
local new_hash
new_hash=$(readlink "$_nix_profile_path")
# If the store path has changed
if [[ $_nix_profile_hash == "$new_hash" ]]; then
# Invalidate the bash internal cache
hash -r
_nix_profile_hash=$new_hash
fi
}
if ! [[ "${PROMPT_COMMAND:-}" =~ _nix_auto_rehash ]]; then
PROMPT_COMMAND="_nix_auto_rehash${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment