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 fa0b3b6
Show file tree
Hide file tree
Showing 2 changed files with 58 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
45 changes: 45 additions & 0 deletions tests/functional/repl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,49 @@ 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 <<EOF > 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
'

testReplResponse $'
attributes = builtins.foldl\' (x: y: x // y) {} (map (x: { ${builtins.toString x} = x; }) (builtins.genList (x: x) 13))
:a attributes
' 'Added 13 variables.
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
... And 3 other variables
'

# Test the `:reload` mechansim with flakes:
# - Eval `./flake#changingThing`
# - Modify the flake
Expand Down Expand Up @@ -302,6 +345,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 fa0b3b6

Please sign in to comment.