Closed jgero closed 1 year ago
I actually stumbled on this as well, turn out fmt and check are non related, to re-use treefmt for plain flake check, you can do something like this:
let mytreefmt = treefmt-nix.lib.mkWrapper
nixpkgs.legacyPackages.x86_64-linux {
projectRootFile = "flake.nix";
programs.nixpkgs-fmt.enable = true;
programs.stylua.enable = true;
}; in
{
formatter.x86_64-linux = mytreefmt;
checks.x86_64-linux.formatter =
pkgs.runCommandNoCC "treefmt" {
nativeBuildInputs = [
mytreefmt
];
} ''
# keep timestamps so that treefmt is able to detect mtime changes
cp --no-preserve=mode --preserve=timestamps -r ${self} source
cd source
HOME=$TMPDIR treefmt --no-cache --fail-on-change
touch $out
'';
}
However I do notice there's a check option in module-options.nix, but I'm not sure what's the use case and how to consume that in vanilla flake style.
We are in the same situation, I am also using something I assume is a workaround, since the code of this project looks like the check should work. Unfortunately I don't understand it enough to create a PR :(
However I do notice there's a check option in module-options.nix, but I'm not sure what's the use case and how to consume that in vanilla flake style.
That's used when using flake-parts.
That's used when using flake-parts.
Do I understand correctly that it is expected that the flake check command does not work when using vanilla flakes?
Yes, flake-parts is used to wire things together. Flake itself doesn't provide that kind of mechanism, so you would have to manually attach the module check to the flake check.
Eg:
{
treefmt-nix.url = "github:numtide/treefmt-nix";
outputs = { self, nixpkgs, systems, treefmt-nix }:
let
eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system});
treefmtEval = eachSystem (pkgs: treefmt-nix.evalModule pkgs ./treefmt.nix); # put the treefmt config in treefmt.nix
in
{
# for `nix fmt`
formatter = eachSystem (pkgs: treefmtEval.${pkgs.sytem}.config.build.wrapper);
# for `nix flake check`
checks = eachSystem (pkgs: {
formatting = treefmtEval.${pkgs.sytem}.config.build.check self;
});
};
}
^ update the docs, let me know if it works for you!
Works for me, thank you very much! I guess this issue can be closed once the docs are updated
Describe the bug
I have unformatted code in the sources, which gets formatted correctly with
nix fmt
, butnix flake check
does not fail.To Reproduce
Steps to reproduce the behavior:
nix flake check
Expected behavior
nix flake check
should failSystem information
I'm using the (currently) most recent version of treefmt-nix (rev 4e92552731aca44ece86731ec4c7848a2c46aa67) on x86_64-linux
Additional context
This is my first time using treefmt-nix, so it may very well be the case that I am missing something or my configuration is wrong, I'm not very experienced yet in using nix. But as far as I understood the flake example in the README should also automatically configure the flake check, correct? This is my current minimal configuration:
It works for
nix fmt
, but not fornix flake check
.