summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorEmery Hemingway <ehmry@posteo.net>2021-03-24 11:20:36 +0100
committerEmery Hemingway <ehmry@posteo.net>2021-04-01 14:29:57 +0200
commit1cd48efa96bc677bfbf7111bd116d092521c3914 (patch)
tree3b1a7f9e9fbcc86ab8108c7ea67268a87e27e9df /lib
parent9d0e4e9192b92b33d7362bd97bf5d8bfd78fa89a (diff)
downloadnixpkgs-1cd48efa96bc677bfbf7111bd116d092521c3914.tar
nixpkgs-1cd48efa96bc677bfbf7111bd116d092521c3914.tar.gz
nixpkgs-1cd48efa96bc677bfbf7111bd116d092521c3914.tar.bz2
nixpkgs-1cd48efa96bc677bfbf7111bd116d092521c3914.tar.lz
nixpkgs-1cd48efa96bc677bfbf7111bd116d092521c3914.tar.xz
nixpkgs-1cd48efa96bc677bfbf7111bd116d092521c3914.tar.zst
nixpkgs-1cd48efa96bc677bfbf7111bd116d092521c3914.zip
lib/generators: add toDhall
Diffstat (limited to 'lib')
-rw-r--r--lib/generators.nix24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index 501a23599f4..c8144db50ac 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -307,4 +307,28 @@ rec {
 ${expr "" v}
 </plist>'';
 
+  /* Translate a simple Nix expression to Dhall notation.
+   * Note that integers are translated to Integer and never
+   * the Natural type.
+  */
+  toDhall = { }@args: v:
+    with builtins;
+    let concatItems = lib.strings.concatStringsSep ", ";
+    in if isAttrs v then
+      "{ ${
+        concatItems (lib.attrsets.mapAttrsToList
+          (key: value: "${key} = ${toDhall args value}") v)
+      } }"
+    else if isList v then
+      "[ ${concatItems (map (toDhall args) v)} ]"
+    else if isInt v then
+      "${if v < 0 then "" else "+"}${toString v}"
+    else if isBool v then
+      (if v then "True" else "False")
+    else if isFunction v then
+      abort "generators.toDhall: cannot convert a function to Dhall"
+    else if isNull v then
+      abort "generators.toDhall: cannot convert a null to Dhall"
+    else
+      builtins.toJSON v;
 }