Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom printing in nix repl #9380

Open
infinisil opened this issue Nov 19, 2023 · 3 comments
Open

Allow custom printing in nix repl #9380

infinisil opened this issue Nov 19, 2023 · 3 comments
Labels
feature Feature request or proposal repl The Read Eval Print Loop, "nix repl" command and debugger

Comments

@infinisil
Copy link
Member

infinisil commented Nov 19, 2023

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

Sometimes there are attribute sets with a public interface but an internal structure, which when passed to nix repl currently print their internal structure by default. A good example is the lib.fileset library:

nix-repl> lib.fileset.unions [ ./. ]
{ _internalBase = /home/tweagysil/src/nixpkgs/filesets; _internalBaseComponents = [ ... ]; _internalBaseRoot = /; _internalIsEmptyWithoutBase = false; _internalTree = "directory"; _internalVersion = 3; _type = "fileset"; }

Related to that: Nixpkgs derivations are currently always printed as their .drv:

nix-repl> hello
«derivation /nix/store/g2mxdrkwr1hck4y5479dww7m56d1x81v-hello-2.12.1.drv»

When really, there's a lot more to the interface of a package than just its .drv, things like passthru, meta, .override attributes, etc. (notably not all attributes).

Describe the solution you'd like

A special attribute like __nixReplShowString, which defines a __toString-like function. When an attribute set with such an attribute gets passed to the REPL, instead of printing it recursively, it should just evaluate that attribute and print the resulting string:

nix-repl> { name = "Paul"; __nixReplShowString = self: "Hello, ${self.name}!"; }
Hello, Paul!

This should take precedence over the existing derivation printing, such that Nixpkgs can customise it more.

Describe alternatives you've considered

Additional context

This issue is sponsored by Antithesis

Priorities

Add 👍 to issues you find important.

@infinisil infinisil added feature Feature request or proposal repl The Read Eval Print Loop, "nix repl" command and debugger labels Nov 19, 2023
@roberth
Copy link
Member

roberth commented Nov 28, 2023

Custom functions are more fragile when it comes to possibly cyclic structures. Those are detected by the current printing routine. To recover that functionality, the special show function could return values in a tree form with value attributes for automatic deduplication.
Other reasons why returning structured data is nice, is that Nix could implement pretty printing, layouts, more semantically useful messages, etc.

@infinisil
Copy link
Member Author

I think it's fine to keep it simple in this case: __nixReplShowString must return a string, which is directly printed. I don't think we have a need for anything more powerful for now. And if we do in the future we could add another special more general special attribute for it.

@SuperSandro2000
Copy link
Member

We should definitely add a cli flag to turn this off in case of any issues for better debugging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature request or proposal repl The Read Eval Print Loop, "nix repl" command and debugger
Projects
None yet
Development

No branches or pull requests

3 participants