lib.meta-types: add binary either/both combinators (#537659)

This commit is contained in:
adisbladis
2026-07-08 05:20:33 +00:00
committed by GitHub
2 changed files with 33 additions and 25 deletions
+26
View File
@@ -160,6 +160,32 @@ lib.fix (self: {
verify = v: all (func: func v) funcs;
};
either =
t1: t2:
assert isTypeDef t1 && isTypeDef t2;
let
# Store the functions directly so we don't have to pay the cost of attrset lookups at runtime.
v1 = t1.verify;
v2 = t2.verify;
in
{
name = "either<${t1.name},${t2.name}>";
verify = v: v1 v || v2 v;
};
both =
t1: t2:
assert isTypeDef t1 && isTypeDef t2;
let
# Store the functions directly so we don't have to pay the cost of attrset lookups at runtime.
v1 = t1.verify;
v2 = t2.verify;
in
{
name = "both<${t1.name},${t2.name}>";
verify = v: v1 v && v2 v;
};
not =
t:
assert isTypeDef t;
+7 -25
View File
@@ -304,21 +304,18 @@ let
types = import ../../../lib/meta-types.nix { inherit lib; };
inherit (types)
str
union
either
int
attrs
any
listOf
bool
record
intersection
both
not
derivation
;
platforms = listOf (union [
str
attrs
]); # see lib.meta.platformMatch
platforms = listOf (either str attrs); # see lib.meta.platformMatch
in
record {
# These keys are documented
@@ -326,31 +323,16 @@ let
mainProgram = str;
longDescription = str;
branch = str;
homepage = union [
(listOf str)
str
];
homepage = either str (listOf str);
donationPage = str;
downloadPage = str;
changelog = union [
(listOf str)
str
];
changelog = either str (listOf str);
license =
let
# TODO disallow `str` licenses, use a module
licenseType = union [
(intersection [
attrs
(not derivation)
])
str
];
licenseType = either (both attrs (not derivation)) str;
in
union [
(listOf licenseType)
licenseType
];
either licenseType (listOf licenseType);
sourceProvenance = listOf attrs;
maintainers = listOf attrs; # TODO use the maintainer type from lib/tests/maintainer-module.nix
nonTeamMaintainers = listOf attrs; # TODO use the maintainer type from lib/tests/maintainer-module.nix