Skip to content

Extend function reflection #10350

Open
Open

Description

@roberth

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

Function reflection is an important part of everyday use of the Nix language, in callPackage and the module system, to improve syntax and to make strict functions lazy.

However, the information is too limited, creating pitfalls that "library" code such as callPackage can not currently compensate.

Furthermore, it prevents improvements to be made without breaking compatibility:

Describe the solution you'd like

Two solutions have been already been proposed:

They could hardly be more different. Open design questions:

  • One primop returning attrset or multiple primops?
  • Use names that relate to the syntax or names that relate to the semantics?
    • Syntax-based: arg, args, ellipsis, hasArgs
    • Semantics-based: bindsAllAttrs, args, isOpen, strict

Currently implemented by neither:

  • Support __functor.

Describe alternatives you've considered

  • Reject function reflection altogether. This would make the language more predictable, but that ship has sailed a very long time ago.
  • "I want as little reflection as possible, because reflection is complicated and the language should be simple."
    • This suggests a linear relation between the "amount of reflection" and difficulty, which is a simplistic view that's false.
    • Everyday users don't need to interact with this newly exposed info at all. They do get to enjoy more robust reflection.
  • "This will cause reflection to be used more often and cause never ending problems. What else do we need to expose next?"
    • More use isn't all that bad, because users are already forced to interact with functionArgs through callPackage and the module system.
    • I believe the proposed interfaces cover all relevant function behaviors, and it's unlikely that we'll need more until perhaps the language itself is extended.

Additional context

Priorities

Add 👍 to issues you find important.

Activity

  1. added
    feature
    error-messages
    language
    on Mar 28, 2024
  2. anna328p commented on Mar 28, 2024

    @anna328p
    Member
  3. roberth commented on Mar 28, 2024

    @roberth
    MemberAuthor

    @infinisil has also written something like that in NixOS/nixpkgs#194992

    I suppose it's an extra argument for making this info available: it already is available.
    Of course it shouldn't have to be hacky or slow.

  4. moved this from To triage to ⚖ To discuss in Nix teamon Jun 19, 2024
  5. ShamrockLee commented on Aug 27, 2026

    @ShamrockLee
    Contributor

    If implemented, Nixpkgs developers no longer have to specify lib.extendDrvArgNames's inheritFunctionArgs manually. (See #10350 (comment) for details.)

    In addition, the callable set support should be handled on the Nixpkgs side, e.g., by having lib.functionInfo read specific callable set attributes.

  6. roberth commented on Nov 3, 2026

    @roberth
    MemberAuthor

    Worth making a decision here, also for improving the module system performance and legibility:

  7. ShamrockLee commented on Nov 4, 2026

    @ShamrockLee
    Contributor

    I'm not sufficiently familliar with the Nix internal to make such decision.
    The following opinions are from a Nix user's perspective.

    Worth making a decision here

    Reading the PR description again, I found my use case suitable to compare the syntatx-based vs. semantics-based interface.

    Typically, inheritFunctiinArgs is set depending on whether the function extenddrvArgs { } (the args: updatingArgs part of finalAttrs: args: updatingArgs) takes arbitrary attribute sets.

    • Semantics-based: (lib.functionInfo (extenddrvArgs { })).isOpen
    • Syntax-based: let fi = lib.functionInfo (extenddrvArgs { }); in fi.isFormal -> fi.hasEllipsis
      (fi.hasEllipsis -> fi.isFormal is always true.)

    I would lean toward the latter, for the filliwing reasins:

    • The syntax component is exhausive but the semantical properties are not.
    • The the semantics of isFormal and hasEllipsis syntax are clear and would hardly change even with future additions like { a, ...@extraArgs }: { }.

    On the other hand, the variable name of args in args: { } and { a, ... }@args: { } doesn't seem to carry any semantics.
    People who would need this piece of information is likely manipulating the *.nix files.
    Such information should only expose via, say, builtins.getAST.

    I found the attribute set output quite ergonomic, but extending it in the future means adding attributes to the output (not sure if it is tolerable).
    If adding attributes would not be possible, providing builtins.isFormal (or builtins.hasFormalSetPattern) and builtins.hasEllipsis seems more robust.

    The corresponding Nixpkgs lib functions could expose a richer interface if the developers would like to.

  8. nixos-discourse commented on Nov 9, 2026

    @nixos-discourse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    error-messagesfeaturelanguage

    Projects

    • Status
      ⚖ To discuss

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions