Last active
March 10, 2020 14:35
-
-
Save mikeplus64/ef220937525e473f1769147c68ffddeb to your computer and use it in GitHub Desktop.
Runs nix.sh, then reads its env, and re-exports to fish anything that was found to have been exported in the script.
This file contains 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 fish | |
# source this within config.fish | |
# set -l NIX_SH ~/.nix-profile/etc/profile.d/nix-daemon.sh | |
set -l NIX_SH ~/.nix-profile/etc/profile.d/nix.sh | |
# get all the vars exported by the script | |
set -l VARS (cat $NIX_SH | grep '^ *export' | sed 's/^ *export *//g' | cut -d= -f1 | sort | uniq) | |
set -l VARS_REGEX '^('(echo $VARS | tr ' ' '|')')$' | |
set ENV_STR "### BEGIN ENV ###" | |
# add 'env' to the end of a script | |
function wrap | |
cat $argv[1] | |
echo 'echo "'$ENV_STR'"' | |
echo env | |
end | |
set -l IN_ENV false | |
# run script in bash, wait for ENV_STR to be echo'd, and begin reading env | |
# values. for keys found in $VARS, add to current env with set -g | |
wrap $NIX_SH | bash | while read l | |
if [ $IN_ENV = true ] | |
set KEY (echo $l | cut -d= -f1) | |
set VAL (echo $l | cut -d= -f2-) | |
# fish has weird handling of PATH | |
if [ $KEY = PATH ] | |
set -xg $KEY (echo $VAL | tr ':' '\n' | while read v | |
string escape $v | |
end) | |
set -g NIX_FISH_ENV $NIX_FISH_ENV $KEY | |
else if echo $KEY | grep -Pq $VARS_REGEX | |
set -xg $KEY $VAL | |
set -g NIX_FISH_ENV $NIX_FISH_ENV $KEY | |
end | |
else if [ $l = $ENV_STR ] | |
set IN_ENV true | |
end | |
end | |
set -e ENV_STR |
You can replace with perl for example I use this:
#!/usr/bin/env fish
# source this within config.fish
set -l NIX_SH ~/.nix-profile/etc/profile.d/nix.sh
# get all the vars exported by the script
set -l VARS (cat $NIX_SH | grep '^ *export' | sed 's/^ *export *//g' | cut -d= -f1 | sort | uniq)
set -l VARS_REGEX ' ('(echo $VARS | perl -pe 's/ /|/g')')='
# set -l VARS_REGEX 'PATH='
bash -c "source $NIX_SH;export|awk '/$VARS_REGEX/'" | while read l
set KEY (echo $l | sed 's/declare -x //' | cut -d= -f1)
set VAL (echo $l | cut -d= -f2- | perl -pe 's/^"//;s/"$//;')
# fish has weird handling of PATH
if [ $KEY = PATH ]
set -xg $KEY (echo $VAL | tr ':' '\n' | while read v
string escape $v
end)
else
# Uncomment to show the list of env var set with this script
# printf "\n set $KEY $VAL\n"
set -xg $KEY $VAL
end
end
echo >/dev/null
fisher add edc/bass
and have bass . ~/.nix-profile/etc/profile.d/nix.sh
on ~/.config/fish/conf.d/nix.fish
did the trick for me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On my computer running macOS 10.13, the
grep -Pq
step fails - could it depend on GNU Grep behavior?