nvf/lib/types/custom.nix
2025-11-14 18:11:25 +01:00

31 lines
887 B
Nix

{lib}: let
inherit (lib.options) mergeEqualOption;
inherit (lib.lists) singleton;
inherit (lib.strings) isString stringLength match;
inherit (lib.types) listOf mkOptionType coercedTo;
in {
mergelessListOf = elemType:
mkOptionType {
name = "mergelessListOf";
description = "mergeless list of ${elemType.description or "values"}";
inherit (lib.types.listOf elemType) check;
merge = mergeEqualOption;
};
char = mkOptionType {
name = "char";
description = "character";
descriptionClass = "noun";
check = value: stringLength value < 2;
merge = mergeEqualOption;
};
hexColor = mkOptionType {
name = "hex-color";
descriptionClass = "noun";
description = "RGB color in hex format";
check = v: isString v && (match "#?[0-9a-fA-F]{6}" v) != null;
};
singleOrListOf = t: coercedTo t singleton (listOf t);
}