Fraggle
1
I feel really, really stupid asking this question, but I’m going round in circles searching for the answer to this and getting nowhere.
If I have a list of attrsets, such that repl reports the list variable as:
[ { ... } { ... } ]
How do I get repl to show me the contents of a selected set from that list? Something akin to the array index operator in C.
rjpc
2
might not work in your case but:
try prepending :p
to the input expression.
nix-repl> { a.b.c = 1; }
{ a = { ... }; }
nix-repl> :p { a.b.c = 1; }
{ a = { b = { c = 1; }; }; }
source: https://nix.dev/tutorials/nix-language#interactive-evaluation
Fraggle
3
That’s a good workaround to know about, thanks. Does that mean there is no actual nix syntax for this?
There’s no operator for this unfortunately, but there is builtins.elemAt
:
nix-repl> builtins.elemAt [ "hi" "there" ] 0
"hi"
Also a bit quirky, the CLI allows you to dig into list elements with an attribute-path syntax:
❯ nix-instantiate --eval --expr '[ "hi" "there" ]' -A 0
"hi"
❯ nix-instantiate --eval --expr '{ x = [ { y = "hi"; } ]; }' -A x.0.y
"hi"
(also works with the experimental nix eval
fyi)
2 Likes
Fraggle
5
Thanks, that’s the ticket!