nix develop: Set output paths to writable locations

Currently, they're set to $(pwd)/outputs/$outputName. This allows
commands like 'make install' to work.
This commit is contained in:
Eelco Dolstra 2020-08-28 18:16:03 +02:00
parent 691a1bd717
commit 3156560d41
3 changed files with 63 additions and 46 deletions

View file

@ -421,6 +421,8 @@
stdenv.mkDerivation { stdenv.mkDerivation {
name = "nix"; name = "nix";
outputs = [ "out" "dev" "doc" ];
buildInputs = buildDeps ++ propagatedDeps ++ perlDeps; buildInputs = buildDeps ++ propagatedDeps ++ perlDeps;
inherit configureFlags; inherit configureFlags;
@ -428,15 +430,6 @@
enableParallelBuilding = true; enableParallelBuilding = true;
installFlags = "sysconfdir=$(out)/etc"; installFlags = "sysconfdir=$(out)/etc";
shellHook =
''
export prefix=$(pwd)/inst
configureFlags+=" --prefix=$prefix"
PKG_CONFIG_PATH=$prefix/lib/pkgconfig:$PKG_CONFIG_PATH
PATH=$prefix/bin:$PATH
unset PYTHONPATH
'';
}); });
}; };

View file

@ -15,7 +15,7 @@ struct Var
{ {
bool exported = true; bool exported = true;
bool associative = false; bool associative = false;
std::string value; // quoted string or array std::string quoted; // quoted string or array
}; };
struct BuildEnvironment struct BuildEnvironment
@ -75,12 +75,12 @@ BuildEnvironment readEnvironment(const Path & path)
else if (std::regex_search(pos, file.cend(), match, varRegex, std::regex_constants::match_continuous)) { else if (std::regex_search(pos, file.cend(), match, varRegex, std::regex_constants::match_continuous)) {
pos = match[0].second; pos = match[0].second;
res.env.insert({match[1], Var { .exported = exported.count(match[1]) > 0, .value = match[2] }}); res.env.insert({match[1], Var { .exported = exported.count(match[1]) > 0, .quoted = match[2] }});
} }
else if (std::regex_search(pos, file.cend(), match, assocArrayRegex, std::regex_constants::match_continuous)) { else if (std::regex_search(pos, file.cend(), match, assocArrayRegex, std::regex_constants::match_continuous)) {
pos = match[0].second; pos = match[0].second;
res.env.insert({match[1], Var { .associative = true, .value = match[2] }}); res.env.insert({match[1], Var { .associative = true, .quoted = match[2] }});
} }
else if (std::regex_search(pos, file.cend(), match, functionRegex, std::regex_constants::match_continuous)) { else if (std::regex_search(pos, file.cend(), match, functionRegex, std::regex_constants::match_continuous)) {
@ -92,6 +92,8 @@ BuildEnvironment readEnvironment(const Path & path)
path, file.substr(pos - file.cbegin(), 60)); path, file.substr(pos - file.cbegin(), 60));
} }
res.env.erase("__output");
return res; return res;
} }
@ -125,27 +127,32 @@ StorePath getDerivationEnvironment(ref<Store> store, const StorePath & drvPath)
/* Rehash and write the derivation. FIXME: would be nice to use /* Rehash and write the derivation. FIXME: would be nice to use
'buildDerivation', but that's privileged. */ 'buildDerivation', but that's privileged. */
drv.name += "-env"; drv.name += "-env";
for (auto & output : drv.outputs) for (auto & output : drv.outputs) {
drv.env.erase(output.first); output.second = { .output = DerivationOutputInputAddressed { .path = StorePath::dummy } };
drv.outputs = {{"out", DerivationOutput { .output = DerivationOutputInputAddressed { .path = StorePath::dummy }}}}; drv.env[output.first] = "";
drv.env["out"] = ""; }
drv.env["_outputs_saved"] = drv.env["outputs"];
drv.env["outputs"] = "out";
drv.inputSrcs.insert(std::move(getEnvShPath)); drv.inputSrcs.insert(std::move(getEnvShPath));
Hash h = std::get<0>(hashDerivationModulo(*store, drv, true)); Hash h = std::get<0>(hashDerivationModulo(*store, drv, true));
auto shellOutPath = store->makeOutputPath("out", h, drv.name);
drv.outputs.insert_or_assign("out", DerivationOutput { .output = DerivationOutputInputAddressed { for (auto & output : drv.outputs) {
.path = shellOutPath auto outPath = store->makeOutputPath(output.first, h, drv.name);
} }); output.second = { .output = DerivationOutputInputAddressed { .path = outPath } };
drv.env["out"] = store->printStorePath(shellOutPath); drv.env[output.first] = store->printStorePath(outPath);
auto shellDrvPath2 = writeDerivation(*store, drv); }
auto shellDrvPath = writeDerivation(*store, drv);
/* Build the derivation. */ /* Build the derivation. */
store->buildPaths({{shellDrvPath2}}); store->buildPaths({{shellDrvPath}});
assert(store->isValidPath(shellOutPath)); for (auto & outPath : drv.outputPaths(*store)) {
assert(store->isValidPath(outPath));
auto outPathS = store->toRealPath(outPath);
if (lstat(outPathS).st_size)
return outPath;
}
return shellOutPath; throw Error("get-env.sh failed to produce an environment");
} }
struct Common : InstallableCommand, MixProfile struct Common : InstallableCommand, MixProfile
@ -171,8 +178,12 @@ struct Common : InstallableCommand, MixProfile
"UID", "UID",
}; };
void makeRcScript(const BuildEnvironment & buildEnvironment, std::ostream & out) std::string makeRcScript(
const BuildEnvironment & buildEnvironment,
const Path & outputsDir = absPath(".") + "/outputs")
{ {
std::ostringstream out;
out << "unset shellHook\n"; out << "unset shellHook\n";
out << "nix_saved_PATH=\"$PATH\"\n"; out << "nix_saved_PATH=\"$PATH\"\n";
@ -180,9 +191,9 @@ struct Common : InstallableCommand, MixProfile
for (auto & i : buildEnvironment.env) { for (auto & i : buildEnvironment.env) {
if (!ignoreVars.count(i.first) && !hasPrefix(i.first, "BASH_")) { if (!ignoreVars.count(i.first) && !hasPrefix(i.first, "BASH_")) {
if (i.second.associative) if (i.second.associative)
out << fmt("declare -A %s=(%s)\n", i.first, i.second.value); out << fmt("declare -A %s=(%s)\n", i.first, i.second.quoted);
else { else {
out << fmt("%s=%s\n", i.first, i.second.value); out << fmt("%s=%s\n", i.first, i.second.quoted);
if (i.second.exported) if (i.second.exported)
out << fmt("export %s\n", i.first); out << fmt("export %s\n", i.first);
} }
@ -193,13 +204,26 @@ struct Common : InstallableCommand, MixProfile
out << buildEnvironment.bashFunctions << "\n"; out << buildEnvironment.bashFunctions << "\n";
// FIXME: set outputs
out << "export NIX_BUILD_TOP=\"$(mktemp -d --tmpdir nix-shell.XXXXXX)\"\n"; out << "export NIX_BUILD_TOP=\"$(mktemp -d --tmpdir nix-shell.XXXXXX)\"\n";
for (auto & i : {"TMP", "TMPDIR", "TEMP", "TEMPDIR"}) for (auto & i : {"TMP", "TMPDIR", "TEMP", "TEMPDIR"})
out << fmt("export %s=\"$NIX_BUILD_TOP\"\n", i); out << fmt("export %s=\"$NIX_BUILD_TOP\"\n", i);
out << "eval \"$shellHook\"\n"; out << "eval \"$shellHook\"\n";
/* Substitute occurrences of output paths. */
auto outputs = buildEnvironment.env.find("outputs");
assert(outputs != buildEnvironment.env.end());
// FIXME: properly unquote 'outputs'.
StringMap rewrites;
for (auto & outputName : tokenizeString<std::vector<std::string>>(replaceStrings(outputs->second.quoted, "'", ""))) {
auto from = buildEnvironment.env.find(outputName);
assert(from != buildEnvironment.env.end());
// FIXME: unquote
rewrites.insert({from->second.quoted, outputsDir + "/" + outputName});
}
return rewriteStrings(out.str(), rewrites);
} }
Strings getDefaultFlakeAttrPaths() override Strings getDefaultFlakeAttrPaths() override
@ -288,19 +312,18 @@ struct CmdDevelop : Common, MixEnvironment
auto [rcFileFd, rcFilePath] = createTempFile("nix-shell"); auto [rcFileFd, rcFilePath] = createTempFile("nix-shell");
std::ostringstream ss; auto script = makeRcScript(buildEnvironment);
makeRcScript(buildEnvironment, ss);
ss << fmt("rm -f '%s'\n", rcFilePath); script += fmt("rm -f '%s'\n", rcFilePath);
if (!command.empty()) { if (!command.empty()) {
std::vector<std::string> args; std::vector<std::string> args;
for (auto s : command) for (auto s : command)
args.push_back(shellEscape(s)); args.push_back(shellEscape(s));
ss << fmt("exec %s\n", concatStringsSep(" ", args)); script += fmt("exec %s\n", concatStringsSep(" ", args));
} }
writeFull(rcFileFd.get(), ss.str()); writeFull(rcFileFd.get(), script);
stopProgressBar(); stopProgressBar();
@ -362,7 +385,7 @@ struct CmdPrintDevEnv : Common
stopProgressBar(); stopProgressBar();
makeRcScript(buildEnvironment, std::cout); std::cout << makeRcScript(buildEnvironment);
} }
}; };

View file

@ -1,12 +1,6 @@
set -e set -e
if [ -e .attrs.sh ]; then source .attrs.sh; fi if [ -e .attrs.sh ]; then source .attrs.sh; fi
outputs=$_outputs_saved
for __output in $_outputs_saved; do
declare "$__output"="$out"
done
unset _outputs_saved __output
export IN_NIX_SHELL=impure export IN_NIX_SHELL=impure
export dontAddDisableDepTrack=1 export dontAddDisableDepTrack=1
@ -14,5 +8,12 @@ if [[ -n $stdenv ]]; then
source $stdenv/setup source $stdenv/setup
fi fi
export > $out for __output in $outputs; do
set >> $out if [[ -z $__done ]]; then
export > ${!__output}
set >> ${!__output}
__done=1
else
echo -n >> ${!__output}
fi
done