summaryrefslogtreecommitdiff
path: root/lib/lua.nix
blob: 704cdfa2196af9059aa598133d884e532c0825f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{lib, ...}: let
  fmt = val:
  # nil
    if val == null
    then "nil"
    # boolean
    else if builtins.isBool val
    then
      if val
      then "true"
      else "false"
    # number
    else if builtins.isInt val || builtins.isFloat val
    then toString val
    # string
    else if builtins.isString val
    then "'${val}'"
    # table (array)
    else if builtins.isList val
    then "{ " + (lib.concatStringsSep ", " (map fmt val)) + " }"
    # table (object)
    else if builtins.isAttrs val
    then
      "{ "
      + (lib.concatStringsSep ", "
        (lib.mapAttrsToList (k: v: "${k} = ${fmt v}") val))
      + " }"
    # invalid
    else throw "Unsupported value: ${toString val}";
in {
  fmt = fmt;
}