Skip to content

Commit

Permalink
nix dev-shell: Refactor script for getting the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Apr 30, 2020
1 parent df8e9d6 commit 2fcfc6c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/nix/get-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set -e
export IN_NIX_SHELL=impure
export dontAddDisableDepTrack=1
if [[ -n $stdenv ]]; then
source $stdenv/setup
fi
export > $out
set >> $out
2 changes: 2 additions & 0 deletions src/nix/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ $(foreach name, \
$(eval $(call install-symlink, $(bindir)/nix, $(libexecdir)/nix/build-remote))

src/nix-env/user-env.cc: src/nix-env/buildenv.nix.gen.hh

src/nix/shell.cc: src/nix/get-env.sh.gen.hh
18 changes: 8 additions & 10 deletions src/nix/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ BuildEnvironment readEnvironment(const Path & path)
return res;
}

const static std::string getEnvSh =
#include "get-env.sh.gen.hh"
;

/* Given an existing derivation, return the shell environment as
initialised by stdenv's setup script. We do this by building a
modified derivation with the same dependencies and nearly the same
Expand All @@ -94,16 +98,9 @@ StorePath getDerivationEnvironment(ref<Store> store, Derivation drv)
if (builder != "bash")
throw Error("'nix dev-shell' only works on derivations that use 'bash' as their builder");

drv.args = {
"-c",
"set -e; "
"export IN_NIX_SHELL=impure; "
"export dontAddDisableDepTrack=1; "
"if [[ -n $stdenv ]]; then "
" source $stdenv/setup; "
"fi; "
"export > $out; "
"set >> $out "};
auto getEnvShPath = store->addTextToStore("get-env.sh", getEnvSh, {});

drv.args = {store->printStorePath(getEnvShPath)};

/* Remove derivation checks. */
drv.env.erase("allowedReferences");
Expand All @@ -120,6 +117,7 @@ StorePath getDerivationEnvironment(ref<Store> store, Derivation drv)
drv.env.erase(output.first);
drv.env["out"] = "";
drv.env["outputs"] = "out";
drv.inputSrcs.insert(std::move(getEnvShPath));
Hash h = hashDerivationModulo(*store, drv, true);
auto shellOutPath = store->makeOutputPath("out", h, drvName);
drv.outputs.insert_or_assign("out", DerivationOutput(shellOutPath.clone(), "", ""));
Expand Down

0 comments on commit 2fcfc6c

Please sign in to comment.