Skip to content

Instantly share code, notes, and snippets.

@mgttlinger
Forked from 3noch/nix-wrap
Created October 21, 2017 10:59
Show Gist options
  • Save mgttlinger/7c755e7a2edae4a9c881a33fd0e82e09 to your computer and use it in GitHub Desktop.
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.
#!/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
@mgttlinger
Copy link
Author

Similar to the original wrapper you just symlink this file to a location in path called like the executable you want to wrap

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