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

Clippy hook fails when run using `nix flake check`

Open Scrumplex opened this issue 1 year ago • 5 comments

This is the relevant flake.parts code:

# SPDX-FileCopyrightText: 2023 Sefa Eyeoglu <[email protected]>
#
# 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.

Scrumplex avatar Jun 02 '24 14:06 Scrumplex

See https://github.com/cachix/git-hooks.nix/pull/396

sandydoo avatar Jun 02 '24 15:06 sandydoo

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?

Mastermindaxe avatar Jun 11 '24 12:06 Mastermindaxe

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 avatar Jun 12 '24 13:06 sandydoo

@sandydoo Thank you for that!

Mastermindaxe avatar Jun 12 '24 13:06 Mastermindaxe

Would still be nice to get this resolved in that PR!

greg-hellings avatar Oct 21 '24 02:10 greg-hellings