From e77b55e8e918cea37c09a3191fa809e67c221b6b Mon Sep 17 00:00:00 2001 From: valentin gagarin Date: Wed, 13 Nov 2024 15:24:41 +0100 Subject: [PATCH] add type for list of unique elements --- website/lib.nix | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/website/lib.nix b/website/lib.nix index 5071c0b5..b1075d4e 100644 --- a/website/lib.nix +++ b/website/lib.nix @@ -113,5 +113,32 @@ rec { }; nestedTypes.elemType = elemType; }; + + listOfUnique = elemType: + let + baseType = lib.types.listOf elemType; + in + baseType // { + merge = loc: defs: + let + # Keep track of which definition each value came from + defsWithValues = map + (def: + map (v: { inherit (def) file; value = v; }) def.value + ) + defs; + flatDefs = lib.flatten defsWithValues; + + # Check for duplicates while preserving source info + seen = builtins.foldl' + (acc: def: + if lib.lists.any (v: v.value == def.value) acc + then throw "The option `${lib.options.showOption loc}` has duplicate values (${toString def.value}) defined in ${def.file}" + else acc ++ [ def ] + ) [ ] + flatDefs; + in + map (def: def.value) seen; + }; }; }