Skip to content

Commit b8513c8

Browse files
committed
compress
This makes all the mechanisms available to mount in another module system. I also fixed a small design issue with the wrapper changing $PWD and making relative paths then be relative to the project root instead of the user's $PWD.
1 parent 9b0f259 commit b8513c8

File tree

2 files changed

+62
-32
lines changed

2 files changed

+62
-32
lines changed

default.nix

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,65 @@ let
6868

6969
configFormat = nixpkgs.formats.toml { };
7070

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+
71124
# Use the Nix module system to validate the treefmt config file format.
72-
evalConfig = settings:
125+
evalModule = config:
73126
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 ];
82128
};
83129

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-
91130
# What is used when invoking `nix run github:numtide/treefmt`
92131
treefmt = rustPackages.rustPlatform.buildRustPackage {
93132
inherit (cargoToml.package) name version;
@@ -110,22 +149,11 @@ let
110149

111150
meta.description = "one CLI to format the code tree";
112151

113-
passthru.withConfig = { config, projectRootFile ? "flake.nix" }:
152+
passthru.withConfig = config:
114153
let
115-
configFile = mkConfig config;
154+
mod = evalModule config;
116155
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;
129157
};
130158

131159
# Add all the dependencies of treefmt, plus more build tools
@@ -160,7 +188,7 @@ let
160188
});
161189
in
162190
{
163-
inherit treefmt devShell evalConfig mkConfig;
191+
inherit treefmt devShell evalModule module;
164192

165193
# A collection of packages for the project
166194
docs = nixpkgs.callPackage ./docs { };

flake.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
# In Nix 2.8 you can run `nix fmt` to format this whole repo. Note that you need to have loaded the
2424
# `nix develop` shell before so the various formatters are available in the PATH.
2525
# It also assumes that the project root has a flake.nix (override this by setting `projectRootFile`).
26-
formatter = pkgs.treefmt.withConfig { config = nixpkgs.lib.importTOML ./treefmt.toml; };
26+
formatter = pkgs.treefmt.withConfig {
27+
settings = nixpkgs.lib.importTOML ./treefmt.toml;
28+
};
2729

2830
devShell = pkgs.devShell;
2931
}

0 commit comments

Comments
 (0)