From 727b43478cce8ebbd0f58530878d3af80a3ba233 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 4 Apr 2024 16:26:25 -0700 Subject: Add `repl-overlays` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a `repl-overlays` option, which specifies files that can overlay and modify the top-level bindings in `nix repl`. For example, with the following contents in `~/.config/nix/repl.nix`: info: final: prev: let optionalAttrs = predicate: attrs: if predicate then attrs else {}; in optionalAttrs (prev ? legacyPackages && prev.legacyPackages ? ${info.currentSystem}) { pkgs = prev.legacyPackages.${info.currentSystem}; } We can run `nix repl` and use `pkgs` to refer to `legacyPackages.${currentSystem}`: $ nix repl --repl-overlays ~/.config/nix/repl.nix nixpkgs Lix 2.90.0 Type :? for help. Loading installable 'flake:nixpkgs#'... Added 5 variables. Loading 'repl-overlays'... Added 6 variables. nix-repl> pkgs.bash «derivation /nix/store/g08b5vkwwh0j8ic9rkmd8mpj878rk62z-bash-5.2p26.drv» Change-Id: Ic12e0f2f210b2f46e920c33088dfe1083f42391a --- src/libcmd/repl.cc | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 163 insertions(+), 2 deletions(-) (limited to 'src/libcmd/repl.cc') diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index ee3a6e268..4aa46dbc0 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -30,6 +30,7 @@ #include "signals.hh" #include "print.hh" #include "progress-bar.hh" +#include "gc-small-vector.hh" #if HAVE_BOEHMGC #define GC_INCLUDE_NEW @@ -118,6 +119,45 @@ struct NixRepl void evalString(std::string s, Value & v); void loadDebugTraceEnv(DebugTrace & dt); + /** + * Load the `repl-overlays` and add the resulting AttrSet to the top-level + * bindings. + */ + void loadReplOverlays(); + + /** + * Get a list of each of the `repl-overlays` (parsed and evaluated). + */ + Value * replOverlays(); + + /** + * Get the Nix function that composes the `repl-overlays` together. + */ + Value * getReplOverlaysEvalFunction(); + + /** + * Cached return value of `getReplOverlaysEvalFunction`. + * + * Note: This is `shared_ptr` to avoid garbage collection. + */ + std::shared_ptr replOverlaysEvalFunction = + std::allocate_shared(traceable_allocator(), nullptr); + + /** + * Get the `info` AttrSet that's passed as the first argument to each + * of the `repl-overlays`. + */ + 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()) @@ -770,14 +810,114 @@ 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); } + + loadReplOverlays(); +} + +void NixRepl::loadReplOverlays() +{ + if (!evalSettings.replOverlays) { + return; + } + + notice("Loading '%1%'...", Magenta("repl-overlays")); + auto replInitFilesFunction = getReplOverlaysEvalFunction(); + + Value &newAttrs(*state->allocValue()); + SmallValueVector<3> args = {replInitInfo(), bindingsToAttrs(), replOverlays()}; + state->callFunction( + *replInitFilesFunction, + args.size(), + args.data(), + newAttrs, + replInitFilesFunction->determinePos(noPos) + ); + + addAttrsToScope(newAttrs); +} + +Value * NixRepl::getReplOverlaysEvalFunction() +{ + if (replOverlaysEvalFunction && *replOverlaysEvalFunction) { + return *replOverlaysEvalFunction; + } + + auto evalReplInitFilesPath = CanonPath::root + "repl-overlays.nix"; + *replOverlaysEvalFunction = state->allocValue(); + auto code = + #include "repl-overlays.nix.gen.hh" + ; + auto expr = state->parseExprFromString( + code, + SourcePath(evalReplInitFilesPath), + state->staticBaseEnv + ); + + state->eval(expr, **replOverlaysEvalFunction); + + return *replOverlaysEvalFunction; +} + +Value * NixRepl::replOverlays() +{ + Value * replInits(state->allocValue()); + state->mkList(*replInits, evalSettings.replOverlays.get().size()); + Value ** replInitElems = replInits->listElems(); + + size_t i = 0; + for (auto path : evalSettings.replOverlays.get()) { + debug("Loading '%1%' path '%2%'...", "repl-overlays", path); + SourcePath sourcePath((CanonPath(path))); + auto replInit = evalFile(sourcePath); + + if (!replInit->isLambda()) { + state->error( + "Expected `repl-overlays` to be a lambda but found %1%: %2%", + showType(*replInit), + ValuePrinter(*state, *replInit, errorPrintOptions) + ) + .atPos(replInit->determinePos(noPos)) + .debugThrow(); + } + + if (replInit->lambda.fun->hasFormals() + && !replInit->lambda.fun->formals->ellipsis) { + state->error( + "Expected first argument of %1% to have %2% to allow future versions of Lix to add additional attributes to the argument", + "repl-overlays", + "..." + ) + .atPos(replInit->determinePos(noPos)) + .debugThrow(); + } + + replInitElems[i] = replInit; + i++; + } + + + return replInits; +} + +Value * NixRepl::replInitInfo() +{ + auto builder = state->buildBindings(2); + + 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; } @@ -810,6 +950,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) { @@ -824,6 +976,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, -- cgit v1.2.3