Skip to content

Commit bfc9966

Browse files
committed
feat: support cargo/runtime dependencies
1 parent 0db2e67 commit bfc9966

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

modules/hooks.nix

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ in
2222
# PLEASE keep this sorted alphabetically.
2323
options.settings =
2424
{
25+
runtimeDeps = mkOption {
26+
type = types.listOf types.package;
27+
description = lib.mdDoc "Runtime dependencies needed to run the checks.";
28+
default = [ ];
29+
example = "[ pkgs.openssl ]";
30+
};
2531
alejandra =
2632
{
2733
check =
@@ -1039,6 +1045,12 @@ in
10391045
description = lib.mdDoc "Path to Cargo.toml";
10401046
default = null;
10411047
};
1048+
cargoDeps = mkOption {
1049+
type = types.nullOr types.attrs;
1050+
description = lib.mdDoc "Cargo dependencies";
1051+
example = "pkgs.rustPlatform.importCargoLock { lockFile = ./Cargo.lock; }";
1052+
default = null;
1053+
};
10421054
};
10431055
statix =
10441056
{

modules/pre-commit.nix

+17-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ let
1313
;
1414

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

1719
cfg = config;
1820
install_stages = lib.unique (cfg.default_stages ++ (builtins.concatLists (lib.mapAttrsToList (_: h: h.stages) enabledHooks)));
@@ -197,17 +199,24 @@ let
197199
} >$out
198200
'';
199201

200-
run =
201-
runCommand "pre-commit-run" { buildInputs = [ git ]; } ''
202+
run = mkDerivation {
203+
name = "pre-commit-run";
204+
205+
src = cfg.rootSrc;
206+
buildInputs = [ git ];
207+
nativeBuildInputs = config.settings.runtimeDeps
208+
++ (if config.settings.rust.cargoDeps != null
209+
then [
210+
cargoSetupHook
211+
]
212+
else [ ]);
213+
cargoDeps = config.settings.rust.cargoDeps;
214+
buildPhase = ''
202215
set +e
203216
HOME=$PWD
204217
# Use `chmod +w` instead of `cp --no-preserve=mode` to be able to write and to
205218
# preserve the executable bit at the same time
206-
cp -R ${cfg.rootSrc} src
207-
chmod -R +w src
208-
ln -fs ${configFile} src/.pre-commit-config.yaml
209-
cd src
210-
rm -rf .git
219+
ln -fs ${configFile} .pre-commit-config.yaml
211220
git init
212221
git add .
213222
git config --global user.email "[email protected]"
@@ -226,6 +235,7 @@ let
226235
touch $out
227236
[ $? -eq 0 ] && exit $exitcode
228237
'';
238+
};
229239
in
230240
{
231241
options =

0 commit comments

Comments
 (0)