Skip to content

Commit

Permalink
Implement repl-init-files
Browse files Browse the repository at this point in the history
Closes NixOS#9940
  • Loading branch information
9999years committed Mar 10, 2024
1 parent 751960c commit 237ae78
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/libcmd/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ libcmd_LDFLAGS = $(EDITLINE_LIBS) $(LOWDOWN_LIBS) $(THREAD_LDFLAGS)
libcmd_LIBS = libstore libutil libexpr libmain libfetchers

$(eval $(call install-file-in, $(buildprefix)$(d)/nix-cmd.pc, $(libdir)/pkgconfig, 0644))

$(d)/repl.cc: $(d)/repl-init-files.nix.gen.hh
8 changes: 8 additions & 0 deletions src/libcmd/repl-init-files.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
info:
initial:
functions:
let final = builtins.foldl'
(prev: function: prev // (function info prev final))
initial
functions;
in final
129 changes: 126 additions & 3 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ extern "C" {
#include "finally.hh"
#include "markdown.hh"
#include "local-fs-store.hh"
#include "print.hh"
#include "gc-small-vector.hh"
#include "fs-input-accessor.hh"
#include "memory-input-accessor.hh"

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

/**
* Load the `repl-init-files` and add the resulting AttrSet to the top-level
* bindings.
*/
void loadReplInitFiles();

/**
* Get a list of each of the `repl-init-files` (parsed and evaluated).
*/
Value * replInitFiles();

/**
* Get the Nix function that composes the `repl-init-files` together.
*/
Value * replInitFilesEvalFunction();

/**
* Get the `info` AttrSet that's passed as the first argument to each
* of the `repl-init-files`.
*/
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 @@ -879,14 +912,83 @@ void NixRepl::loadFiles()
loadedFiles.clear();

for (auto & i : old) {
notice("Loading '%1%'...", i);
notice("Loading '%1%'...", Magenta(i));
loadFile(i);
}

for (auto & [i, what] : getValues()) {
notice("Loading installable '%1%'...", what);
notice("Loading installable '%1%'...", Magenta(what));
addAttrsToScope(*i);
}

loadReplInitFiles();
}

void NixRepl::loadReplInitFiles()
{
if (!evalSettings.replInitFiles) {
return;
}

notice("Loading '%1%'...", Magenta("repl-init-files"));
auto replInitFilesFunction = replInitFilesEvalFunction();

Value &newAttrs(*state->allocValue());
SmallValueVector<3> args = {replInitInfo(), bindingsToAttrs(), replInitFiles()};
state->callFunction(
*replInitFilesFunction,
args.size(),
args.data(),
newAttrs,
replInitFilesFunction->determinePos(noPos)
);

addAttrsToScope(newAttrs);
}

Value * NixRepl::replInitFilesEvalFunction()
{
auto evalReplInitFilesPath = CanonPath("repl-init-files.nix");
if (!state->corepkgsFS->pathExists(evalReplInitFilesPath)) {
state->corepkgsFS->addFile(
evalReplInitFilesPath,
#include "repl-init-files.nix.gen.hh"
);
}

SourcePath evalReplInitFilesSourcePath(state->corepkgsFS, evalReplInitFilesPath);
return evalFile(evalReplInitFilesSourcePath);
}

Value * NixRepl::replInitFiles()
{
Value * replInits(state->allocValue());
state->mkList(*replInits, evalSettings.replInitFiles.get().size());
Value ** replInitElems = replInits->listElems();

size_t i = 0;
for (auto path : evalSettings.replInitFiles.get()) {
debug("Loading '%1%'...", path);
SourcePath sourcePath(makeFSInputAccessor(), CanonPath(path));
replInitElems[i] = evalFile(sourcePath);
i++;
}


return replInits;
}

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 +1021,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 +1047,15 @@ void NixRepl::evalString(std::string s, Value & v)
state->forceValue(v, v.determinePos(noPos));
}

Value * NixRepl::evalFile(SourcePath & path)
{
auto expr = state->parseExprFromFile(path, 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 237ae78

Please sign in to comment.