diff --git a/pkgs/pkgs-lib/formats.nix b/pkgs/pkgs-lib/formats.nix index f83a00b9e610..bbafa42f8ae6 100644 --- a/pkgs/pkgs-lib/formats.nix +++ b/pkgs/pkgs-lib/formats.nix @@ -126,27 +126,42 @@ optionalAttrs allowAliases aliases php = (import ./formats/php/default.nix { inherit lib pkgs; }).format; + /* + Creates a structured value type suitable for serialization formats. + + Parameters: + - typeName: String describing the format (e.g. "JSON", "YAML", "XML") + + 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; + json = { }: { - type = - let - valueType = - nullOr (oneOf [ - bool - int - float - str - path - (attrsOf valueType) - (listOf valueType) - ]) - // { - description = "JSON value"; - }; - in - valueType; + type = mkStructuredType { typeName = "JSON"; }; generate = name: value: @@ -160,7 +175,7 @@ optionalAttrs allowAliases aliases preferLocalBuild = true; } '' - jq . "$valuePath"> $out + jq . "$valuePath" > $out '' ) { }; @@ -187,23 +202,7 @@ optionalAttrs allowAliases aliases '' ) { }; - type = - let - valueType = - nullOr (oneOf [ - bool - int - float - str - path - (attrsOf valueType) - (listOf valueType) - ]) - // { - description = "YAML 1.1 value"; - }; - in - valueType; + type = mkStructuredType { typeName = "YAML 1.1"; }; }; @@ -226,23 +225,7 @@ optionalAttrs allowAliases aliases '' ) { }; - type = - let - valueType = - nullOr (oneOf [ - bool - int - float - str - path - (attrsOf valueType) - (listOf valueType) - ]) - // { - description = "YAML 1.2 value"; - }; - in - valueType; + type = mkStructuredType { typeName = "YAML 1.2"; }; }; @@ -491,23 +474,7 @@ optionalAttrs allowAliases aliases { }: json { } // { - type = - let - valueType = - oneOf [ - bool - int - float - str - path - (attrsOf valueType) - (listOf valueType) - ] - // { - description = "TOML value"; - }; - in - valueType; + type = mkStructuredType { typeName = "TOML"; }; generate = name: value: @@ -540,23 +507,7 @@ optionalAttrs allowAliases aliases { }: json { } // { - type = - let - valueType = - nullOr (oneOf [ - bool - int - float - str - path - (attrsOf valueType) - (listOf valueType) - ]) - // { - description = "CDN value"; - }; - in - valueType; + type = mkStructuredType { typeName = "CDN"; }; generate = name: value: @@ -971,23 +922,9 @@ optionalAttrs allowAliases aliases pythonVars = { }: { - type = - let - valueType = - nullOr (oneOf [ - bool - float - int - path - str - (attrsOf valueType) - (listOf valueType) - ]) - // { - description = "Python value"; - }; - in - attrsOf valueType; + type = attrsOf (mkStructuredType { + typeName = "Python"; + }); lib = { mkRaw = value: { @@ -1067,23 +1004,7 @@ optionalAttrs allowAliases aliases }: if format == "badgerfish" then { - type = - let - valueType = - nullOr (oneOf [ - bool - int - float - str - path - (attrsOf valueType) - (listOf valueType) - ]) - // { - description = "XML value"; - }; - in - valueType; + type = mkStructuredType { typeName = "XML"; }; generate = name: value: