Skip to content

Commit a1bf64c

Browse files
committed
nixfmt-tree: refactor impl to use treefmt.withConfig
Refactor the new `nixfmt-tree` package, to make use of the new `treefmt.withConfig` wrapper. Breaking changes: 1. Overriding `settings` will no longer entirely replace all settings, instead settings are now merged as modules. 2. Adding the package to a nix shell will no longer make runtime inputs (such as `nixfmt`) available on the PATH, only the wrapped `treefmt` will be added to the shell's PATH. Non-breaking changes: 1. Introduced `runtimeInputs`, for consistency with `treefmt.withConfig` and other parts of nixpkgs, such as `writeShellApplication`. 2. Introduced `nixfmtPackage` to allow specifying a different nixfmt package, without having to know to override `nixfmt-rfc-style`. 3. Deprecated `runtimePackages` with a warning. Use the new args instead.
1 parent a432520 commit a1bf64c

File tree

1 file changed

+75
-38
lines changed

1 file changed

+75
-38
lines changed

pkgs/by-name/ni/nixfmt-tree/package.nix

Lines changed: 75 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,61 @@
11
{
22
lib,
33
runCommand,
4-
buildEnv,
5-
makeWrapper,
6-
writers,
74
treefmt,
85
nixfmt-rfc-style,
96
nixfmt-tree,
107

11-
settings ? {
12-
# The default is warn, which would be too annoying for people who just care about Nix
13-
on-unmatched = "info";
14-
# Assumes the user is using Git, fails if it's not
15-
tree-root-file = ".git/index";
16-
17-
formatter.nixfmt = {
18-
command = "nixfmt";
19-
includes = [ "*.nix" ];
20-
};
21-
},
22-
runtimePackages ? [
23-
nixfmt-rfc-style
24-
],
25-
}:
26-
buildEnv {
27-
name = "nixfmt-tree";
28-
29-
# Allows this derivation to be used as a shell providing both treefmt and nixfmt
30-
paths = [ treefmt ] ++ runtimePackages;
31-
pathsToLink = [ "/bin" ];
32-
33-
nativeBuildInputs = [
34-
makeWrapper
35-
];
36-
37-
postBuild = ''
38-
wrapProgram $out/bin/treefmt \
39-
--prefix PATH : $out/bin \
40-
--add-flags "--config-file ${writers.writeTOML "treefmt.toml" settings}"
41-
'';
8+
settings ? { },
9+
runtimeInputs ? [ ],
10+
nixfmtPackage ? nixfmt-rfc-style,
11+
12+
# NOTE: `runtimePackages` is deprecated. Use `nixfmtPackage` and/or `runtimeInputs`.
13+
runtimePackages ? [ nixfmtPackage ],
14+
}@args:
15+
let
16+
allRuntimeInputs = runtimePackages ++ runtimeInputs;
17+
18+
# Tests whether a package's main program is nixfmt.
19+
isNixfmt = p: p.meta.mainProgram or null == "nixfmt";
20+
21+
treefmtWithConfig = treefmt.withConfig {
22+
name = "nixfmt-tree";
23+
24+
settings = [
25+
# Default settings
26+
{
27+
_file = ./package.nix;
28+
29+
# Log level for files treefmt won't format
30+
# The default is warn, which would be too annoying for people who just care about Nix
31+
on-unmatched = lib.mkOptionDefault "info";
32+
33+
# Assume the tree is a Git repository, will fail if it's not
34+
tree-root-file = lib.mkOptionDefault ".git/index";
4235

36+
# NOTE: The `mkIf` condition should not be needed once `runtimePackages` is removed.
37+
formatter.nixfmt = lib.mkIf (lib.any isNixfmt allRuntimeInputs) {
38+
command = "nixfmt";
39+
includes = [ "*.nix" ];
40+
};
41+
}
42+
# User supplied settings
43+
{
44+
_file = "<nixfmt-tree args>";
45+
imports = lib.toList settings;
46+
}
47+
];
48+
49+
runtimeInputs =
50+
# Handle `runtimePackages` deprecation (added 2025-04-01)
51+
lib.warnIf (args ? runtimePackages) ''
52+
nixfmt-tree: overriding `runtimePackages` is deprecated, use `runtimeInputs` instead.
53+
Note: you do not need to supply a nixfmt package when using `runtimeInputs`, however you can override `nixfmtPackage` to a different nixfmt package.
54+
For additional flexibility, or to configure treefmt without nixfmt, consider using `treefmt.withConfig` instead of `nixfmt-tree`.
55+
'' allRuntimeInputs;
56+
};
57+
in
58+
treefmtWithConfig.overrideAttrs {
4359
meta = {
4460
mainProgram = "treefmt";
4561
description = "Official Nix formatter zero-setup starter using treefmt";
@@ -62,7 +78,7 @@ buildEnv {
6278
```
6379
6480
You can then also use `treefmt` in a pre-commit/pre-push [Git hook](https://git-scm.com/docs/githooks)
65-
and `nixfmt` with your editors format-on-save feature.
81+
and with your editor's format-on-save feature.
6682
6783
- To check formatting in CI, run the following in a checkout of your Git repository:
6884
```
@@ -71,9 +87,30 @@ buildEnv {
7187
7288
For more flexibility, you can customise this package using
7389
```nix
74-
nixfmt-tree.override {
75-
settings = { /* treefmt config */ };
76-
runtimePackages = [ /* List any formatters here */ ];
90+
pkgs.nixfmt-tree.override {
91+
settings = { /* additional treefmt config */ };
92+
runtimeInputs = [ /* additional formatter packages */ ];
93+
}
94+
```
95+
96+
You can achieve similar results by manually configuring `treefmt`:
97+
```nix
98+
pkgs.treefmt.withConfig {
99+
runtimeInputs = [ pkgs.nixfmt-rfc-style ];
100+
101+
settings = {
102+
# Log level for files treefmt won't format
103+
on-unmatched = "info";
104+
105+
# Assume the tree is a Git repository, will fail if it's not
106+
tree-root-file = ".git/index";
107+
108+
# Configure nixfmt for .nix files
109+
formatter.nixfmt = {
110+
command = "nixfmt";
111+
includes = [ "*.nix" ];
112+
};
113+
};
77114
}
78115
```
79116

0 commit comments

Comments
 (0)