From 83514ae7a9f25023c2bc7bec323e661b7302d61d Mon Sep 17 00:00:00 2001 From: lucasew Date: Fri, 13 Aug 2021 16:31:14 -0300 Subject: lib.formats.yaml: use well known YAML format The way `(lib.formats.yaml {}).generate` generates YAML is compliant because on YAML 1.2 spec JSON is a subset of YAML but it bugs people's minds and can lead to problems with software that is not compatible with YAML 1.2. This commit also changes the test of the generation function. Data validation/typing remains the same. See #133802. Signed-off-by: lucasew --- pkgs/pkgs-lib/formats.nix | 29 +++++++++++++++++++++++------ pkgs/pkgs-lib/tests/formats.nix | 26 +++++++++++--------------- 2 files changed, 34 insertions(+), 21 deletions(-) (limited to 'pkgs') diff --git a/pkgs/pkgs-lib/formats.nix b/pkgs/pkgs-lib/formats.nix index 44c8f9135439..5e17519d4ce1 100644 --- a/pkgs/pkgs-lib/formats.nix +++ b/pkgs/pkgs-lib/formats.nix @@ -48,14 +48,31 @@ rec { }; - # YAML has been a strict superset of JSON since 1.2 - yaml = {}: - let jsonSet = json {}; - in jsonSet // { - type = jsonSet.type // { + yaml = {}: { + + generate = name: value: pkgs.runCommand name { + nativeBuildInputs = [ pkgs.remarshal ]; + value = builtins.toJSON value; + passAsFile = [ "value" ]; + } '' + json2yaml "$valuePath" "$out" + ''; + + type = with lib.types; let + valueType = nullOr (oneOf [ + bool + int + float + str + path + (attrsOf valueType) + (listOf valueType) + ]) // { description = "YAML value"; }; - }; + in valueType; + + }; ini = { # Represents lists as duplicate keys diff --git a/pkgs/pkgs-lib/tests/formats.nix b/pkgs/pkgs-lib/tests/formats.nix index af19f6100eef..14cc85eff31d 100644 --- a/pkgs/pkgs-lib/tests/formats.nix +++ b/pkgs/pkgs-lib/tests/formats.nix @@ -72,21 +72,17 @@ in runBuildTests { path = ./formats.nix; }; expected = '' - { - "attrs": { - "foo": null - }, - "false": false, - "float": 3.141, - "list": [ - null, - null - ], - "null": null, - "path": "${./formats.nix}", - "str": "foo", - "true": true - } + attrs: + foo: null + 'false': false + float: 3.141 + list: + - null + - null + 'null': null + path: ${./formats.nix} + str: foo + 'true': true ''; }; -- cgit 1.4.1