1
1
forked from extern/flakelight

Allow overriding formatter entirely

Setting the `formatter` option now allows for setting the formatter
directly instead of using the provided formatting functionality with
`formatters`.
This commit is contained in:
Archit Gupta 2023-11-23 16:40:06 -08:00
parent 50982daa63
commit cca0b23070
3 changed files with 52 additions and 30 deletions

View File

@ -577,6 +577,11 @@ For example:
### formatters ### formatters
The `formatter` option allows you to set `formatter.${system}` outputs. It can
be set to a function that takes packages and returns the package to use. This
overrides the `formatters` functionality described below though, so for
configuring formatters for a file type, you likely want to use `formatters`.
The `formatters` option allows you to configure formatting tools that will be The `formatters` option allows you to configure formatting tools that will be
used by `nix fmt`. If formatters are set, Flakelight will export used by `nix fmt`. If formatters are set, Flakelight will export
`formatter.${system}` outputs which apply all the configured formatters. `formatter.${system}` outputs which apply all the configured formatters.

View File

@ -8,7 +8,8 @@ let
in in
{ {
options.flakelight.builtinFormatters = options.flakelight.builtinFormatters =
mkEnableOption "default formatters" // { default = true; }; mkEnableOption "default formatters" //
{ default = config.formatter == null; };
config = mkIf config.flakelight.builtinFormatters { config = mkIf config.flakelight.builtinFormatters {
devShell.packages = pkgs: [ devShell.packages = pkgs: [

View File

@ -4,19 +4,32 @@
{ config, src, lib, flakelight, ... }: { config, src, lib, flakelight, ... }:
let let
inherit (lib) mkOption mkIf mapAttrsToList; inherit (lib) mkDefault mkMerge mkOption mkIf mapAttrsToList;
inherit (lib.types) lazyAttrsOf nullOr str; inherit (lib.types) lazyAttrsOf nullOr package str;
inherit (flakelight.types) optFunctionTo; inherit (flakelight.types) optFunctionTo;
in in
{ {
options.formatters = mkOption { options = {
formatter = mkOption {
type = nullOr (optFunctionTo package);
default = null;
};
formatters = mkOption {
type = nullOr (optFunctionTo (lazyAttrsOf str)); type = nullOr (optFunctionTo (lazyAttrsOf str));
default = null; default = null;
}; };
};
config = mkIf (config.formatters != null) { config = mkMerge [
(mkIf (config.formatter != null) {
perSystem = pkgs: {
formatter = config.formatter pkgs;
};
})
(mkIf (config.formatters != null) {
perSystem = { pkgs, lib, fd, coreutils, ... }: { perSystem = { pkgs, lib, fd, coreutils, ... }: {
formatter = pkgs.writeShellScriptBin "formatter" '' formatter = mkDefault (pkgs.writeShellScriptBin "formatter" ''
PATH=${lib.makeBinPath (config.devShell.packages pkgs)} PATH=${lib.makeBinPath (config.devShell.packages pkgs)}
for f in "$@"; do for f in "$@"; do
if [ -d "$f" ]; then if [ -d "$f" ]; then
@ -29,13 +42,16 @@ in
fi fi
done &>/dev/null done &>/dev/null
wait wait
''; '');
}; };
})
(mkIf ((config.formatters != null) || (config.formatter != null)) {
checks.formatting = { lib, outputs', diffutils, ... }: '' checks.formatting = { lib, outputs', diffutils, ... }: ''
${lib.getExe outputs'.formatter} . ${lib.getExe outputs'.formatter} .
${diffutils}/bin/diff -qr ${src} . |\ ${diffutils}/bin/diff -qr ${src} . |\
sed 's/Files .* and \(.*\) differ/File \1 not formatted/g' sed 's/Files .* and \(.*\) differ/File \1 not formatted/g'
''; '';
}; })
];
} }