Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce `types.anything` #97119

Merged
merged 5 commits into from Sep 21, 2020
Merged

Introduce `types.anything` #97119

merged 5 commits into from Sep 21, 2020

Conversation

@Infinisil
Copy link
Member

@Infinisil Infinisil commented Sep 4, 2020

Motivation for this change

This introduces the new type types.anything, which implements a safe and reasonable merge strategy if you don't care about the type. Specifically:

  • It recursively merges attribute sets together
    • Except if it's string-coercible (aka, packages and co.), then only one definition is allowed
  • It doesn't concatenate list values together (since list concatenation doesn't always make sense)
  • If it's an atomic value (not a set or a list), all definitions must be equal
  • It applies mkIf's and co. for all values

This is in contrast with the often-used types.attrs and types.unspecified, which can be problematic because they don't adhere to above points. If both this PR and #97114 are merged, I can start deprecating those in favor of this one.

Note that the doc entry for this new type is intentionally kept small, because its behavior is very natural and doesn't need a special explanation.

Ping @rycee @roberth @Profpatsch

Things done
  • Wrote a doc entry for the new type
  • Wrote tests for the new type
@edolstra
Copy link
Member

@edolstra edolstra commented Sep 4, 2020

Hm, what's the use case for this? Presumably you always do care about the type of an option since you need to use it somewhere eventually.

@rycee
Copy link
Member

@rycee rycee commented Sep 4, 2020

@edolstra I don't think the anything type is very useful by itself but I would happily use, e.g., attrsOf anything instead of attrs or attrsOf unspecified. That is, when I know the type is an attribute set but not the specific content. I've encountered this mainly when defining settings-like options that gets serialized to some hierarchical configuration format. For example the programs.taskwarrier.config or programs.waybar.settings.*.modules options in Home Manager.

@Infinisil
Copy link
Member Author

@Infinisil Infinisil commented Sep 4, 2020

Yeah exactly, the intention is for this type to replace types.attrs and types.unspecified, which are currently used when the type isn't known, but as mentioned they have some surprising behavior which you wouldn't expect.

nixos/doc/manual/development/option-types.xml Outdated Show resolved Hide resolved
lib/types.nix Show resolved Hide resolved
Infinisil added 3 commits Sep 15, 2020
Previously it would error out for a single function definition
This new type has unsurprising merge behavior: Only attribute sets are
merged together (recursively), and only if they don't conflict.

This is in contrast to the existing types:
- types.attrs is problematic because later definitions completely
  override attributes of earlier definitions, and it doesn't support
  mkIf and co.
- types.unspecified is very similar to types.attrs, but it has smart
  merging behavior that often doesn't make sense, and it doesn't support
  all types
@Infinisil Infinisil force-pushed the Infinisil:types.anything branch from b1a984d to 329f046 Sep 15, 2020
Infinisil added 2 commits Sep 4, 2020
@Infinisil Infinisil force-pushed the Infinisil:types.anything branch from 329f046 to 3106993 Sep 15, 2020
@Infinisil
Copy link
Member Author

@Infinisil Infinisil commented Sep 15, 2020

Changes since the last revision:

  • I moved types.attrs to the bottom of the type list and added a warning about its problems
  • Added an example for types.anything in the docs
  • Fixed mergeEqualOption for when a single function is passed. Now it doesn't error out anymore. Also added a test case to types.anything for this
Copy link
Member

@Profpatsch Profpatsch left a comment

LGTM

@roberth roberth merged commit f3893d8 into NixOS:master Sep 21, 2020
17 checks passed
17 checks passed
tests tests
Details
action
Details
Evaluation Performance Report Evaluator Performance Report
Details
Wait for ofborg
Details
grahamcofborg-eval ^.^!
Details
grahamcofborg-eval-check-maintainers matching changed paths to changed attrs...
Details
grahamcofborg-eval-check-meta config.nix: checkMeta = true
Details
grahamcofborg-eval-darwin nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3106993"; rev="310699319b422210b70ebacf676007172c67c90b"; } ./pkgs/t
Details
grahamcofborg-eval-lib-tests nix-build --arg pkgs import ./. {} ./lib/tests/release.nix
Details
grahamcofborg-eval-nixos nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3106993"; rev="310699319b422210b70ebacf676007172c67c90b"; } ./nixos/
Details
grahamcofborg-eval-nixos-manual nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3106993"; rev="310699319b422210b70ebacf676007172c67c90b"; } ./nixos/
Details
grahamcofborg-eval-nixos-options nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3106993"; rev="310699319b422210b70ebacf676007172c67c90b"; } ./nixos/
Details
grahamcofborg-eval-nixpkgs-manual nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3106993"; rev="310699319b422210b70ebacf676007172c67c90b"; } ./pkgs/t
Details
grahamcofborg-eval-nixpkgs-tarball nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3106993"; rev="310699319b422210b70ebacf676007172c67c90b"; } ./pkgs/t
Details
grahamcofborg-eval-nixpkgs-unstable-jobset nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3106993"; rev="310699319b422210b70ebacf676007172c67c90b"; } ./pkgs/t
Details
grahamcofborg-eval-package-list nix-env -qa --json --file .
Details
grahamcofborg-eval-package-list-no-aliases nix-env -qa --json --file . --arg config { allowAliases = false; }
Details
@roberth
Copy link
Contributor

@roberth roberth commented Sep 21, 2020

Thanks @Infinisil !

@Infinisil Infinisil deleted the Infinisil:types.anything branch Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.