Skip to content
Merged
Show file tree
Hide file tree
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
38 changes: 23 additions & 15 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
45 changes: 30 additions & 15 deletions module-options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,47 @@ 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 = { };
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 = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be an mkRenamedOptionModule to notify users to migrate from global -> top level settings?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, fixed

excludes = mkOption {
description = "A global list of paths to exclude. Supports glob.";
type = types.listOf types.str;
default = [ ];
example = [ "node_modules/*" ];
};
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 [
on-unmatched = mkOption {
description = "Log paths that did not match any formatters at the specified log level.";
type = types.nullOr (
types.enum [
"debug"
"info"
"warn"
"error"
"fatal"
];
default = "warn";
};
]
);
default = null;
};

formatter = mkOption {
Expand Down Expand Up @@ -87,7 +102,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
Expand Down Expand Up @@ -327,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);
};
Expand Down
2 changes: 1 addition & 1 deletion treefmt.nix
Original file line number Diff line number Diff line change
@@ -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";
Expand Down