1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 02:21:16 +02:00

nix repl: Add :ll to show all recently loaded variables

Invoking `:ll` will start a pager with all variables which have just
been loaded by `:lf`, `:l`, or by a flake provided to `nix repl` as an
argument.

https://github.com/NixOS/nix/issues/11404
This commit is contained in:
Kevin Robert Stravers 2024-09-21 02:58:18 -04:00
parent 3c9b9b13af
commit 13e3704329
No known key found for this signature in database
GPG key ID: 7C7E12DE0238D700
2 changed files with 22 additions and 3 deletions

View file

@ -69,6 +69,7 @@ struct NixRepl
const static int envSize = 32768; const static int envSize = 32768;
std::shared_ptr<StaticEnv> staticEnv; std::shared_ptr<StaticEnv> staticEnv;
Value lastLoaded;
Env * env; Env * env;
int displ; int displ;
StringSet varNames; StringSet varNames;
@ -95,6 +96,7 @@ struct NixRepl
void loadFiles(); void loadFiles();
void loadFlakes(); void loadFlakes();
void reloadFilesAndFlakes(); void reloadFilesAndFlakes();
void showLastLoaded();
void addAttrsToScope(Value & attrs); void addAttrsToScope(Value & attrs);
void addVarToScope(const Symbol name, Value & v); void addVarToScope(const Symbol name, Value & v);
Expr * parseString(std::string s); Expr * parseString(std::string s);
@ -378,6 +380,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
<< " current profile\n" << " current profile\n"
<< " :l, :load <path> Load Nix expression and add it to scope\n" << " :l, :load <path> Load Nix expression and add it to scope\n"
<< " :lf, :load-flake <ref> Load Nix flake and add it to scope\n" << " :lf, :load-flake <ref> Load Nix flake and add it to scope\n"
<< " :ll, :last-loaded Show most recently loaded variables added to scope\n"
<< " :p, :print <expr> Evaluate and print expression recursively\n" << " :p, :print <expr> Evaluate and print expression recursively\n"
<< " Strings are printed directly, without escaping.\n" << " Strings are printed directly, without escaping.\n"
<< " :q, :quit Exit nix-repl\n" << " :q, :quit Exit nix-repl\n"
@ -468,6 +471,10 @@ ProcessLineResult NixRepl::processLine(std::string line)
loadFlake(arg); loadFlake(arg);
} }
else if (command == ":ll" || command == ":last-loaded") {
showLastLoaded();
}
else if (command == ":r" || command == ":reload") { else if (command == ":r" || command == ":reload") {
state->resetFileCache(); state->resetFileCache();
reloadFilesAndFlakes(); reloadFilesAndFlakes();
@ -760,6 +767,16 @@ void NixRepl::initEnv()
varNames.emplace(state->symbols[i.first]); varNames.emplace(state->symbols[i.first]);
} }
void NixRepl::showLastLoaded()
{
RunPager pager;
for (auto & i : *lastLoaded.attrs()) {
std::string_view name = state->symbols[i.name];
logger->cout(name);
}
}
void NixRepl::reloadFilesAndFlakes() void NixRepl::reloadFilesAndFlakes()
{ {
@ -814,6 +831,8 @@ void NixRepl::addAttrsToScope(Value & attrs)
staticEnv->deduplicate(); staticEnv->deduplicate();
notice("Added %1% variables.", attrs.attrs()->size()); notice("Added %1% variables.", attrs.attrs()->size());
lastLoaded = attrs;
const int max_print = 20; const int max_print = 20;
int counter = 0; int counter = 0;
std::ostringstream loaded; std::ostringstream loaded;
@ -831,7 +850,7 @@ void NixRepl::addAttrsToScope(Value & attrs)
notice("%1%", loaded.str()); notice("%1%", loaded.str());
if (attrs.attrs()->size() > max_print) if (attrs.attrs()->size() > max_print)
notice("... and %1% more", attrs.attrs()->size() - max_print); notice("... and %1% more; view with :ll", attrs.attrs()->size() - max_print);
} }

View file

@ -181,7 +181,7 @@ testReplResponseNoRegex $'
:a builtins.foldl\' (x: y: x // y) {} (map (x: { ${builtins.toString x} = x; }) (builtins.genList (x: x) 23)) :a builtins.foldl\' (x: y: x // y) {} (map (x: { ${builtins.toString x} = x; }) (builtins.genList (x: x) 23))
' 'Added 23 variables. ' 'Added 23 variables.
"0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "21", "22", "3", "4", "5", "6" "0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "21", "22", "3", "4", "5", "6"
... and 3 more' ... and 3 more; view with :ll'
# Test the `:reload` mechansim with flakes: # Test the `:reload` mechansim with flakes:
# - Eval `./flake#changingThing` # - Eval `./flake#changingThing`
@ -349,7 +349,7 @@ runRepl () {
-e "s@$testDirNoUnderscores@/path/to/tests/functional@g" \ -e "s@$testDirNoUnderscores@/path/to/tests/functional@g" \
-e "s@$nixVersion@<nix version>@g" \ -e "s@$nixVersion@<nix version>@g" \
-e "/Added [0-9]* variables/{s@ [0-9]* @ <number omitted> @;n;d}" \ -e "/Added [0-9]* variables/{s@ [0-9]* @ <number omitted> @;n;d}" \
-e '/\.\.\. and [0-9]* more/d' \ -e '/\.\.\. and [0-9]* more; view with :ll/d' \
| grep -vF $'warning: you don\'t have Internet access; disabling some network-dependent features' \ | grep -vF $'warning: you don\'t have Internet access; disabling some network-dependent features' \
; ;
} }