From 55ccb40083635dbb0f1df36dc2e1a0dfc5f45dbe Mon Sep 17 00:00:00 2001 From: Kevin Robert Stravers Date: Mon, 2 Sep 2024 15:11:06 -0400 Subject: [PATCH] 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 10 variables in order to avoid excessive prints. Github issue: 11404 --- src/libcmd/repl.cc | 13 +++++++++++++ tests/functional/repl.sh | 2 ++ 2 files changed, 15 insertions(+) diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 63f6c1bdd0db..0aed7eda8653 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -790,6 +790,19 @@ void NixRepl::addAttrsToScope(Value & attrs) staticEnv->sort(); staticEnv->deduplicate(); notice("Added %1% variables.", attrs.attrs()->size()); + + const int max_print = 10; + int count = 0; + for (auto & i : *attrs.attrs()) { + count += 1; + if (count > max_print) { + notice("... And %1% other variables", attrs.attrs()->size() - max_print); + break; + } + + std::string_view name = state->symbols[i.name]; + notice("- %1%", name); + } } diff --git a/tests/functional/repl.sh b/tests/functional/repl.sh index 706e0f5dba98..d416a05e905c 100755 --- a/tests/functional/repl.sh +++ b/tests/functional/repl.sh @@ -302,6 +302,8 @@ runRepl () { -e "s@$testDirNoUnderscores@/path/to/tests/functional@g" \ -e "s@$nixVersion@@g" \ -e "s@Added [0-9]* variables@Added variables@g" \ + -e '/^- /d' \ + -e '/\.\.\. And [0-9]* other variables/d' \ | grep -vF $'warning: you don\'t have Internet access; disabling some network-dependent features' \ ; }