From 3c9b9b13af52776ca08bbbb12b3a98f45de6083b Mon Sep 17 00:00:00 2001 From: Kevin Robert Stravers Date: Sat, 21 Sep 2024 02:56:04 -0400 Subject: [PATCH 1/2] nix repl: Print which variables are just loaded When we run `nix repl nixpkgs` we get "Added 6 variables". This is not useful as it doesn't tell us which variables the flake has exported to our global repl scope. This patch prints the name of each variable that was just loaded. We currently cap printing to 20 variables in order to avoid excessive prints. https://github.com/NixOS/nix/issues/11404 --- src/libcmd/repl.cc | 19 +++++++++++++++++++ tests/functional/repl.sh | 29 ++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 07968fa43..e5164b137 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -813,6 +813,25 @@ void NixRepl::addAttrsToScope(Value & attrs) staticEnv->sort(); staticEnv->deduplicate(); notice("Added %1% variables.", attrs.attrs()->size()); + + const int max_print = 20; + int counter = 0; + std::ostringstream loaded; + for (auto & i : attrs.attrs()->lexicographicOrder(state->symbols)) { + if (counter >= max_print) + break; + + if (counter > 0) + loaded << ", "; + + printIdentifier(loaded, state->symbols[i->name]); + counter += 1; + } + + notice("%1%", loaded.str()); + + if (attrs.attrs()->size() > max_print) + notice("... and %1% more", attrs.attrs()->size() - max_print); } diff --git a/tests/functional/repl.sh b/tests/functional/repl.sh index 762636e44..7f5b26913 100755 --- a/tests/functional/repl.sh +++ b/tests/functional/repl.sh @@ -157,6 +157,32 @@ foo + baz ' "3" \ ./flake ./flake\#bar --experimental-features 'flakes' +testReplResponse $' +:a { a = 1; b = 2; longerName = 3; "with spaces" = 4; } +' 'Added 4 variables. +a, b, longerName, "with spaces" +' + +cat < attribute-set.nix +{ + a = 1; + b = 2; + longerName = 3; + "with spaces" = 4; +} +EOF +testReplResponse ' +:l ./attribute-set.nix +' 'Added 4 variables. +a, b, longerName, "with spaces" +' + +testReplResponseNoRegex $' +:a builtins.foldl\' (x: y: x // y) {} (map (x: { ${builtins.toString x} = x; }) (builtins.genList (x: x) 23)) +' 'Added 23 variables. +"0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "21", "22", "3", "4", "5", "6" +... and 3 more' + # Test the `:reload` mechansim with flakes: # - Eval `./flake#changingThing` # - Modify the flake @@ -322,7 +348,8 @@ runRepl () { -e "s@$testDir@/path/to/tests/functional@g" \ -e "s@$testDirNoUnderscores@/path/to/tests/functional@g" \ -e "s@$nixVersion@@g" \ - -e "s@Added [0-9]* variables@Added variables@g" \ + -e "/Added [0-9]* variables/{s@ [0-9]* @ @;n;d}" \ + -e '/\.\.\. and [0-9]* more/d' \ | grep -vF $'warning: you don\'t have Internet access; disabling some network-dependent features' \ ; } From 13e37043292df2b39c0252b1d476dad4c4aa8cce Mon Sep 17 00:00:00 2001 From: Kevin Robert Stravers Date: Sat, 21 Sep 2024 02:58:18 -0400 Subject: [PATCH 2/2] 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 --- src/libcmd/repl.cc | 21 ++++++++++++++++++++- tests/functional/repl.sh | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index e5164b137..54a002765 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -69,6 +69,7 @@ struct NixRepl const static int envSize = 32768; std::shared_ptr staticEnv; + Value lastLoaded; Env * env; int displ; StringSet varNames; @@ -95,6 +96,7 @@ struct NixRepl void loadFiles(); void loadFlakes(); void reloadFilesAndFlakes(); + void showLastLoaded(); void addAttrsToScope(Value & attrs); void addVarToScope(const Symbol name, Value & v); Expr * parseString(std::string s); @@ -378,6 +380,7 @@ ProcessLineResult NixRepl::processLine(std::string line) << " current profile\n" << " :l, :load Load Nix expression and add it to scope\n" << " :lf, :load-flake Load Nix flake and add it to scope\n" + << " :ll, :last-loaded Show most recently loaded variables added to scope\n" << " :p, :print Evaluate and print expression recursively\n" << " Strings are printed directly, without escaping.\n" << " :q, :quit Exit nix-repl\n" @@ -468,6 +471,10 @@ ProcessLineResult NixRepl::processLine(std::string line) loadFlake(arg); } + else if (command == ":ll" || command == ":last-loaded") { + showLastLoaded(); + } + else if (command == ":r" || command == ":reload") { state->resetFileCache(); reloadFilesAndFlakes(); @@ -760,6 +767,16 @@ void NixRepl::initEnv() 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() { @@ -814,6 +831,8 @@ void NixRepl::addAttrsToScope(Value & attrs) staticEnv->deduplicate(); notice("Added %1% variables.", attrs.attrs()->size()); + lastLoaded = attrs; + const int max_print = 20; int counter = 0; std::ostringstream loaded; @@ -831,7 +850,7 @@ void NixRepl::addAttrsToScope(Value & attrs) notice("%1%", loaded.str()); 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); } diff --git a/tests/functional/repl.sh b/tests/functional/repl.sh index 7f5b26913..7c4bae4ba 100755 --- a/tests/functional/repl.sh +++ b/tests/functional/repl.sh @@ -181,7 +181,7 @@ testReplResponseNoRegex $' :a builtins.foldl\' (x: y: x // y) {} (map (x: { ${builtins.toString x} = x; }) (builtins.genList (x: x) 23)) ' 'Added 23 variables. "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: # - Eval `./flake#changingThing` @@ -349,7 +349,7 @@ runRepl () { -e "s@$testDirNoUnderscores@/path/to/tests/functional@g" \ -e "s@$nixVersion@@g" \ -e "/Added [0-9]* variables/{s@ [0-9]* @ @;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' \ ; }