-
-
Save mgttlinger/7c755e7a2edae4a9c881a33fd0e82e09 to your computer and use it in GitHub Desktop.
Command-line wrapper to put commands within a nix-shell using the nearest parent shell.nix file.
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 | |
# based on discussion: https://github.com/atom-haskell/haskell-ghc-mod/issues/160 | |
origdir=$PWD | |
# Source nix env because this script is intended to be | |
# used by editors, e.g. Atom which doesn't source .bashrc, | |
# and we need NIX_PATH to be set correctly. | |
source /etc/profile | |
# Walk up the FS hierarchy until we find a shell.nix | |
while [ "$PWD" != "/" ] && [ ! -f shell.nix ]; do | |
cd .. | |
done | |
# Arg list trick: | |
# https://stackoverflow.com/questions/3104209 | |
ARGS=$(printf "%q"" " "$@") | |
# get a filename this is executed with | |
execf="$(basename $0)" | |
mydir="$(dirname $0)" | |
# Remove this script's basedir from PATH | |
PATH=":$PATH:" | |
PATH=${PATH//:$mydir:/:} | |
PATH=${PATH#:} | |
PATH=${PATH%:} | |
# Just in case | |
export PATH | |
# If we didn't find a shell.nix, give up. | |
if [ "$PWD" = "/" ]; then | |
(>&2 echo "No shell.nix found") | |
cd $origdir | |
eval $execf $ARGS | |
else | |
nixshelldir=$PWD | |
(>&2 echo "Foud shell.nix in $nixshelldir") | |
cd $origdir | |
nix-shell $nixshelldir/shell.nix --run "$execf $ARGS" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Similar to the original wrapper you just symlink this file to a location in path called like the executable you want to wrap