Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git-hooks: prevent pre-commit from leaking build inputs into env #1689

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/modules/integrations/git-hooks.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
{ pkgs, self, lib, config, inputs, ... }:

let
cfg = config.git-hooks;

git-hooks-module =
inputs.git-hooks
or inputs.pre-commit-hooks
or (throw "git-hooks or pre-commit-hooks input required");

# `propagatedBuildInputs` in Python apps are leaked into the environment.
# This normally leaks the Python interpreter and its site-packages, causing collision errors.
# This affects all packages built with `buildPythonApplication` or `toPythonApplication`.
# pre-commit is particularly annoying as it is difficult for end-users to track down.
# Tracking: https://github.com/NixOS/nixpkgs/issues/302376
packageBin = pkgs.runCommandLocal "pre-commit-bin" { } ''
mkdir -p $out/bin
ln -s ${cfg.package}/bin/pre-commit $out/bin/pre-commit
'';
in
{
imports = [
Expand All @@ -28,14 +40,14 @@ in
description = "Integration with https://github.com/cachix/git-hooks.nix";
};

config = lib.mkIf ((lib.filterAttrs (id: value: value.enable) config.git-hooks.hooks) != { }) {
ci = [ config.git-hooks.run ];
config = lib.mkIf ((lib.filterAttrs (id: value: value.enable) cfg.hooks) != { }) {
ci = [ cfg.run ];
# Add the packages for any enabled hooks at the end to avoid overriding the language-defined packages.
packages = lib.mkAfter ([ config.git-hooks.package ] ++ (config.git-hooks.enabledPackages or [ ]));
packages = lib.mkAfter ([ packageBin ] ++ (cfg.enabledPackages or [ ]));
tasks = {
# TODO: split installation script into status + exec
"devenv:git-hooks:install" = {
exec = config.git-hooks.installationScript;
exec = cfg.installationScript;
before = [ "devenv:enterShell" ];
};
"devenv:git-hooks:run" = {
Expand Down
12 changes: 12 additions & 0 deletions tests/git-hooks-no-python-leak/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Assert that the pre-commit package does not leak its dependencies into the environment.
{
git-hooks.hooks.nixfmt-rfc-style.enable = true;

enterTest = ''
if [ -n "$PYTHONPATH" ]; then
echo "PYTHONPATH is non-empty: $PYTHONPATH" >&2
echo "The pre-commit package is leaking its dependencies into the environment." >&2
exit 1
fi
'';
}
Loading