Skip to content

Commit

Permalink
Implement repl-init-file
Browse files Browse the repository at this point in the history
Closes NixOS#9940
  • Loading branch information
9999years committed Mar 9, 2024
1 parent e80cb0e commit cf2a891
Showing 1 changed file with 78 additions and 1 deletion.
79 changes: 78 additions & 1 deletion src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extern "C" {
#include "finally.hh"
#include "markdown.hh"
#include "local-fs-store.hh"
#include "print.hh"
#include "gc-small-vector.hh"

#if HAVE_BOEHMGC
#define GC_INCLUDE_NEW
Expand Down Expand Up @@ -114,6 +114,26 @@ struct NixRepl
void evalString(std::string s, Value & v);
void loadDebugTraceEnv(DebugTrace & dt);

/**
* Load the `repl-init-file` and add the resulting AttrSet to the top-level
* bindings.
*/
void loadReplInitFile();
/**
* Get the `info` AttrSet that's passed as the first argument to the
* `repl-init-file`.
*/
Value * replInitInfo();

/**
* Get the current top-level bindings as an AttrSet.
*/
Value * bindingsToAttrs();
/**
* Parse a file, evaluate its result, and force the resulting value.
*/
Value * evalFile(SourcePath & path);

void printValue(std::ostream & str,
Value & v,
unsigned int maxDepth = std::numeric_limits<unsigned int>::max())
Expand Down Expand Up @@ -887,6 +907,42 @@ void NixRepl::loadFiles()
notice("Loading installable '%1%'...", what);
addAttrsToScope(*i);
}

loadReplInitFile();
}

void NixRepl::loadReplInitFile()
{
if (!evalSettings.replInitFile) {
return;
}

SourcePath replInitFile = evalSettings.replInitFile;
notice("Loading '%1%'...", replInitFile);
auto replInit = evalFile(replInitFile);

auto info = replInitInfo();

auto currentAttrs = bindingsToAttrs();

Value &newAttrs(*state->allocValue());
SmallValueVector<2> args = {info, currentAttrs};
state->callFunction(*replInit, args.size(), args.data(), newAttrs, replInit->determinePos(noPos));

addAttrsToScope(newAttrs);
}

Value * NixRepl::replInitInfo()
{
auto builder = state->buildBindings(1);

Value * currentSystem(state->allocValue());
currentSystem->mkString(evalSettings.getCurrentSystem());
builder.insert(state->symbols.create("currentSystem"), currentSystem);

Value * info(state->allocValue());
info->mkAttrs(builder.finish());
return info;
}


Expand Down Expand Up @@ -919,6 +975,18 @@ void NixRepl::addVarToScope(const Symbol name, Value & v)
varNames.emplace(state->symbols[name]);
}

Value * NixRepl::bindingsToAttrs()
{
auto builder = state->buildBindings(staticEnv->vars.size());
for (auto & [symbol, displacement] : staticEnv->vars) {
builder.insert(symbol, env->values[displacement]);
}

Value * attrs(state->allocValue());
attrs->mkAttrs(builder.finish());
return attrs;
}


Expr * NixRepl::parseString(std::string s)
{
Expand All @@ -933,6 +1001,15 @@ void NixRepl::evalString(std::string s, Value & v)
state->forceValue(v, v.determinePos(noPos));
}

Value * NixRepl::evalFile(SourcePath & path)
{
auto expr = state->parseExprFromFile(evalSettings.replInitFile, staticEnv);
Value * result(state->allocValue());
expr->eval(*state, *env, *result);
state->forceValue(*result, result->determinePos(noPos));
return result;
}


std::unique_ptr<AbstractNixRepl> AbstractNixRepl::create(
const SearchPath & searchPath, nix::ref<Store> store, ref<EvalState> state,
Expand Down

0 comments on commit cf2a891

Please sign in to comment.