Skip to content

lib/modules: Add moduleType parameter#147000

Merged
roberth merged 2 commits intoNixOS:masterfrom
hercules-ci:modules-moduleType
Nov 22, 2021
Merged

lib/modules: Add moduleType parameter#147000
roberth merged 2 commits intoNixOS:masterfrom
hercules-ci:modules-moduleType

Conversation

@roberth
Copy link
Member

@roberth roberth commented Nov 22, 2021

Motivation for this change

Follow-up to #143207. Allows the current configuration to be replicated and modified. Summarizing the test:

{ moduleType, ... }:
{
  options.variants = mkOption {
    type = types.lazyAttrsOf moduleType;
  };
}
  config = {
    list = [ 1 ];
    variants.foo.list = [ 2 ];
  }
assert config.variants.foo.list == [ 1 2 ]
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/)
  • 21.11 Release Notes (or backporting 21.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
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@roberth roberth changed the title Modules module type modules: Add moduleType parameter Nov 22, 2021
@roberth roberth changed the title modules: Add moduleType parameter lib/modules: Add moduleType parameter Nov 22, 2021
@roberth roberth added the 6.topic: module system About "NixOS" module system internals label Nov 22, 2021
@roberth roberth marked this pull request as draft November 22, 2021 13:53
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux. labels Nov 22, 2021
@roberth roberth force-pushed the modules-moduleType branch 3 times, most recently from 9bb0030 to b3178d5 Compare November 22, 2021 15:16
Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

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

Oh my, this is really neat! Other than the small nit I think this looks good.

@roberth roberth marked this pull request as ready for review November 22, 2021 16:08
@infinisil
Copy link
Member

I guess the only thing I have a slight worry about is name clashes in the module argument namespace. I don't think it's a big problem though

@roberth
Copy link
Member Author

roberth commented Nov 22, 2021

I guess the only thing I have a slight worry about is name clashes in the module argument namespace. I don't think it's a big problem though

I've thought about that too. type certainly wasn't specific enough, unlike in the evalModules output. moduleType wasn't used in Nixpkgs and I don't expect out-of-tree modules to use it either.

@roberth roberth merged commit 322b3bf into NixOS:master Nov 22, 2021
@nbp
Copy link
Member

nbp commented Nov 25, 2021

Whoa this is a highly specific use case, and strangely expressive.
However, as the test case demonstrate, there is a high risk of infinite recursions, if there is no override of the variant value to prevent such recursion.

Do you have examples where this might be used?

@roberth
Copy link
Member Author

roberth commented Dec 2, 2021

Whoa this is a highly specific use case, and strangely expressive.

💯

It is similar to extendModules, which is useful for features like NixOS specialisations and the alterations required for the NixOS docs generation to work.

However, as the test case demonstrate, there is a high risk of infinite recursions

It is productive recursion though. This is fine as long as you're not trying to compute something over all the data, which is generally not something you do in modules anyway, except in some "data" submodules (which I dislike) and in RFC42 style settings with a freeformType (which are ok).

if there is no override of the variant value to prevent such recursion.

You'd have to condition the type on a config value, which is awkward but possible. Dependent types, I guess.

Do you have examples where this might be used?

Besides the NixOS use cases, which use the same functionality via extendModules primarily, I also use it in a flake where I want to provide variations of a build and test that use a different version of an important dependency.

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

Labels

6.topic: module system About "NixOS" module system internals 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants