diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 75f20d635842..42162868e19f 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -44,6 +44,8 @@ extern "C" { #include "markdown.hh" #include "local-fs-store.hh" #include "print.hh" +#include "gc-small-vector.hh" +#include "print-options.hh" #if HAVE_BOEHMGC #define GC_INCLUDE_NEW @@ -887,6 +889,49 @@ void NixRepl::loadFiles() notice("Loading installable '%1%'...", what); addAttrsToScope(*i); } + + if (evalSettings.replInitFile) { + notice("Loading '%1%'...", *evalSettings.replInitFile.get()); + Value replInit; + state->evalFile(evalSettings.replInitFile, replInit); + // // Doesn't help: + // state->forceValue(replInit, replInit.determinePos(noPos)); + + auto infoBuilder = state->buildBindings(1); + Value currentSystem; + currentSystem.mkString(evalSettings.getCurrentSystem()); + infoBuilder.insert(state->symbols.create("currentSystem"), ¤tSystem); + Value info; + info.mkAttrs(infoBuilder.finish()); + + // // This doesn't work either: + // auto map = mapStaticEnvBindings(state->symbols, *staticEnv, *env); + // auto currentBuilder = state->buildBindings(map->size()); + // for (auto & [name, value] : *map) { + // currentBuilder.insert(state->symbols.create(name), value); + // } + + auto currentBuilder = state->buildBindings(staticEnv->vars.size()); + for (auto & [symbol, displacement] : staticEnv->vars) { + currentBuilder.insert(symbol, env->values[displacement]); + } + Value currentAttrs; + currentAttrs.mkAttrs(currentBuilder.finish()); + + Value newAttrs; + SmallValueVector<2> args = {&info, ¤tAttrs}; + state->callFunction(replInit, args.size(), args.data(), newAttrs, replInit.determinePos(noPos)); + std::cout << ValuePrinter(*state, newAttrs, errorPrintOptions) << std::endl; + + addAttrsToScope(newAttrs); + + // This prints the values of the variables fine: + for (auto & [symbol, displacement] : staticEnv->vars) { + auto value = *env->values[displacement]; + state->forceValue(value, value.determinePos(noPos)); + std::cout << state->symbols[symbol] << " = " << ValuePrinter(*state, value, errorPrintOptions) << std::endl; + } + } }