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

Support unlinking the config when using multiple flakes + misc improvements #573

Merged
merged 4 commits into from
Mar 7, 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: 13 additions & 7 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ in
default = ".pre-commit-config.yaml";
description =
''
The path to the generated pre-commit configuration file.
The path where to generate the pre-commit configuration file.

This path is relative to the root of the project. By default,
this is set to ".pre-commit-config.yaml", which is the standard
location expected by pre-commit.
'';
};

Expand Down Expand Up @@ -428,14 +432,16 @@ in
if ! readlink "''${GIT_WC}/${cfg.configPath}" >/dev/null \
|| [[ $(readlink "''${GIT_WC}/${cfg.configPath}") != ${cfg.configFile} ]]; then
echo 1>&2 "git-hooks.nix: updating $PWD repo"
[ -L ${cfg.configPath} ] && unlink ${cfg.configPath}
[ -L "''${GIT_WC}/${cfg.configPath}" ] && unlink "''${GIT_WC}/${cfg.configPath}"

if [ -e "''${GIT_WC}/${cfg.configPath}" ]; then
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of pre-existing ${cfg.configPath}"
echo 1>&2 " 1. Translate ${cfg.configPath} contents to the new syntax in your Nix file"
echo 1>&2 " see https://github.com/cachix/git-hooks.nix#getting-started"
echo 1>&2 " 2. remove ${cfg.configPath}"
echo 1>&2 " 3. add ${cfg.configPath} to .gitignore"
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of an existing config at ${cfg.configPath}"
echo 1>&2 ""
echo 1>&2 " To migrate the existing config to a Nix configuration:"
echo 1>&2 " 1. Translate the contents of ${cfg.configPath} into a Nix configuration."
echo 1>&2 " See https://github.com/cachix/git-hooks.nix#getting-started"
echo 1>&2 " 2. Remove ${cfg.configPath}"
echo 1>&2 " 3. Add ${cfg.configPath} to .gitignore"
else
if ${boolToString cfg.addGcRoot}; then
nix-store --add-root "''${GIT_WC}/${cfg.configPath}" --indirect --realise ${cfg.configFile}
Expand Down
36 changes: 16 additions & 20 deletions nix/run.nix
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
builtinStuff@{ pkgs, tools, isFlakes, pre-commit, git, runCommand, writeText, writeScript, lib, gitignore-nix-src }:

{ src
, settings ? { }
, hooks ? { }
, excludes ? [ ]
, tools ? { }
, default_stages ? [ "pre-commit" ]
, addGcRoot ? true
options@{ src
, imports ? [ ]
, configPath ? ".pre-commit-config.yaml"
, tools ? { }
, ...
}:
let
moduleOptions = builtins.removeAttrs options [ "imports" "tools" ];

project =
lib.evalModules {
modules =
[
../modules/all-modules.nix
{
config =
{
_module.args.pkgs = pkgs;
_module.args.gitignore-nix-src = gitignore-nix-src;
inherit hooks excludes default_stages settings addGcRoot configPath;
tools = builtinStuff.tools // tools;
package = pre-commit;
} // (if isFlakes
then { rootSrc = src; }
else {
rootSrc = gitignore-nix-src.lib.gitignoreSource src;
});
config = moduleOptions //
{
_module.args.pkgs = pkgs;
_module.args.gitignore-nix-src = gitignore-nix-src;
package = lib.mkDefault pre-commit;
tools = lib.mkDefault (builtinStuff.tools // tools);
} // (if isFlakes
then { rootSrc = src; }
else {
rootSrc = gitignore-nix-src.lib.gitignoreSource src;
});
}
] ++ imports;
};
Expand Down