Skip to content

Add attrpath notation equivalent to elemAt #10949

@rhendric

Description

@rhendric

Is your feature request related to a problem? Please describe.

I'm in a REPL. I'm exploring parts of Nixpkgs. I type a partial expression:

nix-repl> someExpr.foo
{ bar = { ... }; ignoreThis = { ... }; moreStuff = { ... }; }

I hit up-arrow and keep typing to drill deeper:

nix-repl> someExpr.foo.bar
{ baz = true; qux = [ ... ]; quux = { ... }; }

nix-repl> someExpr.foo.bar.qux
[ { ... } ]

nix-repl> someExpr.foo.bar.qux.0
error: attempt to call something which is not a function but a list

       at «string»:1:1:

            1| someExpr.foo.bar.qux.0
             | ^

Of course that doesn't work. I don't actually type that. What I actually do is hit up-arrow, then hit Home, then type builtins.elemAt, then hit End, then type 0.

nix-repl> builtins.elemAt someExpr.foo.bar.qux 0
{ greatMoreStuff = { ... }; }

Now what do I get to do? That's right, hit up-arrow, then hit Home, then type (, then hit End, then type ).greatMoreStuff.

It's a tiny papercut of a problem, but it makes me die a little every time I see a list in a complicated Nix expression. In many other languages with left-to-right dotted accessor syntax, I'd be able to write someExpr.foo.bar.qux[0].greatMoreStuff, or someExpr.foo.bar.qux.0.greatMoreStuff. Why not in Nix?

Describe the solution you'd like

I'd like the Nix attrpath grammar rule to be extended with some notation that's equivalent to builtins.elemAt, such as attrpath.0, attrpath[0], attrpath@0 if you like—any available syntax is fine with me as long as the result is still an attrpath, and thus can be extended with more attribute selectors without enclosing the entire previous base expression plus attrpath in parentheses the way that (builtins.elemAt ... 0).greatMoreStuff requires. I would even be happy with reusing the existing syntax attrpath.${0} and supporting integers applied to lists with that in addition to strings applied to attrsets.

Describe alternatives you've considered

Not caring about the tiny details that make Nix uncomfortable relative to other languages.

Additional context

I imagine this needs an RFC, given NixOS/rfcs#148, but I wanted to float this here first to see if there's an instant hell-no.

Priorities

Add 👍 to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureFeature request or proposallanguageThe Nix expression language; parser, interpreter, primops, evaluation, etc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions