Skip to content

Commit 0b8a19b

Browse files
committed
feat: support cargo/runtime dependencies
1 parent e611897 commit 0b8a19b

File tree

2 files changed

+55
-32
lines changed

2 files changed

+55
-32
lines changed

modules/hooks.nix

+18-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,24 @@ in
4646

4747
# PLEASE keep this sorted alphabetically.
4848
options.settings = {
49-
rust.cargoManifestPath = mkOption {
50-
type = types.nullOr types.str;
51-
description = lib.mdDoc "Path to Cargo.toml";
52-
default = null;
49+
runtimeDeps = mkOption {
50+
type = types.listOf types.package;
51+
description = lib.mdDoc "Runtime dependencies needed to run the checks.";
52+
default = [ ];
53+
example = "[ pkgs.openssl ]";
54+
};
55+
rust = {
56+
cargoDeps = mkOption {
57+
type = types.nullOr types.attrs;
58+
description = lib.mdDoc "Cargo dependencies";
59+
example = "pkgs.rustPlatform.importCargoLock { lockFile = ./Cargo.lock; }";
60+
default = null;
61+
};
62+
cargoManifestPath = mkOption {
63+
type = types.nullOr types.str;
64+
description = lib.mdDoc "Path to Cargo.toml";
65+
default = null;
66+
};
5367
};
5468
};
5569

modules/pre-commit.nix

+37-28
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)));
@@ -54,34 +56,41 @@ let
5456
);
5557

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

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

0 commit comments

Comments
 (0)