git-hooks.nix
                                
                                 git-hooks.nix copied to clipboard
                                
                                    git-hooks.nix copied to clipboard
                            
                            
                            
                        Cargo check fails to load crates.io-index correctly
When enabling cargo-check.enable = true I get this error:
cargo-check..............................................................Failed
- hook id: cargo-check
- exit code: 101
    Updating crates.io index
warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name; class=Net (12)
warning: spurious network error (1 tries remaining): [6] Couldn't resolve host name; class=Net (12)
error: failed to get `base-x` as a dependency of package `hashexpr v0.1.0 (/build/src/hashexpr)`
Caused by:
  failed to fetch `https://github.com/rust-lang/crates.io-index`
Caused by:
  [6] Couldn't resolve host name; class=Net (12)
building '/nix/store/6lsmz02z9679x4wb1qiy0733689h2fk3-git-deps.drv'...
nix-linter...............................................................Passed
nixpkgs-fmt..............................................................Passed
shellcheck...........................................(no files to check)Skipped
builder for '/nix/store/wi0lbb6j045ma537m6xdvyrcya8g56rg-pre-commit-run.drv' failed with exit code 1
I guess the class=Net has gotten there by mistake?
Here's the command it runs: https://github.com/cachix/pre-commit-hooks.nix/blob/7a2bec0576d2b7a91e2bbf0ad31e80f08a0b7af3/modules/hooks.nix#L203
cargo check runs fine. I think the problem is that it expects to have internet access which is not allowed in nix-build.
In the meantime, I have a very hacky workaround (for cargo clippy), by saving $CARGO_HOME from the dependencies build (there should be something better using builtDependencies directly but I'm getting empty output right now).
Write your Naersk package:
my-crate = naersk.buildPackage {
  # standard!
  inherit src;
  pname = "my-crate";
  version = "0.1";
  # hacks for making clippy work in CI
  postInstall = ''
    cp -r $CARGO_HOME $out/.cargo
  '';
};
And then your pre-commit hooks:
pre-commit-hooks.run {
  inherit src;
  hooks = {
    my-clippy = {
      enable = true;
      entry = ''
        bash -c ' \
           cp -r ${builtins.head my-crate.builtDependencies}/.cargo/ .cargo
           CARGO_HOME=.cargo \
           PATH=${rust}/bin:${pkgs.gcc}/bin:$PATH \
           cargo clippy --release --features strict --offline -- --no-deps
        '
      '';
      pass_filenames = false;
      types = [ "file" "rust" ];
    };
  };
};
This is not fixed, I am still getting issues with clippy not being able to download dependencies
Ya, I am getting bit by this today also. I am looking into solutions.
My error for reference.
pre-commit-run> Running: $ pre-commit run --all-files
pre-commit-run> alejandra................................................................Passed
pre-commit-run> cargo-check..............................................................Failed
pre-commit-run> - hook id: cargo-check
pre-commit-run> - exit code: 101
pre-commit-run>     Updating crates.io index
pre-commit-run> warning: spurious network error (3 tries remaining): [6] Couldn't resolve host name (Could not resolve host: index.crates.io)
pre-commit-run> warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: index.crates.io)
pre-commit-run> warning: spurious network error (1 tries remaining): [6] Couldn't resolve host name (Could not resolve host: index.crates.io)
pre-commit-run> error: failed to get `anyhow` as a dependency of package `idempotent_set v0.1.0 (/build/src)`
pre-commit-run> Caused by:
pre-commit-run>   failed to query replaced source registry `crates-io`
pre-commit-run> Caused by:
pre-commit-run>   download of config.json failed
pre-commit-run> Caused by:
pre-commit-run>   failed to download from `https://index.crates.io/config.json`
pre-commit-run> Caused by:
pre-commit-run>   [6] Couldn't resolve host name (Could not resolve host: index.crates.io)
pre-commit-run> clippy...................................................................Failed
pre-commit-run> - hook id: clippy
pre-commit-run> - exit code: 101
pre-commit-run> error: no matching package named `clap` found
pre-commit-run> location searched: registry `crates-io`
pre-commit-run> required by package `idempotent_set v0.1.0 (/build/src)`
pre-commit-run> 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.
pre-commit-run> deadnix..................................................................Passed
pre-commit-run> flake-checker............................................................Passed
pre-commit-run> rustfmt..................................................................Passed
pre-commit-run> statix...................................................................Passed
https://github.com/cachix/git-hooks.nix/pull/396 tries to address this.