fork/home-manager

git clone https://git.julia.blue/fork/home-manager

h7x4lib: add generator for KDLfce9dbfe

master
3.6 KiB102 linesraw
1{ lib }:
2
3{
4  toKDL = { }:
5    let
6      inherit (lib) concatStringsSep splitString mapAttrsToList any;
7      inherit (builtins) typeOf replaceStrings elem;
8
9      # ListOf String -> String
10      indentStrings = let
11        # Although the input of this function is a list of strings,
12        # the strings themselves *will* contain newlines, so you need
13        # to normalize the list by joining and resplitting them.
14        unlines = lib.splitString "\n";
15        lines = lib.concatStringsSep "\n";
16        indentAll = lines: concatStringsSep "\n" (map (x: "	" + x) lines);
17      in stringsWithNewlines: indentAll (unlines (lines stringsWithNewlines));
18
19      # String -> String
20      sanitizeString = replaceStrings [ "\n" ''"'' ] [ "\\n" ''\"'' ];
21
22      # OneOf [Int Float String Bool Null] -> String
23      literalValueToString = element:
24        lib.throwIfNot
25        (elem (typeOf element) [ "int" "float" "string" "bool" "null" ])
26        "Cannot convert value of type ${typeOf element} to KDL literal."
27        (if typeOf element == "null" then
28          "null"
29        else if element == false then
30          "false"
31        else if element == true then
32          "true"
33        else if typeOf element == "string" then
34          ''"${sanitizeString element}"''
35        else
36          toString element);
37
38      # Attrset Conversion
39      # String -> AttrsOf Anything -> String
40      convertAttrsToKDL = name: attrs:
41        let
42          optArgsString = lib.optionalString (attrs ? "_args")
43            (lib.pipe attrs._args [
44              (map literalValueToString)
45              (lib.concatStringsSep " ")
46              (s: s + " ")
47            ]);
48
49          optPropsString = lib.optionalString (attrs ? "_props")
50            (lib.pipe attrs._props [
51              (lib.mapAttrsToList
52                (name: value: "${name}=${literalValueToString value}"))
53              (lib.concatStringsSep " ")
54              (s: s + " ")
55            ]);
56
57          children =
58            lib.filterAttrs (name: _: !(elem name [ "_args" "_props" ])) attrs;
59        in ''
60          ${name} ${optArgsString}${optPropsString}{
61          ${indentStrings (mapAttrsToList convertAttributeToKDL children)}
62          }'';
63
64      # List Conversion
65      # String -> ListOf (OneOf [Int Float String Bool Null])  -> String
66      convertListOfFlatAttrsToKDL = name: list:
67        let flatElements = map literalValueToString list;
68        in "${name} ${concatStringsSep " " flatElements}";
69
70      # String -> ListOf Anything -> String
71      convertListOfNonFlatAttrsToKDL = name: list: ''
72        ${name} {
73        ${indentStrings (map (x: convertAttributeToKDL "-" x) list)}
74        }'';
75
76      # String -> ListOf Anything  -> String
77      convertListToKDL = name: list:
78        let elementsAreFlat = !any (el: elem (typeOf el) [ "list" "set" ]) list;
79        in if elementsAreFlat then
80          convertListOfFlatAttrsToKDL name list
81        else
82          convertListOfNonFlatAttrsToKDL name list;
83
84      # Combined Conversion
85      # String -> Anything  -> String
86      convertAttributeToKDL = name: value:
87        let vType = typeOf value;
88        in if elem vType [ "int" "float" "bool" "null" "string" ] then
89          "${name} ${literalValueToString value}"
90        else if vType == "set" then
91          convertAttrsToKDL name value
92        else if vType == "list" then
93          convertListToKDL name value
94        else
95          throw ''
96            Cannot convert type `(${typeOf value})` to KDL:
97              ${name} = ${toString value}
98          '';
99    in attrs: ''
100      ${concatStringsSep "\n" (mapAttrsToList convertAttributeToKDL attrs)}
101    '';
102}