about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlucasew <lucas59356@gmail.com>2021-08-13 16:31:14 -0300
committerRobert Helgesson <robert@rycee.net>2021-08-25 14:02:59 +0200
commit83514ae7a9f25023c2bc7bec323e661b7302d61d (patch)
treee834ae9a1c2304d8016d1d92466ae884d802b8e3
parent346d5ce8a087cdd6a35c1fd34ee33c6bb79f2058 (diff)
downloadnixlib-83514ae7a9f25023c2bc7bec323e661b7302d61d.tar
nixlib-83514ae7a9f25023c2bc7bec323e661b7302d61d.tar.gz
nixlib-83514ae7a9f25023c2bc7bec323e661b7302d61d.tar.bz2
nixlib-83514ae7a9f25023c2bc7bec323e661b7302d61d.tar.lz
nixlib-83514ae7a9f25023c2bc7bec323e661b7302d61d.tar.xz
nixlib-83514ae7a9f25023c2bc7bec323e661b7302d61d.tar.zst
nixlib-83514ae7a9f25023c2bc7bec323e661b7302d61d.zip
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 <lucas59356@gmail.com>
-rw-r--r--nixos/doc/manual/from_md/release-notes/rl-2111.section.xml7
-rw-r--r--nixos/doc/manual/release-notes/rl-2111.section.md2
-rw-r--r--pkgs/pkgs-lib/formats.nix29
-rw-r--r--pkgs/pkgs-lib/tests/formats.nix26
4 files changed, 43 insertions, 21 deletions
diff --git a/nixos/doc/manual/from_md/release-notes/rl-2111.section.xml b/nixos/doc/manual/from_md/release-notes/rl-2111.section.xml
index 6d02f71ac989..39a7965a7ed4 100644
--- a/nixos/doc/manual/from_md/release-notes/rl-2111.section.xml
+++ b/nixos/doc/manual/from_md/release-notes/rl-2111.section.xml
@@ -1058,6 +1058,13 @@ Superuser created successfully.
           sign OCSP responses and server certificates.
         </para>
       </listitem>
+      <listitem>
+        <para>
+          <literal>lib.formats.yaml</literal>’s
+          <literal>generate</literal> will not generate JSON anymore,
+          but instead use more of the YAML-specific syntax.
+        </para>
+      </listitem>
     </itemizedlist>
   </section>
 </section>
diff --git a/nixos/doc/manual/release-notes/rl-2111.section.md b/nixos/doc/manual/release-notes/rl-2111.section.md
index a337a72b7166..0203215f11e8 100644
--- a/nixos/doc/manual/release-notes/rl-2111.section.md
+++ b/nixos/doc/manual/release-notes/rl-2111.section.md
@@ -302,3 +302,5 @@ To be able to access the web UI this port needs to be opened in the firewall.
 - Zfs: `latestCompatibleLinuxPackages` is now exported on the zfs package. One can use `boot.kernelPackages = config.boot.zfs.package.latestCompatibleLinuxPackages;` to always track the latest compatible kernel with a given version of zfs.
 
 - Nginx will use the value of `sslTrustedCertificate` if provided for a virtual host, even if `enableACME` is set. This is useful for providers not using the same certificate to sign OCSP responses and server certificates.
+
+- `lib.formats.yaml`'s `generate` will not generate JSON anymore, but instead use more of the YAML-specific syntax.
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
     '';
   };