[Nix#9380] Allow custom printing in nix repl #99

Open
opened 2024-03-16 06:44:54 +00:00 by lix-bot · 0 comments
Member

Upstream-Issue: NixOS/nix#9380

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.

Upstream-Issue: https://git.lix.systems/NixOS/nix/issues/9380 ### 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](https://github.com/NixOS/nixpkgs/pull/217243)). ### 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](https://antithesis.com/) :sparkles: ### Priorities Add :+1: to [issues you find important](https://github.com/NixOS/nix/issues?q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc).
lix-bot added the
imported
label 2024-03-16 06:44:54 +00:00
jade added the
Area/repl
label 2024-05-30 07:13:14 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: lix-project/lix#99
No description provided.