Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 77 additions & 3 deletions programs/kdlfmt.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,88 @@
{ mkFormatterModule, ... }:
{
meta.maintainers = [ ];
lib,
pkgs,
config,
mkFormatterModule,
...
}:
let
cfg = config.programs.kdlfmt;
in
{
meta.maintainers = [ "tetov" ];

imports = [
(mkFormatterModule {
name = "kdlfmt";
args = [ "format" ];
args = [ cfg.formatCommand ];
includes = [
"*.kdl"
];
})
];

options.programs.kdlfmt = {
formatCommand = lib.mkOption {
type = lib.types.enum [
"format"
"check"
];
description = "The command to run kdlfmt with.";
default = "format";
example = "check";
};
kdlVersion = lib.mkOption {
type = lib.types.enum [
"v1"
"v2"
null
];
default = null;
description = "kdl specification to use. By default all versions are tried";
};
settings = lib.mkOption {
type =
with lib.types;
attrsOf (oneOf [
bool
int
str
]);
description = "kdlfmt configuration.";
default = { };
example = {
indent_size = 4;
use_tabs = true;
};
};
};

config = lib.mkIf cfg.enable {
settings.formatter.kdlfmt.options =
let
inherit (lib)
concatMapAttrsStringSep
isAttrs
isBool
toString
;
toSimpleKDL =
v:
if isAttrs v then
concatMapAttrsStringSep "\n" (key: value: "${key} ${toSimpleKDL value}") v
else if isBool v then
(if v then "#true" else "#false")
else
toString v;
kdlFile = pkgs.writeText "kdlfmt.kdl" (toSimpleKDL cfg.settings);
in
lib.optionals (cfg.settings != { }) [
"--config"
"${kdlFile}"
]
++ lib.optionals (cfg.kdlVersion != null) [
"--kdl-version"
cfg.kdlVersion
];
};
}