From 72121d09b7ba68bbc23c165bc306b826d7416a98 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Wed, 8 Jan 2025 10:48:54 +0000 Subject: [PATCH 1/3] fix: on-unmatched option Also moves excludes in the root of the settings, as global has been deprecated. Signed-off-by: Brian McGee --- module-options.nix | 41 +++++++++++++++++++++-------------------- treefmt.nix | 2 +- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/module-options.nix b/module-options.nix index 8379b52e..fe8d3915 100644 --- a/module-options.nix +++ b/module-options.nix @@ -28,27 +28,28 @@ let default = { }; description = "The contents of treefmt.toml"; type = types.submodule { + imports = [ + (lib.mkRenamedOptionModule [ "global" "excludes" ] [ "excludes" ]) + (lib.mkRenamedOptionModule [ "global" "on-unmatched" ] [ "on-unmatched" ]) + ]; freeformType = configFormat.type; options = { - global = { - excludes = mkOption { - description = "A global list of paths to exclude. Supports glob."; - type = types.listOf types.str; - default = [ ]; - example = [ "node_modules/*" ]; - }; - - on-unmatched = mkOption { - description = "Log paths that did not match any formatters at the specified log level."; - type = types.enum [ - "debug" - "info" - "warn" - "error" - "fatal" - ]; - default = "warn"; - }; + excludes = mkOption { + description = "A global list of paths to exclude. Supports glob."; + type = types.listOf types.str; + default = [ ]; + example = [ "node_modules/*" ]; + }; + on-unmatched = mkOption { + description = "Log paths that did not match any formatters at the specified log level."; + type = types.enum [ + "debug" + "info" + "warn" + "error" + "fatal" + ]; + default = "warn"; }; formatter = mkOption { @@ -87,7 +88,7 @@ let }; }; config = { - global.excludes = lib.mkIf config.enableDefaultExcludes [ + excludes = lib.mkIf config.enableDefaultExcludes [ # generated lock files i.e. yarn, cargo, nix flakes "*.lock" # Files generated by patch diff --git a/treefmt.nix b/treefmt.nix index f4270c30..1721a60d 100644 --- a/treefmt.nix +++ b/treefmt.nix @@ -1,7 +1,7 @@ { pkgs, ... }: { projectRootFile = "treefmt.nix"; - settings.global.excludes = [ "*.toml" ]; + settings.excludes = [ "*.toml" ]; programs.deadnix.enable = true; programs.deno.enable = pkgs.hostPlatform.system != "riscv64-linux"; From 5eb7434820f549f58c12a384b87ee73359c04c7b Mon Sep 17 00:00:00 2001 From: zimbatm Date: Wed, 8 Jan 2025 15:29:10 +0100 Subject: [PATCH 2/3] fix: avoid putting empty keys in the config file --- module-options.nix | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/module-options.nix b/module-options.nix index fe8d3915..49cdea7c 100644 --- a/module-options.nix +++ b/module-options.nix @@ -23,6 +23,17 @@ let configFormat = pkgs.formats.toml { }; + # Remove keys in the setting that are "empty" to keep the config file lean + emptySettingsKeys = + lib.optional (config.settings.excludes == [ ]) "excludes" + ++ lib.optional (config.settings.on-unmatched == null) "on-unmatched" + # Remove deprecated 'global' key (created by mkRenamedOptionModule for backwards compatibility) + ++ [ "global" ]; + + settingsData = builtins.removeAttrs config.settings emptySettingsKeys; + + configFile = configFormat.generate "treefmt.toml" settingsData; + # The schema of the treefmt.toml data structure. configSchema = mkOption { default = { }; @@ -40,16 +51,19 @@ let default = [ ]; example = [ "node_modules/*" ]; }; + on-unmatched = mkOption { description = "Log paths that did not match any formatters at the specified log level."; - type = types.enum [ - "debug" - "info" - "warn" - "error" - "fatal" - ]; - default = "warn"; + type = types.nullOr ( + types.enum [ + "debug" + "info" + "warn" + "error" + "fatal" + ] + ); + default = null; }; formatter = mkOption { @@ -328,7 +342,7 @@ in # Config config.build = { - configFile = configFormat.generate "treefmt.toml" config.settings; + inherit configFile; devShell = pkgs.mkShell { nativeBuildInputs = [ config.build.wrapper ] ++ (lib.attrValues config.build.programs); }; From e523901d6f621b8baa8c211b8c3e7d389b3f5355 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Sun, 11 Jan 2026 19:58:35 +0100 Subject: [PATCH 3/3] fix: fix example generation --- checks/default.nix | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/checks/default.nix b/checks/default.nix index 808bdba4..904b44f4 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -10,6 +10,14 @@ let programs.${name}.enable = true; }; + # Generate example config with only the formatter section (no default excludes) + toExampleConfig = + name: + treefmt-nix.mkConfigFile pkgs { + enableDefaultExcludes = false; + programs.${name}.enable = true; + }; + # Helper to get meta attributes for a specific formatter getFormatterMeta = name: @@ -78,23 +86,23 @@ let }) usableFormatterNames ); + # Example configs without default excludes + exampleConfigs = lib.listToAttrs ( + map (name: { + name = "formatter-${name}"; + value = toExampleConfig name; + }) (lib.filter (n: !shouldSkipExample n) usableFormatterNames) + ); + examples = let - configs = - lib.mapAttrs - (name: value: '' - { - echo "# Example generated by ../examples.sh" - sed -n '/^$/q;p' ${value} | sed 's|\(command = "\).*/\([^"]\+"\)|\1\2|' | sed 's|/nix/store/.*-||' - } > "$out/${name}.toml" - '') - ( - lib.filterAttrs ( - n: _: - # Filter out formatters to skip in example generation - !shouldSkipExample (lib.removePrefix "formatter-" n) - ) programConfigs - ); + configs = lib.mapAttrs (name: value: '' + { + echo "# Example generated by ../examples.sh" + # Clean up store paths from command + sed 's|\(command = "\).*/\([^"]\+"\)|\1\2|' ${value} | sed 's|/nix/store/.*-||' + } > "$out/${name}.toml" + '') exampleConfigs; in pkgs.runCommand "examples" { } '' mkdir $out