Loading lib/types.nix +39 −0 Original line number Diff line number Diff line Loading @@ -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: Loading nixos/doc/manual/development/option-types.section.md +14 −0 Original line number Diff line number Diff line Loading @@ -497,6 +497,20 @@ Composed types are types that take a type as parameter. `listOf value of type *`to`*. Can be used to preserve backwards compatibility of an option if its type was changed. `types.json` : A type representing JSON-compatible values. This includes `null`, booleans, integers, floats, strings, paths, attribute sets, and lists. Attribute sets and lists can be arbitrarily nested and contain any JSON-compatible values. `types.toml` : A type representing TOML-compatible values. This includes booleans, integers, floats, strings, paths, attribute sets, and lists. Attribute sets and lists can be arbitrarily nested and contain any TOML-compatible values. ## Submodule {#section-option-types-submodule} `submodule` is a very powerful type that defines a set of sub-options Loading pkgs/pkgs-lib/formats.nix +8 −42 Original line number Diff line number Diff line Loading @@ -40,6 +40,7 @@ let ; inherit (lib.types) serializableValueWith attrsOf atom bool Loading @@ -58,38 +59,6 @@ let submodule ; /* Creates a structured 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 */ mkStructuredType = { 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; # Attributes added accidentally in https://github.com/NixOS/nixpkgs/pull/335232 (2024-08-18) # Deprecated in https://github.com/NixOS/nixpkgs/pull/415666 (2025-06) allowAliases = pkgs.config.allowAliases or false; Loading Loading @@ -162,7 +131,7 @@ optionalAttrs allowAliases aliases { }: { type = mkStructuredType { typeName = "JSON"; }; type = types.json; generate = name: value: Loading Loading @@ -203,7 +172,7 @@ optionalAttrs allowAliases aliases '' ) { }; type = mkStructuredType { typeName = "YAML 1.1"; }; type = serializableValueWith { typeName = "YAML 1.1"; }; }; Loading @@ -226,7 +195,7 @@ optionalAttrs allowAliases aliases '' ) { }; type = mkStructuredType { typeName = "YAML 1.2"; }; type = serializableValueWith { typeName = "YAML 1.2"; }; }; Loading Loading @@ -487,10 +456,7 @@ optionalAttrs allowAliases aliases { }: json { } // { type = mkStructuredType { typeName = "TOML"; nullable = false; }; type = types.toml; generate = name: value: Loading Loading @@ -523,7 +489,7 @@ optionalAttrs allowAliases aliases { }: json { } // { type = mkStructuredType { typeName = "CDN"; }; type = serializableValueWith { typeName = "CDN"; }; generate = name: value: Loading Loading @@ -938,7 +904,7 @@ optionalAttrs allowAliases aliases pythonVars = { }: { type = attrsOf (mkStructuredType { type = attrsOf (serializableValueWith { typeName = "Python"; }); Loading Loading @@ -1020,7 +986,7 @@ optionalAttrs allowAliases aliases }: if format == "badgerfish" then { type = mkStructuredType { typeName = "XML"; }; type = serializableValueWith { typeName = "XML"; }; generate = name: value: Loading Loading
lib/types.nix +39 −0 Original line number Diff line number Diff line Loading @@ -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: Loading
nixos/doc/manual/development/option-types.section.md +14 −0 Original line number Diff line number Diff line Loading @@ -497,6 +497,20 @@ Composed types are types that take a type as parameter. `listOf value of type *`to`*. Can be used to preserve backwards compatibility of an option if its type was changed. `types.json` : A type representing JSON-compatible values. This includes `null`, booleans, integers, floats, strings, paths, attribute sets, and lists. Attribute sets and lists can be arbitrarily nested and contain any JSON-compatible values. `types.toml` : A type representing TOML-compatible values. This includes booleans, integers, floats, strings, paths, attribute sets, and lists. Attribute sets and lists can be arbitrarily nested and contain any TOML-compatible values. ## Submodule {#section-option-types-submodule} `submodule` is a very powerful type that defines a set of sub-options Loading
pkgs/pkgs-lib/formats.nix +8 −42 Original line number Diff line number Diff line Loading @@ -40,6 +40,7 @@ let ; inherit (lib.types) serializableValueWith attrsOf atom bool Loading @@ -58,38 +59,6 @@ let submodule ; /* Creates a structured 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 */ mkStructuredType = { 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; # Attributes added accidentally in https://github.com/NixOS/nixpkgs/pull/335232 (2024-08-18) # Deprecated in https://github.com/NixOS/nixpkgs/pull/415666 (2025-06) allowAliases = pkgs.config.allowAliases or false; Loading Loading @@ -162,7 +131,7 @@ optionalAttrs allowAliases aliases { }: { type = mkStructuredType { typeName = "JSON"; }; type = types.json; generate = name: value: Loading Loading @@ -203,7 +172,7 @@ optionalAttrs allowAliases aliases '' ) { }; type = mkStructuredType { typeName = "YAML 1.1"; }; type = serializableValueWith { typeName = "YAML 1.1"; }; }; Loading @@ -226,7 +195,7 @@ optionalAttrs allowAliases aliases '' ) { }; type = mkStructuredType { typeName = "YAML 1.2"; }; type = serializableValueWith { typeName = "YAML 1.2"; }; }; Loading Loading @@ -487,10 +456,7 @@ optionalAttrs allowAliases aliases { }: json { } // { type = mkStructuredType { typeName = "TOML"; nullable = false; }; type = types.toml; generate = name: value: Loading Loading @@ -523,7 +489,7 @@ optionalAttrs allowAliases aliases { }: json { } // { type = mkStructuredType { typeName = "CDN"; }; type = serializableValueWith { typeName = "CDN"; }; generate = name: value: Loading Loading @@ -938,7 +904,7 @@ optionalAttrs allowAliases aliases pythonVars = { }: { type = attrsOf (mkStructuredType { type = attrsOf (serializableValueWith { typeName = "Python"; }); Loading Loading @@ -1020,7 +986,7 @@ optionalAttrs allowAliases aliases }: if format == "badgerfish" then { type = mkStructuredType { typeName = "XML"; }; type = serializableValueWith { typeName = "XML"; }; generate = name: value: Loading