diff --git a/doc/manual/nix-shell.xml b/doc/manual/nix-shell.xml index e57c726f53..4b369662e2 100644 --- a/doc/manual/nix-shell.xml +++ b/doc/manual/nix-shell.xml @@ -51,6 +51,22 @@ derivation for development. shell.nix if it exists, and default.nix otherwise. +If the derivation defines the variable +shellHook, it will be evaluated after +$stdenv/setup has been sourced. Since this hook is +not executed by regular Nix builds, it allows you to perform +initialisation specific to nix-shell. For example, +the derivation attribute + + +shellHook = + '' + echo "Hello shell!" + ''; + + +will cause nix-shell to print Hello shell!. + diff --git a/scripts/nix-build.in b/scripts/nix-build.in index fff71021fb..c197dcca9a 100755 --- a/scripts/nix-build.in +++ b/scripts/nix-build.in @@ -207,6 +207,7 @@ foreach my $expr (@exprs) { ($pure ? '' : 'p=$PATH; ' ) . 'dontAddDisableDepTrack=1; ' . '[ -e $stdenv/setup ] && source $stdenv/setup; ' . + 'if [ "$(type -t runHook)" = function ]; then runHook shellHook; fi; ' . ($pure ? '' : 'PATH=$PATH:$p; unset p; ') . 'set +e; ' . '[ -n "$PS1" ] && PS1="\n\[\033[1;32m\][nix-shell:\w]$\[\033[0m\] "; ' .