git-hooks.nix icon indicating copy to clipboard operation
git-hooks.nix copied to clipboard

Add Nix Flake Check and Nix Fmt to Devenv Hooks

Open shikanime opened this issue 1 month ago • 5 comments

Automatically integrate standard nix flake check and nix fmt. Specifically, when treefmt or git-hooks are configured in a project.

shikanime avatar Nov 11 '25 19:11 shikanime

We won't add nix flake check as it takes too long, commit hooks should be fast!

We should be able to add nix fmt.

domenkozar avatar Nov 11 '25 19:11 domenkozar

Sorry, yes it was meant to devenv.

shikanime avatar Nov 11 '25 19:11 shikanime

I don't understand.

domenkozar avatar Nov 11 '25 19:11 domenkozar

This issue was intended to be opened in cachix/devenv, since the git-hooks.nix and treefmt-nix Flake modules already add the formatters and checks individually. However, problems arise when they are defined at the devenv level such as:

{
  inputs = {
    devenv.url = "github:cachix/devenv";
    flake-parts.url = "github:hercules-ci/flake-parts";
    git-hooks.url = "github:cachix/git-hooks.nix";
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    treefmt-nix.url = "github:numtide/treefmt-nix";
  };

  outputs =
    inputs@{
      self,
      devenv,
      git-hooks,
      flake-parts,
      treefmt-nix,
      ...
    }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      imports = [
        devenv.flakeModule
        treefmt-nix.flakeModule
        git-hooks.flakeModule
      ];
      perSystem =
        {
          config,
          pkgs,
          system,
          ...
        }:
          {
            # We need to set the treefmt wrapper to the one from the devenv shell
            treefmt.build.wrapper = config.devenv.shells.default.treefmt.config.build.wrapper;
            devenv.shells.default.treefmt.enable = true;
          };
    };
}

shikanime avatar Nov 11 '25 19:11 shikanime

Here's an example of how I fixed it on my side: https://github.com/shikanime-studio/devlib/blob/cc2d111e461b4fa4b413f3d14ddffa50de65e007/modules/flake/default.nix

shikanime avatar Nov 11 '25 20:11 shikanime