Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,23 @@ let
};
};

json =
let
valueType = nullOr (oneOf [
bool
int
float
str
path
(attrsOf valueType)
(listOf valueType)
]);
in
valueType
// {
description = "JSON value";
};

# Either value of type `t1` or `t2`.
either =
t1: t2:
Expand Down
7 changes: 7 additions & 0 deletions nixos/doc/manual/development/option-types.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,13 @@ 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.

## Submodule {#section-option-types-submodule}

`submodule` is a very powerful type that defines a set of sub-options
Expand Down
21 changes: 4 additions & 17 deletions pkgs/pkgs-lib/formats.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ let

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
Expand All @@ -72,23 +71,11 @@ let
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;
types.json
// {
description = "${typeName} value";
};

# 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)
Expand Down
Loading