Skip to content

Commit

Permalink
nix repl: Print which variables are just loaded
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kstrafe committed Sep 3, 2024
1 parent ef1ac0d commit 55ccb40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}


Expand Down
2 changes: 2 additions & 0 deletions tests/functional/repl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ runRepl () {
-e "s@$testDirNoUnderscores@/path/to/tests/functional@g" \
-e "s@$nixVersion@<nix version>@g" \
-e "s@Added [0-9]* variables@Added <number omitted> 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' \
;
}
Expand Down

0 comments on commit 55ccb40

Please sign in to comment.