query
On this page

toPlist

lib.generators.toPlist

Docs pulled from | This Revision | 10 minutes ago


Translate a simple Nix expression to Plist notation.

Inputs

Structured function argument
escape (optional, default: false)
If this option is true, XML special characters are escaped in string values and keys
Value
The value to be converted to Plist

Noogle detected

Implementation

The following is the current implementation of this function.

toPlist =
    {
      escape ? false,
    }:
    v:
    let
      expr =
        ind: x:
        if x == null then
          ""
        else if isBool x then
          bool ind x
        else if isInt x then
          int ind x
        else if isString x then
          str ind x
        else if isList x then
          list ind x
        else if isAttrs x then
          attrs ind x
        else if isPath x then
          str ind (toString x)
        else if isFloat x then
          float ind x
        else
          abort "generators.toPlist: should never happen (v = ${v})";

      literal = ind: x: ind + x;

      maybeEscapeXML = if escape then escapeXML else x: x;

      bool = ind: x: literal ind (if x then "<true/>" else "<false/>");
      int = ind: x: literal ind "<integer>${toString x}</integer>";
      str = ind: x: literal ind "<string>${maybeEscapeXML x}</string>";
      key = ind: x: literal ind "<key>${maybeEscapeXML x}</key>";
      float = ind: x: literal ind "<real>${toString x}</real>";

      indent = ind: expr "\t${ind}";

      item = ind: concatMapStringsSep "\n" (indent ind);

      list =
        ind: x:
        concatStringsSep "\n" [
          (literal ind "<array>")
          (item ind x)
          (literal ind "</array>")
        ];

      attrs =
        ind: x:
        concatStringsSep "\n" [
          (literal ind "<dict>")
          (attr ind x)
          (literal ind "</dict>")
        ];

      attr =
        let
          attrFilter = name: value: name != "_module" && value != null;
        in
        ind: x:
        concatStringsSep "\n" (
          flatten (
            mapAttrsToList (
              name: value:
              optionals (attrFilter name value) [
                (key "\t${ind}" name)
                (expr "\t${ind}" value)
              ]
            ) x
          )
        );

    in
    # TODO: As discussed in #356502, deprecated functionality should be removed sometime after 25.11.
    lib.warnIf (!escape && lib.oldestSupportedReleaseIsAtLeast 2505)
      "Using `lib.generators.toPlist` without `escape = true` is deprecated"
      ''
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
        <plist version="1.0">
        ${expr "" v}
        </plist>'';