pkgs: let
  lib = pkgs.lib;
  util = import ../. pkgs;

  inherit (lib)
    mapAttrsToList
    concatStringsSep
    concatMapStringsSep
    filter
    filterAttrs
    elem
    toList
    const
    flip
    ;
  inherit (util.fn) notP compose;
  inherit (util.str) isEmp;
  inherit (util.op) equal;

in
attrs: let
  toKDL = (import ./toKDL.nix pkgs).toKDL;

  quote = lib.strings.escapeNixString;

  byKey = compose const;

  formatOutput = name: config: let
    position =
      if config ? position
      then "\tposition x=${toString config.position.x} y=${toString config.position.y}"
      else "";
    otherConfig = filterAttrs (byKey <| notP <| equal "position") config;
    otherLines = mapAttrsToList (key: value: "\t${key} \"${toString value}\"") otherConfig;
    content = filter (notP isEmp) ([position] ++ otherLines);
  in
  ''
  output "${name}" {
    ${concatStringsSep "\n" content}
  }
  '';

  outputNodes =
    if attrs ? output
    then attrs.output |> mapAttrsToList formatOutput |> concatStringsSep "\n"
    else "";

  formatWRs = attr:
  ''
  window-rule {
    ${toKDL attr}
  }
  '';

  outputWRs =
    if attrs ? window-rule
    then concatMapStringsSep "\n" formatWRs attrs.window-rule
    else "";

  formatSpawnCmd = cmd:
    "spawn-at-startup "
    + toList cmd |> concatMapStringsSep " " quote;

  spawnAtStartupNodes =
    if attrs ? spawn-at-startup
    then concatMapStringsSep "\n" formatSpawnCmd attrs.spawn-at-startup
    else "";

  specialAttrs = ["output" "window-rule" "spawn-at-startup" "_extraConfig"];
  mainAttrs = filterAttrs (byKey <| notP <| flip elem specialAttrs) attrs;
  mainConfig = toKDL mainAttrs;

  extraConfig =
    if attrs ? _extraConfig
    then attrs._extraConfig
    else "";
in [mainConfig outputNodes outputWRs spawnAtStartupNodes extraConfig]
  |> filter (notP isEmp)
  |> concatStringsSep "\n\n"