numtide / treefmt-nix

treefmt nix configuration
https://numtide.github.io/treefmt/
MIT License
270 stars 83 forks source link

Flake check not failing when there is unformatted code #81

Closed jgero closed 1 year ago

jgero commented 1 year ago

Describe the bug

I have unformatted code in the sources, which gets formatted correctly with nix fmt, but nix flake check does not fail.

To Reproduce

Steps to reproduce the behavior:

  1. have unformatted code
  2. run nix flake check

Expected behavior

nix flake check should fail

System 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:

      formatter.x86_64-linux = treefmt-nix.lib.mkWrapper
        nixpkgs.legacyPackages.x86_64-linux
        {
          projectRootFile = "flake.nix";
          programs.nixpkgs-fmt.enable = true;
          programs.stylua.enable = true;
        };

It works for nix fmt, but not for nix flake check.

sg-qwt commented 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
'';
}
sg-qwt commented 1 year ago

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.

jgero commented 1 year ago

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 :(

srid commented 1 year ago

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.

jgero commented 1 year ago

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?

zimbatm commented 1 year ago

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;
      });
    };
}
zimbatm commented 1 year ago

^ update the docs, let me know if it works for you!

jgero commented 1 year ago

Works for me, thank you very much! I guess this issue can be closed once the docs are updated