Files
Matt Sturgeon 134bfa753c treefmt.withConfig: add check function
Inspired by treefmt-nix's `config.build.check` option, add a similar
check function to `treefmt.withConfig`'s wrapper.

`check` is a function of `Path → Derivation`, which returns a derivation
that builds ok when the path is already formatted, or fails to build if
the wrapped-treefmt would change the path's formatting.

Add the `check` function, tests for it, and initial docs.

See treefmt-nix: https://github.com/numtide/treefmt-nix/blob/db947814/module-options.nix#L301-L349
2026-06-09 07:37:38 +01:00

108 lines
2.1 KiB
Nix

{
lib,
pkgs,
treefmt,
}:
{
/**
Evaluate a treefmt configuration.
# Type
```
Module -> Configuration
```
# Inputs
`module`
: A treefmt module. See [options reference](#sec-treefmt-options-reference).
*/
evalConfig =
module:
lib.evalModules {
class = "treefmtConfig";
specialArgs.modulesPath = ./modules;
modules = [
{
_file = "treefmt.evalConfig";
_module.args.pkgs = lib.mkOptionDefault pkgs;
package = lib.mkOptionDefault treefmt;
}
{
_file = "<treefmt.evalConfig args>";
imports = lib.toList module;
}
./modules/default.nix
];
};
/**
Wrap treefmt, configured using structured settings.
# Check
The resulting package has a `check` attribute of type `Path -> Derivation`.
The derivation returned will only build if the path supplied is already formatted correctly.
```
{ pkgs }:
let
myTreefmt = pkgs.treefmt.withConfig ./treefmt-config.nix;
project = ../.;
in
myTreefmt.check project
```
# Type
```
Module -> Derivation
```
# Inputs
`module`
: A treefmt module. See [options reference](#sec-treefmt-options-reference).
*/
withConfig =
module:
let
configuration = treefmt.evalConfig {
_file = "<treefmt.withConfig args>";
imports = lib.toList module;
};
in
configuration.config.result;
/**
Build a treefmt config file from structured settings.
# Type
```
Module -> Derivation
```
# Inputs
`settings`
: A settings module, used to build a treefmt config file.
See [`settings` option reference](#opt-treefmt-settings).
*/
buildConfig =
module:
let
configuration = treefmt.evalConfig {
_file = "<treefmt.buildConfig args>";
settings.imports = lib.toList module;
};
in
configuration.config.configFile.overrideAttrs {
passthru = {
inherit (configuration.config) settings;
options = (opt: opt.type.getSubOptions opt.loc) configuration.options.settings;
};
};
}