Skip to content

Don't require ellipsis in NixOS module definitions.#254212

Open
vkryachko wants to merge 1 commit intoNixOS:masterfrom
vkryachko:module_ellipsis
Open

Don't require ellipsis in NixOS module definitions.#254212
vkryachko wants to merge 1 commit intoNixOS:masterfrom
vkryachko:module_ellipsis

Conversation

@vkryachko
Copy link
Contributor

@vkryachko vkryachko commented Sep 9, 2023

Description of changes

Having used callPackage a bit, it seems a bit odd and inconsistent that modules require ... in their signatures as opposed to figuring out the signature and passing only what's requested, like callPackage does.

Now I don't know if there is an actual reason for why it is important for modules to require ..., but I could not find any and upon a brief chat with @infinisil, it seemed like there might not be a reason. So if you know of a reason, please let me know.

Worth noting that, as currently implemented, it's is a breaking change, namely modules that use at-pattern arg capture don't work ({ ... }@args, args used to contain all available module args, but is now empty, see newly added failing test for details). I don't have any data on how common it is to write modules with at-pattern capturing, so would like to get some guidance on how important it is to preserve this behavior.

It's possible to make it a non-breaking change by detecting ... and/or @args with something like #8961, and passing all available arguments if detected. Similar prior art: #194992 #7317.

Would like to get your thoughts on the idea to see if there are any strong objections to pursuing this further.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.11 Release Notes (or backporting 23.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Having used `callPackage` a bit, it seems a bit odd and inconsistent that modules
require `...` in their signatures as opposed to figuring out the
signature and passing only what's requested, like `callPackage` does.

Now I don't know if there is an actual reason for why it is important
for modules to require `...`, but I could not find any and upon a brief
chat with @infinisil, it seemed like there might not be a reason. So if
you know of a reason, please let me know.

Worth noting that, as currently implemented, it's is a breaking change,
namely modules that use at-pattern arg capture don't work (`{ ...
}@args`, args used to contain all available module args, but is now
empty, see newly added failing test for details). I don't have any data
on how common it is to write modules with at-pattern capturing, so would
like to get some guidance on how important it is to preserve this
behavior.

It's possible to make it a non-breaking change by detecting `...` and/or `@args`
with something like NixOS#194992 or rather with a new builtin like
was proposed in NixOS#7317, and passing all available arguments if detected.

Would like to get your thoughts on the idea to see if there are any
strong objections to pursuing this further.
@github-actions github-actions bot added 6.topic: module system About "NixOS" module system internals 6.topic: lib The Nixpkgs function library labels Sep 9, 2023
@NixOSInfra NixOSInfra added the 12.first-time contribution This PR is the author's first one; please be gentle! label Sep 9, 2023
vkryachko added a commit to vkryachko/nix that referenced this pull request Sep 10, 2023
This function returns full information about the function, including its
formal arguments, whether formals have an ellipse, and optionally the
name of the @-pattern argument.

Motivation: it's sometimes useful to know if the function uses @-pattern
to know if it's safe to call it with callPackage-style, i.e. only
passing its "formals" to it. i.e. useful in
[254212](NixOS/nixpkgs#254212).
A similar change has previously bee proposed as well NixOS#7317
@Artturin Artturin requested a review from roberth September 11, 2023 05:03
Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is a breaking change

That's correct and I don't see the benefit of doing this.

Even with more function reflection info, I don't believe we should do this.
Reflection/introspection make programs divert from normal language semantics, so it should only be used as a last resort, such as to avoid breaking changes and help users migrate their code. For example NixOS/nix@e9e1897

Even in these cases, users should be migrated off the weird behavior to something that fits within normal language semantics, e.g. with a deprecation warning.

@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 19, 2024
@vkryachko
Copy link
Contributor Author

is a breaking change

Actually, it is likely not abreaking change after all.

I did some archeology on the module system source code and stumbled upon this PR #6794

Its description states the following

Explicit argument of modules: Due to the strictness of the deconstruction of attribute set, modules arguments are computed in a way which is similar to the callPackage function. This implies that one cannot write a module which expects any argument custom argument — other than config, lib and options — which is not explicitly named.

I've done some testing and this appears to be still the case, i.e. any custom args that I don't mention in the formals, does not appear in @args.

So if that's accurate, we can improve ergonomics of writing modules at a very low cost

@roberth wdyt? does the fact that it appears backwards compatible, change anything for you?

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 28, 2024
@roberth
Copy link
Member

roberth commented Mar 28, 2024

likely not a breaking change

Many modules will keep working, that is true, but it is a breaking change nonetheless. Consider this module:

top@{ ... }: {
  foo = { config, ... }: {
    bar = something config top.config.qux;
  }
  qux = <...>;
}

By simply merging this PR, we do break it, because top will be { }, which has no .config.

I think you're right that with better reflection, we could pick the right behavior, but that's not available yet. Unfortunately that means this will be blocked for a long time, considering Nixpkgs' range of Nix version support.

# operator is used to make the "args@{ ... }: with args.lib;" notation
# works.
in f (args // extraArgs);
in f (builtins.intersectAttrs (functionArgs f) (args // extraArgs));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like performance can improve slightly with this change:

Suggested change
in f (builtins.intersectAttrs (functionArgs f) (args // extraArgs));
in f extraArgs;

(and a rename would be in order)

@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 4, 2024
@FliegendeWurst FliegendeWurst added the 2.status: needs-changes This PR needs changes by the author label Dec 12, 2024
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Dec 12, 2024
@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 2, 2025
@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Apr 2, 2025
@nixpkgs-ci nixpkgs-ci bot removed the 12.first-time contribution This PR is the author's first one; please be gentle! label Nov 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 2.status: needs-changes This PR needs changes by the author 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: lib The Nixpkgs function library 6.topic: module system About "NixOS" module system internals

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

5 participants