Skip to content

Instantly share code, notes, and snippets.

@mrjk
Created August 19, 2024 06:19
Show Gist options
  • Save mrjk/8901c680d393a143cce8ab233ad79d89 to your computer and use it in GitHub Desktop.
Save mrjk/8901c680d393a143cce8ab233ad79d89 to your computer and use it in GitHub Desktop.
Direnv with recurive parent loading support
# This snippet allow recurive loading of parent .envrc files
# To put in your: ~/.config/direnv/direnvrc
# Direnv Recursive mode
# =========================
# Disable existing source_up commands
source_up ()
{
echo "WARNING: 'source_up' is disabled from user config"
}
source_up_if_exists ()
{
echo "WARNING: 'source_up_if_exists' is disabled from user config"
}
print_section ()
{
printf '%s\n' "~~~ ~~~ ~~~ ~~~ ~~~ ~~~"
}
# Allow recursive source
_source_up_all(){
echo "INFO: Direnv source_up recursive mode enabled from user configuration"
local file=${1:-.envrc}
local all_targets=$(find_up_all $file)
local parent_targets=$(sed '1d' <<<"$all_targets" | tac)
for i in $parent_targets; do
[[ -n "$i" ]] || continue
print_section
source_env "$i"
done
print_section
}
# Load parent configs
_source_up_all
@boukadam
Copy link

boukadam commented Jan 4, 2025

Thanks for sharing !
Where are defined find_up_all and tac commands ?

@mrjk
Copy link
Author

mrjk commented Jan 6, 2025

Hey, thanks for your feedback. The find_up command is part of direnv stdlib, while tac is the cat, but reversed, and it's part of standard GNU utils.

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