Skip to content

Allow custom printing in nix repl #9380

Open
@infinisil

Description

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.

Metadata

Assignees

No one assigned

    Labels

    featureFeature request or proposalreplThe Read Eval Print Loop, "nix repl" command and debugger

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions