From ba3e91ed43c05a4a0984a6faa948949612fd113c Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 24 Jan 2022 01:07:32 +0100 Subject: lib.types: Add unique like uniq, but custom errors Couldn't extend types.uniq and it had a silly name anyway. Now we can have better error messages. --- lib/options.nix | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/options.nix') diff --git a/lib/options.nix b/lib/options.nix index 53001a3113f..44ec335545c 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -134,6 +134,12 @@ rec { throw "The unique option `${showOption loc}' is defined multiple times. Definition values:${showDefs defs}" else (head defs).value; + mergeUniqueOption = { message }: loc: defs: + if length defs == 1 + then (head defs).value + else assert length defs > 1; + throw "The option `${showOption loc}' is defined multiple times.\n${message}\nDefinition values:${showDefs defs}"; + /* "Merge" option definitions by checking that they all have the same value. */ mergeEqualOption = loc: defs: if defs == [] then abort "This case should never happen." -- cgit 1.4.1 From 8691ab3d47f1f9f94b51357fba7b8133cc8bcd88 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 24 Jan 2022 16:23:18 +0100 Subject: lib.modules: Define mergeOneOption in terms of mergeUniqueOption --- lib/options.nix | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'lib/options.nix') diff --git a/lib/options.nix b/lib/options.nix index 44ec335545c..ffe4b265166 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -128,11 +128,7 @@ rec { else if all isInt list && all (x: x == head list) list then head list else throw "Cannot merge definitions of `${showOption loc}'. Definition values:${showDefs defs}"; - mergeOneOption = loc: defs: - if defs == [] then abort "This case should never happen." - else if length defs != 1 then - throw "The unique option `${showOption loc}' is defined multiple times. Definition values:${showDefs defs}" - else (head defs).value; + mergeOneOption = mergeUniqueOption { message = ""; }; mergeUniqueOption = { message }: loc: defs: if length defs == 1 -- cgit 1.4.1