From b6d9d72d3dcedbe564a0c5ad47d311a0bc6a6563 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Wed, 20 Mar 2024 21:23:38 +0100 Subject: [PATCH] feat: support cargo/runtime dependencies --- modules/hooks.nix | 16 ++++++++--- modules/pre-commit.nix | 62 +++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 29d36495..85753fd1 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -67,10 +67,18 @@ in # PLEASE keep this sorted alphabetically. options.settings = { - rust.cargoManifestPath = mkOption { - type = types.nullOr types.str; - description = "Path to Cargo.toml"; - default = null; + rust = { + check.cargoDeps = mkOption { + type = types.nullOr types.attrs; + description = "Cargo dependencies needed to run the checks."; + example = "pkgs.rustPlatform.importCargoLock { lockFile = ./Cargo.lock; }"; + default = null; + }; + cargoManifestPath = mkOption { + type = types.nullOr types.str; + description = "Path to Cargo.toml"; + default = null; + }; }; }; diff --git a/modules/pre-commit.nix b/modules/pre-commit.nix index d9da6db9..d3578d1c 100644 --- a/modules/pre-commit.nix +++ b/modules/pre-commit.nix @@ -13,6 +13,8 @@ let ; inherit (pkgs) runCommand; + inherit (pkgs.rustPlatform) cargoSetupHook; + inherit (pkgs.stdenv) mkDerivation; cfg = config; install_stages = lib.unique (builtins.concatLists (lib.mapAttrsToList (_: h: h.stages) enabledHooks)); @@ -26,6 +28,7 @@ let if excludes == [ ] then "^$" else "(${concatStringsSep "|" excludes})"; enabledHooks = filterAttrs (id: value: value.enable) cfg.hooks; + enabledExtraPackages = builtins.concatLists (mapAttrsToList (_: value: value.extraPackages) enabledHooks); processedHooks = mapAttrsToList (id: value: value.raw // { inherit id; }) enabledHooks; @@ -51,34 +54,37 @@ let ); run = - runCommand "pre-commit-run" { buildInputs = [ cfg.gitPackage ]; } '' - set +e - HOME=$PWD - # Use `chmod +w` instead of `cp --no-preserve=mode` to be able to write and to - # preserve the executable bit at the same time - cp -R ${cfg.rootSrc} src - chmod -R +w src - ln -fs ${configFile} src/.pre-commit-config.yaml - cd src - rm -rf .git - git init -q - git add . - git config --global user.email "you@example.com" - git config --global user.name "Your Name" - git commit -m "init" -q - if [[ ${toString (compare install_stages [ "manual" ])} -eq 0 ]] - then - echo "Running: $ pre-commit run --hook-stage manual --all-files" - ${cfg.package}/bin/pre-commit run --hook-stage manual --all-files - else - echo "Running: $ pre-commit run --all-files" - ${cfg.package}/bin/pre-commit run --all-files - fi - exitcode=$? - git --no-pager diff --color - mkdir $out - [ $? -eq 0 ] && exit $exitcode - ''; + mkDerivation { + name = "pre-commit-run"; + + src = cfg.rootSrc; + buildInputs = [ cfg.gitPackage ]; + nativeBuildInputs = enabledExtraPackages + ++ lib.optional (config.settings.rust.check.cargoDeps != null) cargoSetupHook; + cargoDeps = config.settings.rust.check.cargoDeps; + buildPhase = '' + set +e + HOME=$PWD + ln -fs ${configFile} .pre-commit-config.yaml + git init -q + git add . + git config --global user.email "you@example.com" + git config --global user.name "Your Name" + git commit -m "init" -q + if [[ ${toString (compare install_stages [ "manual" ])} -eq 0 ]] + then + echo "Running: $ pre-commit run --hook-stage manual --all-files" + ${cfg.package}/bin/pre-commit run --hook-stage manual --all-files + else + echo "Running: $ pre-commit run --all-files" + ${cfg.package}/bin/pre-commit run --all-files + fi + exitcode=$? + git --no-pager diff --color + mkdir $out + [ $? -eq 0 ] && exit $exitcode + ''; + }; failedAssertions = builtins.map (x: x.message) (builtins.filter (x: !x.assertion) config.assertions);