68
68
69
69
configFormat = nixpkgs . formats . toml { } ;
70
70
71
+ module = { config , ... } : {
72
+ options = {
73
+ settings = configSchema ;
74
+ wrapper = {
75
+ package = lib . mkOption {
76
+ description = "Package to wrap" ;
77
+ type = lib . types . package ;
78
+ default = treefmt ;
79
+ } ;
80
+ projectRootFile = lib . mkOption {
81
+ description = ''
82
+ File to look for to determine the root of the project.
83
+ '' ;
84
+ example = "flake.nix" ;
85
+ } ;
86
+ } ;
87
+ # Outputs
88
+ build = {
89
+ configFile = lib . mkOption {
90
+ description = ''
91
+ Contains the generated config file derived from the settings.
92
+ '' ;
93
+ type = lib . types . path ;
94
+ } ;
95
+ wrapper = lib . mkOption {
96
+ description = ''
97
+ The treefmt package, wrapped with the config file.
98
+ '' ;
99
+ type = lib . types . package ;
100
+ } ;
101
+ } ;
102
+ } ;
103
+ config . build = {
104
+ configFile = configFormat . generate "treefmt.toml" config . settings ;
105
+
106
+ wrapper = nixpkgs . writeShellScriptBin "treefmt" ''
107
+ find_up() (
108
+ ancestors=()
109
+ while [[ ! -f ${ config . wrapper . projectRootFile } ]]; do
110
+ ancestors+=("$PWD")
111
+ if [[ $PWD == / ]]; then
112
+ echo "ERROR: Unable to locate the projectRootFile (${ config . wrapper . projectRootFile } ) in any of: '' ${ancestors[*]@Q}" >&2
113
+ exit 1
114
+ fi
115
+ cd ..
116
+ done
117
+ )
118
+ tree_root=$(find_up)
119
+ exec ${ config . wrapper . package } /bin/treefmt --config-file ${ config . build . configFile } "$@" --tree-root "$tree_root"
120
+ '' ;
121
+ } ;
122
+ } ;
123
+
71
124
# Use the Nix module system to validate the treefmt config file format.
72
- evalConfig = settings :
125
+ evalModule = config :
73
126
lib . evalModules {
74
- modules = [
75
- ( { config , ... } : {
76
- options . settings = configSchema ;
77
- options . configFile = lib . mkOption { type = lib . types . path ; } ;
78
- config . settings = settings ;
79
- config . configFile = configFormat . generate "treefmt.toml" config . settings ;
80
- } )
81
- ] ;
127
+ modules = [ module config ] ;
82
128
} ;
83
129
84
- # Pass treefmt setting options as Nix data, and get back a treefmt.toml file.
85
- mkConfig = settings :
86
- let
87
- mod = evalConfig settings ;
88
- in
89
- mod . config . configFile ;
90
-
91
130
# What is used when invoking `nix run github:numtide/treefmt`
92
131
treefmt = rustPackages . rustPlatform . buildRustPackage {
93
132
inherit ( cargoToml . package ) name version ;
@@ -110,22 +149,11 @@ let
110
149
111
150
meta . description = "one CLI to format the code tree" ;
112
151
113
- passthru . withConfig = { config , projectRootFile ? "flake.nix" } :
152
+ passthru . withConfig = config :
114
153
let
115
- configFile = mkConfig config ;
154
+ mod = evalModule config ;
116
155
in
117
- nixpkgs . writeShellScriptBin "treefmt" ''
118
- ANCESTORS=()
119
- while [[ ! -f ${ projectRootFile } ]]; do
120
- ANCESTORS+=("$PWD")
121
- if [[ $PWD == / ]]; then
122
- echo "ERROR: Unable to locate the projectRootFile (${ projectRootFile } ) in any of: '' ${ANCESTORS[*]}" >&2
123
- exit 1
124
- fi
125
- cd ..
126
- done
127
- exec ${ treefmt } /bin/treefmt --config-file ${ configFile } "$@" --tree-root "$PWD"
128
- '' ;
156
+ mod . build . wrapper ;
129
157
} ;
130
158
131
159
# Add all the dependencies of treefmt, plus more build tools
160
188
} ) ;
161
189
in
162
190
{
163
- inherit treefmt devShell evalConfig mkConfig ;
191
+ inherit treefmt devShell evalModule module ;
164
192
165
193
# A collection of packages for the project
166
194
docs = nixpkgs . callPackage ./docs { } ;
0 commit comments