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 5e48be5 commit a7b5ef3
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
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
106 changes: 105 additions & 1 deletion 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,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 +909,67 @@ void NixRepl::loadFiles()
notice("Loading installable '%1%'...", what);
addAttrsToScope(*i);
}

loadReplInitFile();
}

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

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++;
}

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);
auto replInitFilesFunction = evalFile(evalReplInitFilesSourcePath);

auto info = replInitInfo();

auto currentAttrs = bindingsToAttrs();

Value &newAttrs(*state->allocValue());
SmallValueVector<3> args = {info, currentAttrs, replInits};
state->callFunction(
*replInitFilesFunction,
args.size(),
args.data(),
newAttrs,
replInitFilesFunction->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 +1002,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 +1028,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 a7b5ef3

Please sign in to comment.