summaryrefslogtreecommitdiffstats
path: root/lib/generators.nix
diff options
context:
space:
mode:
Diffstat (limited to 'lib/generators.nix')
-rw-r--r--lib/generators.nix41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
new file mode 100644
index 0000000..e93d4b0
--- /dev/null
+++ b/lib/generators.nix
@@ -0,0 +1,41 @@
1{ lib, ... }:
2
3with lib;
4
5{
6 lua = {}: rec {
7 type = with types; let
8 valueType = nullOr
9 (oneOf [
10 bool
11 int
12 float
13 str
14 (attrsOf valueType)
15 (listOf valueType)
16 ]) // {
17 description = "Lua value";
18 }; in
19 valueType;
20
21 lib = {
22 mkRaw = value: { inherit value; _type = "raw"; };
23 };
24
25 generate = value:
26 let
27 list = l: "{ ${concatMapStringsSep ", " generate l} }";
28 attrs = a: "{ ${concatStringsSep ", " (mapAttrsToList (name: value: "[${generate name}] = ${generate value}") a)} }";
29 in
30 if value ? _type && value._type == "raw" then value.value
31 else if isInt value then toString value
32 else if isFloat value then toString value
33 else if isString value then "'${escape [ "'" ] value }'"
34 else if true == value then "true"
35 else if false == value then "false"
36 else if null == value then "nil"
37 else if isList value then list value
38 else if isAttrs value then attrs value
39 else abort "Lua value not supported: ${toPretty value}";
40 };
41}