1 aug 2013
using my-env you create a custom environment, see [1] where you will find a ~/.nixpkgs/config.nix example script. a couple of times i was discussing with others that it would be cool to have that environment loaded automatically when entering that directory. some say that the ruby devs did astonishing things in that regard. reading [2] finally inspired me to write this example how this could be done.
so assuming you have installed ‘nix-env -i env-sdl’, this posting describes what to do in order to get it loaded automatically.
i put this into my ~/.zshrc:
function cd {
# loads environments automatically when .load-env file exists with the project name <name> in it
# load-env-<name> must of course exist, example: load-env-sdl where .load-env would contain sdl
builtin cd "$@"
if [ -f ".load-env" ] ; then
echo "Note: found '.load-env' file, automatically loading the 'load-env-${a}' environment."
# display its contents
a=$(cat .load-env)
load-env-${a}
echo "Note: You left the 'load-env-${a}' environment. Type: load-env-${a} to reload it"
fi
}
afterwards type:
zsh
to make changes to the spawned shell, edit $NIXREPOS/nixpkgs/pkgs/misc/my-env/default.nix
% load-env-sdl
env-sdl loaded
sdl:[joachim@lenovo-t530:~/projects/myproject]$
% cd ~/projects/myproject
% echo "sdl" > .load-env
this creates a .load-env file containing ‘sdl’ in it.
% cd ~/projects/myproject
env-sdl loaded
sdl:[joachim@lenovo-t530:~/projects/myproject]$
there is currently no support for environment nesting but i doubt this is needed anyway. whenever you want to leave the environment simply type ‘exit’ or hit ‘ctrl+d’.
see also [3]