cachix / git-hooks.nix

Seamless integration of https://pre-commit.com git hooks with Nix.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clippy hook fails when run using `nix flake check`

Scrumplex opened this issue · comments

This is the relevant flake.parts code:

# SPDX-FileCopyrightText: 2023 Sefa Eyeoglu <contact@scrumplex.net>
#
# SPDX-License-Identifier: GPL-3.0-or-later
{
  perSystem = {
    config,
    lib,
    pkgs,
    ...
  }: {
    pre-commit.settings = {
      excludes = [
        "vendor/"
      ];
      hooks = {
        alejandra.enable = true;
        rustfmt.enable = true;
        clippy.enable = true;
        prettier = {
          enable = true;
          excludes = ["flake.lock"];
        };
      };
    };
    devShells.default = pkgs.mkShell {
      shellHook = ''
        ${config.pre-commit.installationScript}
      '';

      inputsFrom = [config.packages.default];
      buildInputs = config.pre-commit.settings.enabledPackages;
      packages = with pkgs; [reuse];
    };
    formatter = pkgs.alejandra;
  };
}

The hook works fine when run in a devShell, but it fails when run as a flake check, as there is no internet access, no access to the cargo cache. Error:

       > - hook id: clippy
       > - exit code: 101
       >
       > error: no matching package named `tokio` found
       > location searched: registry `crates-io`
       > required by package `inhibridge v0.3.0 (/build/src)`
       > As a reminder, you're using offline mode (--offline) which can sometimes cause surprising resolution failures, if this error is too confusing you may wish to retry without the offline flag.
       >

Maybe we should have a way to pass a cargoDeps derivation to allow it to run in a flake check.

See #396

Maybe a stupid question, but I'm not gaining any insight from that PR. Is there any workaround to make these checks work in nix flake check currently? Or do we have to wait til that PR lands?

Is there any workaround to make these checks work in nix flake check currently?

nix flake check runs in a pure environment, soclippy and cargo don't have access to anything that's not tracked by git and can't fetch dependencies from the internet.

Workarounds:

Use nix develop:

nix develop -c pre-commit run --all-files

Override the run derivation (example for flake-parts):

          checks.pre-commit = pkgs.lib.mkForce (let
             drv = config.pre-commit.settings.run;
          in
            pkgs.stdenv.mkDerivation {
              name = "pre-commit-run";
              src = config.pre-commit.settings.rootSrc;
              buildInputs = [ pkgs.git pkgs.openssl pkgs.pkg-config ];
              nativeBuildInputs = [pkgs.rustPlatform.cargoSetupHook];
              cargoDeps = pkgs.rustPlatform.importCargoLock {
                lockFile = ./Cargo.lock;
              };
              buildPhase = drv.buildCommand;
            });

@sandydoo Thank you for that!