diff --git a/doc/manual/rl-next/repl-overlays.md b/doc/manual/rl-next/repl-overlays.md new file mode 100644 index 00000000000..93d85c46242 --- /dev/null +++ b/doc/manual/rl-next/repl-overlays.md @@ -0,0 +1,35 @@ +--- +synopsis: Add `repl-overlays` option +prs: 10203 +--- + +A `repl-overlays` option has been added, 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`: + +```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}`: + +```ShellSession +$ nix repl --repl-overlays ~/.config/nix/repl.nix nixpkgs +Nix 2.21.0pre20240309_4111bb6 +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» +``` diff --git a/src/libcmd/local.mk b/src/libcmd/local.mk index abb7459a7d0..7ddf9523f4a 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-overlays.nix.gen.hh diff --git a/src/libcmd/repl-overlays.nix b/src/libcmd/repl-overlays.nix new file mode 100644 index 00000000000..33ce49482af --- /dev/null +++ b/src/libcmd/repl-overlays.nix @@ -0,0 +1,8 @@ +info: +initial: +functions: +let final = builtins.foldl' + (prev: function: prev // (function info final prev)) + initial + functions; +in final diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 75f20d63584..76f2fa029cb 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,37 @@ 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 * replOverlaysEvalFunction(); + + /** + * 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()) @@ -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); } + + loadReplOverlays(); +} + +void NixRepl::loadReplOverlays() +{ + if (!evalSettings.replOverlays) { + return; + } + + notice("Loading '%1%'...", Magenta("repl-overlays")); + auto replInitFilesFunction = replOverlaysEvalFunction(); + + 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::replOverlaysEvalFunction() +{ + auto evalReplInitFilesPath = CanonPath("repl-overlays.nix"); + if (!state->corepkgsFS->pathExists(evalReplInitFilesPath)) { + state->corepkgsFS->addFile( + evalReplInitFilesPath, + #include "repl-overlays.nix.gen.hh" + ); + } + + SourcePath evalReplInitFilesSourcePath(state->corepkgsFS, evalReplInitFilesPath); + return evalFile(evalReplInitFilesSourcePath); +} + +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); + 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; } @@ -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) { @@ -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::create( const SearchPath & searchPath, nix::ref store, ref state, diff --git a/src/libexpr/eval-settings.hh b/src/libexpr/eval-settings.hh index b5783d28ff2..29d3548e30c 100644 --- a/src/libexpr/eval-settings.hh +++ b/src/libexpr/eval-settings.hh @@ -137,6 +137,42 @@ struct EvalSettings : Config This is useful for debugging warnings in third-party Nix code. )"}; + + PathsSetting replOverlays{this, Paths(), "repl-overlays", + R"( + A list of files containing Nix expressions that can be used to add + default bindings to [`nix + repl`](@docroot@/command-ref/new-cli/nix3-repl.md) sessions. + + Each file is called with three arguments: + 1. An [attribute set](@docroot@/language/values.html#attribute-set) + containing a + [`currentSystem`](@docroot@/language/builtin-constants.md#builtins-currentSystem) + attribute (this is identical to + [`builtins.currentSystem`](@docroot@/language/builtin-constants.md#builtins-currentSystem), + except that it's available in + [`pure-eval`](@docroot@/command-ref/conf-file.html#conf-pure-eval) + mode). + 2. The top-level bindings produced by the previous `repl-overlays` + value (or the default top-level bindings). + 3. The final top-level bindings produced by calling all + `repl-overlays`. + + For example, the following file would alias `pkgs` to + `legacyPackages.${info.currentSystem}` (if that attribute is defined): + + ```nix + info: prev: final: + if prev ? legacyPackages + && prev.legacyPackages ? ${info.currentSystem} + then + { + pkgs = prev.legacyPackages.${info.currentSystem}; + } + else + { } + ``` + )"}; }; extern EvalSettings evalSettings; diff --git a/src/libexpr/gc-small-vector.hh b/src/libexpr/gc-small-vector.hh index 7f4f08fc753..94c3ad28b03 100644 --- a/src/libexpr/gc-small-vector.hh +++ b/src/libexpr/gc-small-vector.hh @@ -4,6 +4,7 @@ #if HAVE_BOEHMGC +#define GC_INCLUDE_NEW #include #include #include @@ -39,4 +40,4 @@ using SmallValueVector = SmallVector; template using SmallTemporaryValueVector = SmallVector; -} \ No newline at end of file +} diff --git a/src/libutil/config.cc b/src/libutil/config.cc index 617c2ec89dc..dfb2fb3b073 100644 --- a/src/libutil/config.cc +++ b/src/libutil/config.cc @@ -443,6 +443,39 @@ void OptionalPathSetting::operator =(const std::optional & v) this->assign(v); } +PathsSetting::PathsSetting(Config * options, + const Paths & def, + const std::string & name, + const std::string & description, + const std::set & aliases) + : BaseSetting(def, true, name, description, aliases) +{ + options->addSetting(this); +} + + +Paths PathsSetting::parse(const std::string & str) const +{ + auto strings = tokenizeString(str); + Paths parsed; + + for (auto str : strings) { + parsed.push_back(canonPath(str)); + } + + return parsed; +} + +void PathsSetting::operator =(const Paths & v) +{ + this->assign(v); +} + +PathsSetting::operator bool() const noexcept +{ + return !get().empty(); +} + bool GlobalConfig::set(const std::string & name, const std::string & value) { for (auto & config : *configRegistrations) diff --git a/src/libutil/config.hh b/src/libutil/config.hh index 07322b60d7a..b9ac91c48ad 100644 --- a/src/libutil/config.hh +++ b/src/libutil/config.hh @@ -375,6 +375,26 @@ public: void operator =(const std::optional & v); }; +/** + * Like `OptionalPathSetting`, but allows multiple paths. + */ +class PathsSetting : public BaseSetting +{ +public: + + PathsSetting(Config * options, + const Paths & def, + const std::string & name, + const std::string & description, + const std::set & aliases = {}); + + Paths parse(const std::string & str) const override; + + void operator =(const Paths & v); + + operator bool() const noexcept; +}; + struct GlobalConfig : public AbstractConfig { typedef std::vector ConfigRegistrations;