Skip to content

Commit

Permalink
compress
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zimbatm committed Aug 23, 2022
1 parent 9b0f259 commit b8513c8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 32 deletions.
90 changes: 59 additions & 31 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,65 @@ let

configFormat = nixpkgs.formats.toml { };

module = { config, ... }: {
options = {
settings = configSchema;
wrapper = {
package = lib.mkOption {
description = "Package to wrap";
type = lib.types.package;
default = treefmt;
};
projectRootFile = lib.mkOption {
description = ''
File to look for to determine the root of the project.
'';
example = "flake.nix";
};
};
# Outputs
build = {
configFile = lib.mkOption {
description = ''
Contains the generated config file derived from the settings.
'';
type = lib.types.path;
};
wrapper = lib.mkOption {
description = ''
The treefmt package, wrapped with the config file.
'';
type = lib.types.package;
};
};
};
config.build = {
configFile = configFormat.generate "treefmt.toml" config.settings;

wrapper = nixpkgs.writeShellScriptBin "treefmt" ''
find_up() (
ancestors=()
while [[ ! -f ${config.wrapper.projectRootFile} ]]; do
ancestors+=("$PWD")
if [[ $PWD == / ]]; then
echo "ERROR: Unable to locate the projectRootFile (${config.wrapper.projectRootFile}) in any of: ''${ancestors[*]@Q}" >&2
exit 1
fi
cd ..
done
)
tree_root=$(find_up)
exec ${config.wrapper.package}/bin/treefmt --config-file ${config.build.configFile} "$@" --tree-root "$tree_root"
'';
};
};

# Use the Nix module system to validate the treefmt config file format.
evalConfig = settings:
evalModule = config:
lib.evalModules {
modules = [
({ config, ... }: {
options.settings = configSchema;
options.configFile = lib.mkOption { type = lib.types.path; };
config.settings = settings;
config.configFile = configFormat.generate "treefmt.toml" config.settings;
})
];
modules = [ module config ];
};

# Pass treefmt setting options as Nix data, and get back a treefmt.toml file.
mkConfig = settings:
let
mod = evalConfig settings;
in
mod.config.configFile;

# What is used when invoking `nix run github:numtide/treefmt`
treefmt = rustPackages.rustPlatform.buildRustPackage {
inherit (cargoToml.package) name version;
Expand All @@ -110,22 +149,11 @@ let

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

passthru.withConfig = { config, projectRootFile ? "flake.nix" }:
passthru.withConfig = config:
let
configFile = mkConfig config;
mod = evalModule config;
in
nixpkgs.writeShellScriptBin "treefmt" ''
ANCESTORS=()
while [[ ! -f ${projectRootFile} ]]; do
ANCESTORS+=("$PWD")
if [[ $PWD == / ]]; then
echo "ERROR: Unable to locate the projectRootFile (${projectRootFile}) in any of: ''${ANCESTORS[*]}" >&2
exit 1
fi
cd ..
done
exec ${treefmt}/bin/treefmt --config-file ${configFile} "$@" --tree-root "$PWD"
'';
mod.build.wrapper;
};

# Add all the dependencies of treefmt, plus more build tools
Expand Down Expand Up @@ -160,7 +188,7 @@ let
});
in
{
inherit treefmt devShell evalConfig mkConfig;
inherit treefmt devShell evalModule module;

# A collection of packages for the project
docs = nixpkgs.callPackage ./docs { };
Expand Down
4 changes: 3 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
# In Nix 2.8 you can run `nix fmt` to format this whole repo. Note that you need to have loaded the
# `nix develop` shell before so the various formatters are available in the PATH.
# It also assumes that the project root has a flake.nix (override this by setting `projectRootFile`).
formatter = pkgs.treefmt.withConfig { config = nixpkgs.lib.importTOML ./treefmt.toml; };
formatter = pkgs.treefmt.withConfig {
settings = nixpkgs.lib.importTOML ./treefmt.toml;
};

devShell = pkgs.devShell;
}
Expand Down

0 comments on commit b8513c8

Please sign in to comment.