summary refs log tree commit diff
path: root/lib/generators.nix
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2016-11-06 14:14:24 +0100
committerProfpatsch <mail@profpatsch.de>2016-11-17 23:16:24 +0100
commit26eb10e771c2e7abdfe79d7f3db472af2e56a9fd (patch)
treeae0e1d736fb4f6a20c593d5ce94434a36dd301a6 /lib/generators.nix
parent61311665cb0f11c4688fc0013d732d85bbd66fca (diff)
downloadnixpkgs-26eb10e771c2e7abdfe79d7f3db472af2e56a9fd.tar
nixpkgs-26eb10e771c2e7abdfe79d7f3db472af2e56a9fd.tar.gz
nixpkgs-26eb10e771c2e7abdfe79d7f3db472af2e56a9fd.tar.bz2
nixpkgs-26eb10e771c2e7abdfe79d7f3db472af2e56a9fd.tar.lz
nixpkgs-26eb10e771c2e7abdfe79d7f3db472af2e56a9fd.tar.xz
nixpkgs-26eb10e771c2e7abdfe79d7f3db472af2e56a9fd.tar.zst
nixpkgs-26eb10e771c2e7abdfe79d7f3db472af2e56a9fd.zip
lib: add generator functions for toJSON & toYAML
They both reference the toJSON builtin, so we get semantic identifiers
that express the intent of the generation.
Both should be able to map each nix value (minus functions) to the
destination config files.

Includes two invocation unit tests.
Diffstat (limited to 'lib/generators.nix')
-rw-r--r--lib/generators.nix16
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index 90dd3371454..a1396873695 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -10,7 +10,7 @@ let
   flipMapAttrs = flip libAttr.mapAttrs;
 in
 
-{
+rec {
 
   /* Generates an INI-style config file from an
    * attrset of sections to an attrset of key-value pairs.
@@ -50,4 +50,18 @@ in
     in
       # map input to ini sections
       mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
+
+
+  /* Generates JSON from an arbitrary (non-function) value.
+    * For more information see the documentation of the builtin.
+    */
+  toJSON = {}: builtins.toJSON;
+
+
+  /* YAML has been a strict superset of JSON since 1.2, so we
+    * use toJSON. Before it only had a few differences referring
+    * to implicit typing rules, so it should work with older
+    * parsers as well.
+    */
+  toYAML = {}@args: toJSON args;
 }