about summary refs log tree commit diff
path: root/nixpkgs/pkgs/pkgs-lib/formats.nix
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2021-01-10 07:13:44 +0000
committerAlyssa Ross <hi@alyssa.is>2021-01-12 14:07:16 +0000
commite2698550456abba83c6dcd5d5e5a9990a0b96f8a (patch)
tree79a56f0df3fa55e470d84b4dff6059fbf487ec18 /nixpkgs/pkgs/pkgs-lib/formats.nix
parent1cdc42df888dc98c347e03bd942ed9825a55bcb3 (diff)
parent84d74ae9c9cbed73274b8e4e00be14688ffc93fe (diff)
downloadnixlib-e2698550456abba83c6dcd5d5e5a9990a0b96f8a.tar
nixlib-e2698550456abba83c6dcd5d5e5a9990a0b96f8a.tar.gz
nixlib-e2698550456abba83c6dcd5d5e5a9990a0b96f8a.tar.bz2
nixlib-e2698550456abba83c6dcd5d5e5a9990a0b96f8a.tar.lz
nixlib-e2698550456abba83c6dcd5d5e5a9990a0b96f8a.tar.xz
nixlib-e2698550456abba83c6dcd5d5e5a9990a0b96f8a.tar.zst
nixlib-e2698550456abba83c6dcd5d5e5a9990a0b96f8a.zip
Merge commit '84d74ae9c9cbed73274b8e4e00be14688ffc93fe'
Diffstat (limited to 'nixpkgs/pkgs/pkgs-lib/formats.nix')
-rw-r--r--nixpkgs/pkgs/pkgs-lib/formats.nix109
1 files changed, 109 insertions, 0 deletions
diff --git a/nixpkgs/pkgs/pkgs-lib/formats.nix b/nixpkgs/pkgs/pkgs-lib/formats.nix
new file mode 100644
index 000000000000..14589f8ecdc3
--- /dev/null
+++ b/nixpkgs/pkgs/pkgs-lib/formats.nix
@@ -0,0 +1,109 @@
+{ lib, pkgs }:
+rec {
+
+  /*
+
+  Every following entry represents a format for program configuration files
+  used for `settings`-style options (see https://github.com/NixOS/rfcs/pull/42).
+  Each entry should look as follows:
+
+    <format> = <parameters>: {
+      #        ^^ Parameters for controlling the format
+
+      # The module system type most suitable for representing such a format
+      # The description needs to be overwritten for recursive types
+      type = ...;
+
+      # generate :: Name -> Value -> Path
+      # A function for generating a file with a value of such a type
+      generate = ...;
+
+    });
+  */
+
+
+  json = {}: {
+
+    type = with lib.types; let
+      valueType = nullOr (oneOf [
+        bool
+        int
+        float
+        str
+        (attrsOf valueType)
+        (listOf valueType)
+      ]) // {
+        description = "JSON value";
+      };
+    in valueType;
+
+    generate = name: value: pkgs.runCommandNoCC name {
+      nativeBuildInputs = [ pkgs.jq ];
+      value = builtins.toJSON value;
+      passAsFile = [ "value" ];
+    } ''
+      jq . "$valuePath"> $out
+    '';
+
+  };
+
+  # YAML has been a strict superset of JSON since 1.2
+  yaml = {}:
+    let jsonSet = json {};
+    in jsonSet // {
+      type = jsonSet.type // {
+        description = "YAML value";
+      };
+    };
+
+  ini = { listsAsDuplicateKeys ? false, ... }@args: {
+
+    type = with lib.types; let
+
+      singleIniAtom = nullOr (oneOf [
+        bool
+        int
+        float
+        str
+      ]) // {
+        description = "INI atom (null, bool, int, float or string)";
+      };
+
+      iniAtom =
+        if listsAsDuplicateKeys then
+          coercedTo singleIniAtom lib.singleton (listOf singleIniAtom) // {
+            description = singleIniAtom.description + " or a list of them for duplicate keys";
+          }
+        else
+          singleIniAtom;
+
+    in attrsOf (attrsOf iniAtom);
+
+    generate = name: value: pkgs.writeText name (lib.generators.toINI args value);
+
+  };
+
+  toml = {}: json {} // {
+    type = with lib.types; let
+      valueType = oneOf [
+        bool
+        int
+        float
+        str
+        (attrsOf valueType)
+        (listOf valueType)
+      ] // {
+        description = "TOML value";
+      };
+    in valueType;
+
+    generate = name: value: pkgs.runCommandNoCC name {
+      nativeBuildInputs = [ pkgs.remarshal ];
+      value = builtins.toJSON value;
+      passAsFile = [ "value" ];
+    } ''
+      json2toml "$valuePath" "$out"
+    '';
+
+  };
+}