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
28 changes: 19 additions & 9 deletions programs/shfmt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,40 @@ in
options.programs.shfmt = {
indent_size = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = 2;
default = if cfg.useEditorConfig then null else 2;
example = 4;
description = ''
Sets the number of spaces to be used in indentation. Uses tabs if set to
zero. If this is null, then [.editorconfig will be used to configure
shfmt](https://github.com/patrickvane/shfmt#description).
zero.
'';
};

simplify = lib.mkOption {
type = lib.types.bool;
default = true;
default = !cfg.useEditorConfig;
description = ''
Enables the `-s` (`--simplify`) flag, which simplifies code where possible.
'';
};

useEditorConfig = lib.mkEnableOption ''
Use .editorconfig file for
formatting. This is mutually exlusive with all other settings. See [shfmt
docs](https://github.com/mvdan/sh/blob/v3.12.0/cmd/shfmt/shfmt.1.scd?plain=1#L20-L21).
'';
};

config = lib.mkIf cfg.enable {
settings.formatter.shfmt.options =
(lib.optionals (!isNull cfg.indent_size) [
"-i"
(toString cfg.indent_size)
])
++ (lib.optionals (cfg.simplify) [ "-s" ]);
let
options =
(lib.optionals (!isNull cfg.indent_size) [
"-i"
(toString cfg.indent_size)
])
++ (lib.optionals (cfg.simplify) [ "-s" ]);
in
assert cfg.useEditorConfig -> builtins.length options == 0;
options;
};
}