Skip to content

Commit 499c28e

Browse files
committed
feat: support cargo/runtime dependencies
1 parent 4509ca6 commit 499c28e

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-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

+37-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));
@@ -53,34 +55,41 @@ let
5355
);
5456

5557
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-
'';
58+
mkDerivation {
59+
name = "pre-commit-run";
60+
61+
src = cfg.rootSrc;
62+
buildInputs = [ git ];
63+
nativeBuildInputs = config.extraPackages
64+
++ (if config.settings.rust.check.cargoDeps != null
65+
then [
66+
cargoSetupHook
67+
]
68+
else [ ]);
69+
cargoDeps = config.settings.rust.cargoDeps;
70+
buildPhase = ''
71+
set +e
72+
HOME=$PWD
73+
ln -fs ${configFile} .pre-commit-config.yaml
74+
git init -q
75+
git add .
76+
git config --global user.email "[email protected]"
77+
git config --global user.name "Your Name"
78+
git commit -m "init" -q
79+
if [[ ${toString (compare install_stages [ "manual" ])} -eq 0 ]]
80+
then
81+
echo "Running: $ pre-commit run --hook-stage manual --all-files"
82+
${cfg.package}/bin/pre-commit run --hook-stage manual --all-files
83+
else
84+
echo "Running: $ pre-commit run --all-files"
85+
${cfg.package}/bin/pre-commit run --all-files
86+
fi
87+
exitcode=$?
88+
git --no-pager diff --color
89+
touch $out
90+
[ $? -eq 0 ] && exit $exitcode
91+
'';
92+
};
8493

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

0 commit comments

Comments
 (0)