Last active
March 11, 2025 04:30
-
-
Save ian-h-chamberlain/7d45d46e903b3dd869b6fda40a05ac8c to your computer and use it in GitHub Desktop.
Direnv helper to load extra nixpkgs into the environment
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
if upfind .envrc >/dev/null; and command -q direnv; and not set -q DIRENV_DIR | |
# Do an early load of env vars so that things like XDG_DATA_DIRS can take effect | |
direnv export fish | source | |
# Then re-exec fish for $__fish_data_dir/config.fish to pick up the vars | |
# https://fishshell.com/docs/3.7/language.html#configuration-files | |
# So far this seems to be the only option to pick up .direnv/ files... | |
exec fish | |
# TODO: PWD var hook to exec again for unsetting XDG_DATA_DIRS | |
# Possible inspiration: https://github.com/direnv/direnv/issues/73#issuecomment-747646730 | |
end |
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
#!/usr/bin/env bash | |
use_pkgs() { | |
pkgs=() | |
for pkg in "$@"; do | |
if [[ "$pkg" = *#* || "$pkg" = *:* ]]; then | |
pkgs+=("$pkg") | |
else | |
pkgs+=("nixpkgs#${pkg}") | |
fi | |
done | |
direnv_load nix shell "${pkgs[@]}" --command "$direnv" dump | |
} | |
layout_fish() { | |
local data_dir | |
data_dir=$(direnv_layout_dir)/share | |
# TODO: maybe export these as vars or helper functions | |
local func_dir=$data_dir/fish/vendor_functions.d | |
mkdir -p "$func_dir" | |
local comp_dir=$data_dir/fish/vendor_completions.d | |
mkdir -p "$comp_dir" | |
local conf_dir=$data_dir/fish/vendor_conf.d | |
mkdir -p "$conf_dir" | |
export XDG_DATA_DIRS | |
if [[ "$XDG_DATA_DIRS" != *"$data_dir"* ]]; then | |
XDG_DATA_DIRS="$data_dir:$XDG_DATA_DIRS" | |
fi | |
} | |
fish_alias() { | |
layout_fish | |
local name=$1 | |
local func_dir | |
func_dir=$(direnv_layout_dir)/share/fish/vendor_functions.d | |
fish --no-config -c "alias \$argv && funcsave -d '$func_dir' '$name'" -- "$@" | |
} | |
# vim:ft=bash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment