-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
treefmt.nix
120 lines (102 loc) · 2.71 KB
/
treefmt.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{ pkgs, lib }:
let
inherit (lib) getExe;
projectRootFile = "flake.lock";
toml = pkgs.formats.toml { };
treefmt = {
formatter =
lib.mapAttrs
(_name: fmt: {
inherit (fmt) command;
excludes = fmt.excludes or [ ];
includes = fmt.includes or [ ];
options = fmt.options or [ ];
})
{
deadnix = {
command = getExe pkgs.deadnix;
includes = [ "*.nix" ];
options = [ "--edit" ];
};
nixfmt = {
command = getExe pkgs.nixfmt-rfc-style;
includes = [ "*.nix" ];
};
ruff-check = {
command = getExe pkgs.ruff;
includes = [
"*.py"
"*.pyi"
];
options = [
"check"
"--fix"
];
};
ruff-format = {
command = getExe pkgs.ruff;
includes = [
"*.py"
"*.pyi"
];
options = [ "format" ];
};
shfmt = {
command = getExe pkgs.shfmt;
includes = [
"*.sh"
"*.bash"
"*.envrc"
"*.envrc.*"
];
options = [
"-i"
"2"
"-s"
"-w"
];
};
statix = {
command =
let
configFile = (pkgs.formats.toml { }).generate "statix.toml" { disabled = [ ]; };
# statix requires its configuration file to be named statix.toml exactly
# See: https://github.com/nerdypepper/statix/pull/54
configDir = pkgs.runCommandLocal "statix-config" { } ''
mkdir "$out"
cp ${configFile} "''${out}/statix.toml"
'';
in
pkgs.writeShellScript "statix-fix" ''
for file in "''$@"; do
${lib.getExe pkgs.statix} fix --config '${toString configDir}/statix.toml' "$file"
done
'';
includes = [ "*.nix" ];
};
};
global.excludes = [
"*.lock"
".gitignore"
];
};
configFile = toml.generate "treefmt.toml" treefmt;
in
pkgs.runCommand "treefmt-pyproject"
{
meta.mainProgram = "treefmt-pyproject";
}
''
mkdir -p $out/bin
cat > $out/bin/$name << EOF
#!${pkgs.runtimeShell}
set -euo pipefail
unset PRJ_ROOT
exec ${lib.getExe pkgs.treefmt} \
--config-file=${configFile} \
--tree-root-file=${projectRootFile} \
"$@"
EOF
chmod +x $out/bin/$name
ln -s $out/bin/$name $out/bin/treefmt
''