From cca0b23070df00952cdc1c5dc8c252f81c8107f2 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Thu, 23 Nov 2023 16:40:06 -0800 Subject: [PATCH] Allow overriding formatter entirely Setting the `formatter` option now allows for setting the formatter directly instead of using the provided formatting functionality with `formatters`. --- api_guide.md | 5 ++ builtinModules/builtinFormatters.nix | 3 +- builtinModules/formatter.nix | 74 +++++++++++++++++----------- 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/api_guide.md b/api_guide.md index de5bd86..ba485bd 100644 --- a/api_guide.md +++ b/api_guide.md @@ -577,6 +577,11 @@ For example: ### 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 used by `nix fmt`. If formatters are set, Flakelight will export `formatter.${system}` outputs which apply all the configured formatters. diff --git a/builtinModules/builtinFormatters.nix b/builtinModules/builtinFormatters.nix index 86b2c3a..ed23a10 100644 --- a/builtinModules/builtinFormatters.nix +++ b/builtinModules/builtinFormatters.nix @@ -8,7 +8,8 @@ let in { options.flakelight.builtinFormatters = - mkEnableOption "default formatters" // { default = true; }; + mkEnableOption "default formatters" // + { default = config.formatter == null; }; config = mkIf config.flakelight.builtinFormatters { devShell.packages = pkgs: [ diff --git a/builtinModules/formatter.nix b/builtinModules/formatter.nix index 91ecf46..0c379b6 100644 --- a/builtinModules/formatter.nix +++ b/builtinModules/formatter.nix @@ -4,38 +4,54 @@ { config, src, lib, flakelight, ... }: let - inherit (lib) mkOption mkIf mapAttrsToList; - inherit (lib.types) lazyAttrsOf nullOr str; + inherit (lib) mkDefault mkMerge mkOption mkIf mapAttrsToList; + inherit (lib.types) lazyAttrsOf nullOr package str; inherit (flakelight.types) optFunctionTo; in { - options.formatters = mkOption { - type = nullOr (optFunctionTo (lazyAttrsOf str)); - default = null; - }; - - config = mkIf (config.formatters != null) { - perSystem = { pkgs, lib, fd, coreutils, ... }: { - formatter = pkgs.writeShellScriptBin "formatter" '' - PATH=${lib.makeBinPath (config.devShell.packages pkgs)} - for f in "$@"; do - if [ -d "$f" ]; then - ${fd}/bin/fd "$f" -Htf -x "$0" & - else - case "$(${coreutils}/bin/basename "$f")" in - ${toString (mapAttrsToList - (n: v: "${n}) ${v} \"$f\" & ;;") (config.formatters pkgs))} - esac - fi - done &>/dev/null - wait - ''; + options = { + formatter = mkOption { + type = nullOr (optFunctionTo package); + default = null; + }; + formatters = mkOption { + type = nullOr (optFunctionTo (lazyAttrsOf str)); + default = null; }; - - checks.formatting = { lib, outputs', diffutils, ... }: '' - ${lib.getExe outputs'.formatter} . - ${diffutils}/bin/diff -qr ${src} . |\ - sed 's/Files .* and \(.*\) differ/File \1 not formatted/g' - ''; }; + + config = mkMerge [ + (mkIf (config.formatter != null) { + perSystem = pkgs: { + formatter = config.formatter pkgs; + }; + }) + + (mkIf (config.formatters != null) { + perSystem = { pkgs, lib, fd, coreutils, ... }: { + formatter = mkDefault (pkgs.writeShellScriptBin "formatter" '' + PATH=${lib.makeBinPath (config.devShell.packages pkgs)} + for f in "$@"; do + if [ -d "$f" ]; then + ${fd}/bin/fd "$f" -Htf -x "$0" & + else + case "$(${coreutils}/bin/basename "$f")" in + ${toString (mapAttrsToList + (n: v: "${n}) ${v} \"$f\" & ;;") (config.formatters pkgs))} + esac + fi + done &>/dev/null + wait + ''); + }; + }) + + (mkIf ((config.formatters != null) || (config.formatter != null)) { + checks.formatting = { lib, outputs', diffutils, ... }: '' + ${lib.getExe outputs'.formatter} . + ${diffutils}/bin/diff -qr ${src} . |\ + sed 's/Files .* and \(.*\) differ/File \1 not formatted/g' + ''; + }) + ]; }