diff --git a/src/libcmd/local.mk b/src/libcmd/local.mk index abb7459a7d09..ab9ecf07041d 100644 --- a/src/libcmd/local.mk +++ b/src/libcmd/local.mk @@ -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 diff --git a/src/libcmd/repl-init-files.nix b/src/libcmd/repl-init-files.nix new file mode 100644 index 000000000000..ea8faef591ae --- /dev/null +++ b/src/libcmd/repl-init-files.nix @@ -0,0 +1,8 @@ +info: +initial: +functions: +let final = builtins.foldl' + (prev: function: prev // (function info prev final)) + initial + functions; +in final diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 75f20d635842..6c79d2f88318 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -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 @@ -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::max()) @@ -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; } @@ -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) { @@ -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::create( const SearchPath & searchPath, nix::ref store, ref state,