Skip to content

Commit 5070023

Browse files
committed
feat: support cargo/runtime dependencies
1 parent 4e743a6 commit 5070023

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed

modules/hooks.nix

+12-4
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

+34-28
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ let
1515
;
1616

1717
inherit (pkgs) runCommand writeText git;
18+
inherit (pkgs.rustPlatform) cargoSetupHook;
19+
inherit (pkgs.stdenv) mkDerivation;
1820

1921
cfg = config;
2022
install_stages = lib.unique (builtins.concatLists (lib.mapAttrsToList (_: h: h.stages) enabledHooks));
@@ -28,6 +30,7 @@ let
2830
if excludes == [ ] then "^$" else "(${concatStringsSep "|" excludes})";
2931

3032
enabledHooks = filterAttrs (id: value: value.enable) cfg.hooks;
33+
enabledExtraPackages = builtins.concatLists (mapAttrsToList (_: value: value.extraPackages) enabledHooks);
3134
processedHooks =
3235
mapAttrsToList (id: value: value.raw // { inherit id; }) enabledHooks;
3336

@@ -53,34 +56,37 @@ let
5356
);
5457

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

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

0 commit comments

Comments
 (0)