diff options
author | Robert Hensing <robert@roberthensing.nl> | 2024-02-04 16:02:13 +0100 |
---|---|---|
committer | Robert Hensing <robert@roberthensing.nl> | 2024-02-04 16:02:13 +0100 |
commit | 542f5d4f4d80a35d8f03aa5cf2a2a0b1a0345c41 (patch) | |
tree | da212399dd57321fd9256a227c96d5269a0053b9 /lib | |
parent | bd285d2c11a4d906ba5a15ee2472f33dfa113546 (diff) | |
download | nixlib-542f5d4f4d80a35d8f03aa5cf2a2a0b1a0345c41.tar nixlib-542f5d4f4d80a35d8f03aa5cf2a2a0b1a0345c41.tar.gz nixlib-542f5d4f4d80a35d8f03aa5cf2a2a0b1a0345c41.tar.bz2 nixlib-542f5d4f4d80a35d8f03aa5cf2a2a0b1a0345c41.tar.lz nixlib-542f5d4f4d80a35d8f03aa5cf2a2a0b1a0345c41.tar.xz nixlib-542f5d4f4d80a35d8f03aa5cf2a2a0b1a0345c41.tar.zst nixlib-542f5d4f4d80a35d8f03aa5cf2a2a0b1a0345c41.zip |
lib.option.mergeUniqueOption: Simplify and add warning about merge function
The previous code was optimized for the old uniq behavior, which did not call merge. That's changed, so the legacy path is not a hot path anymore, and is not worth any tech debt.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/options.nix | 27 |
1 files changed, 11 insertions, 16 deletions
diff --git a/lib/options.nix b/lib/options.nix index 03ae32d22916..eea5a091b408 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -266,24 +266,19 @@ rec { NOTE: When the type is not checked completely by check, pass a merge function for further checking (of sub-attributes, etc). */ - mergeUniqueOption = args@{ message, merge ? null }: - let - notUnique = loc: defs: + mergeUniqueOption = args@{ + message, + # WARNING: the default merge function assumes that the definition is a valid (option) value. You MUST pass a merge function if the return value needs to be + # - type checked beyond what .check does (which should be very litte; only on the value head; not attribute values, etc) + # - if you want attribute values to be checked, or list items + # - if you want coercedTo-like behavior to work + merge ? loc: defs: (head defs).value }: + loc: defs: + if length defs == 1 + then merge loc defs + else assert length defs > 1; throw "The option `${showOption loc}' is defined multiple times while it's expected to be unique.\n${message}\nDefinition values:${showDefs defs}\n${prioritySuggestion}"; - in - if merge == null - # The inner conditional could be factored out, but this way we take advantage of partial application. - then - loc: defs: - if length defs == 1 - then (head defs).value - else notUnique loc defs - else - loc: defs: - if length defs == 1 - then merge loc defs - else notUnique loc defs; /* "Merge" option definitions by checking that they all have the same value. */ mergeEqualOption = loc: defs: |