Skip to content

Commit 1ba78e7

Browse files
committed
feat: support cargo/runtime dependencies
1 parent 3c3e88f commit 1ba78e7

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed

modules/hooks.nix

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,18 @@ in
6767

6868
# PLEASE keep this sorted alphabetically.
6969
options.settings = {
70-
rust.cargoManifestPath = mkOption {
71-
type = types.nullOr types.str;
72-
description = "Path to Cargo.toml";
73-
default = null;
70+
rust = {
71+
check.cargoDeps = mkOption {
72+
type = types.nullOr types.attrs;
73+
description = "Cargo dependencies needed to run the checks.";
74+
example = "pkgs.rustPlatform.importCargoLock { lockFile = ./Cargo.lock; }";
75+
default = null;
76+
};
77+
cargoManifestPath = mkOption {
78+
type = types.nullOr types.str;
79+
description = "Path to Cargo.toml";
80+
default = null;
81+
};
7482
};
7583
};
7684

modules/pre-commit.nix

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ let
1313
;
1414

1515
inherit (pkgs) runCommand git;
16+
inherit (pkgs.rustPlatform) cargoSetupHook;
17+
inherit (pkgs.stdenv) mkDerivation;
1618

1719
cfg = config;
1820
install_stages = lib.unique (builtins.concatLists (lib.mapAttrsToList (_: h: h.stages) enabledHooks));
@@ -26,6 +28,7 @@ let
2628
if excludes == [ ] then "^$" else "(${concatStringsSep "|" excludes})";
2729

2830
enabledHooks = filterAttrs (id: value: value.enable) cfg.hooks;
31+
enabledExtraPackages = builtins.concatLists (mapAttrsToList (_: value: value.extraPackages) enabledHooks);
2932
processedHooks =
3033
mapAttrsToList (id: value: value.raw // { inherit id; }) enabledHooks;
3134

@@ -51,34 +54,37 @@ let
5154
);
5255

5356
run =
54-
runCommand "pre-commit-run" { buildInputs = [ git ]; } ''
55-
set +e
56-
HOME=$PWD
57-
# Use `chmod +w` instead of `cp --no-preserve=mode` to be able to write and to
58-
# preserve the executable bit at the same time
59-
cp -R ${cfg.rootSrc} src
60-
chmod -R +w src
61-
ln -fs ${configFile} src/.pre-commit-config.yaml
62-
cd src
63-
rm -rf .git
64-
git init -q
65-
git add .
66-
git config --global user.email "[email protected]"
67-
git config --global user.name "Your Name"
68-
git commit -m "init" -q
69-
if [[ ${toString (compare install_stages [ "manual" ])} -eq 0 ]]
70-
then
71-
echo "Running: $ pre-commit run --hook-stage manual --all-files"
72-
${cfg.package}/bin/pre-commit run --hook-stage manual --all-files
73-
else
74-
echo "Running: $ pre-commit run --all-files"
75-
${cfg.package}/bin/pre-commit run --all-files
76-
fi
77-
exitcode=$?
78-
git --no-pager diff --color
79-
mkdir $out
80-
[ $? -eq 0 ] && exit $exitcode
81-
'';
57+
mkDerivation {
58+
name = "pre-commit-run";
59+
60+
src = cfg.rootSrc;
61+
buildInputs = [ git ];
62+
nativeBuildInputs = enabledExtraPackages
63+
++ lib.optional (config.settings.rust.check.cargoDeps != null) cargoSetupHook;
64+
cargoDeps = config.settings.rust.check.cargoDeps;
65+
buildPhase = ''
66+
set +e
67+
HOME=$PWD
68+
ln -fs ${configFile} .pre-commit-config.yaml
69+
git init -q
70+
git add .
71+
git config --global user.email "[email protected]"
72+
git config --global user.name "Your Name"
73+
git commit -m "init" -q
74+
if [[ ${toString (compare install_stages [ "manual" ])} -eq 0 ]]
75+
then
76+
echo "Running: $ pre-commit run --hook-stage manual --all-files"
77+
${cfg.package}/bin/pre-commit run --hook-stage manual --all-files
78+
else
79+
echo "Running: $ pre-commit run --all-files"
80+
${cfg.package}/bin/pre-commit run --all-files
81+
fi
82+
exitcode=$?
83+
git --no-pager diff --color
84+
mkdir $out
85+
[ $? -eq 0 ] && exit $exitcode
86+
'';
87+
};
8288

8389
failedAssertions = builtins.map (x: x.message) (builtins.filter (x: !x.assertion) config.assertions);
8490

0 commit comments

Comments
 (0)