diff options
| author | Johannes Kirschbauer <hsjobeki@gmail.com> | 2026-01-07 17:14:55 +0100 |
|---|---|---|
| committer | Johannes Kirschbauer <hsjobeki@gmail.com> | 2026-01-20 21:26:05 +0100 |
| commit | c988f82f7a5f32fbf8d2fd3efb5aec8b3d5b9316 (patch) | |
| tree | 3e4fbf76a1bcf478c65fc5888ea7065aadb9d0b1 /lib | |
| parent | wrangler: 4.59.1 -> 4.59.3 (#482030) (diff) | |
| download | nixpkgs-c988f82f7a5f32fbf8d2fd3efb5aec8b3d5b9316.tar.gz | |
lib/types: add types.{json,toml} from pkgs.formats
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/types.nix | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/types.nix b/lib/types.nix index bd9ac93df472..1a98b12bd5ca 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -1437,6 +1437,45 @@ rec { }; }; + /** + Creates a value type suitable for serialization formats. + + Parameters: + - typeName: String describing the format (e.g. "JSON", "YAML", "XML") + - nullable: Whether the structured value type allows `null` values. + + Returns a type suitable for structured data formats that supports: + - Basic types: boolean, integer, float, string, path + - Complex types: attribute sets and lists + */ + serializableValueWith = + { + typeName, + nullable ? true, + }: + let + baseType = oneOf [ + bool + int + float + str + path + (attrsOf valueType) + (listOf valueType) + ]; + valueType = (if nullable then nullOr baseType else baseType) // { + description = "${typeName} value"; + }; + in + valueType; + + json = serializableValueWith { typeName = "JSON"; }; + + toml = serializableValueWith { + typeName = "TOML"; + nullable = false; + }; + # Either value of type `t1` or `t2`. either = t1: t2: |
