Skip to content

Commit

Permalink
Invert project.nix dependency, simplify installation script
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth committed Oct 1, 2019
1 parent 2739673 commit 669b2f3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DO NOT MODIFY
# This file was generated by project.nix
# This file was generated by nix-pre-commit-hooks
{
"repos": [
{
Expand Down
80 changes: 38 additions & 42 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ let
[ $? -eq 0 ] && exit $exitcode
'';

srcStr = toString ( config.root.origSrc or config.root );

# TODO: provide a default pin that the user may override
inherit (import (import ../nix/sources.nix).gitignore { inherit lib; })
gitignoreSource
Expand Down Expand Up @@ -255,6 +253,16 @@ in {
default = run;
};

installationScript =
mkOption {
type = types.str;
description =
''
A bash snippet that installs nix-pre-commit in the current directory
'';
readOnly = true;
};

rootSrc =
mkOption {
type = types.package;
Expand All @@ -269,51 +277,39 @@ in {
};

config =
mkIf cfg.enable {

shell.packages = [ cfg.package ];
{

activation.hooks =
mkIf cfg.enableAutoInstall [
''
export PATH=$PATH:${cfg.package}/bin
if ! type -t git >/dev/null; then
# This happens in pure shells, including lorri
echo 1>&2 "WARNING: nix-pre-commit-hooks: git command not found; skipping installation."
pre-commit.installationScript =
''
export PATH=$PATH:${cfg.package}/bin
if ! type -t git >/dev/null; then
# This happens in pure shells, including lorri
echo 1>&2 "WARNING: nix-pre-commit-hooks: git command not found; skipping installation."
else
# Avoid filesystem churn. We may be watched!
# This prevents lorri from looping after every interactive shell command.
if readlink .pre-commit-hooks >/dev/null \
&& [[ $(readlink .pre-commit-hooks) == ${hooks} ]]; then
echo 1>&2 "nix-pre-commit-hooks: hooks up to date"
else
(
# We use srcStr to protect against installing pre-commit hooks
# in the wrong places such as for example ./. when invoking
# nix-shell ../../other-project/shell.nix
cd ${lib.escapeShellArg srcStr} && {
# Avoid filesystem churn. We may be watched!
# This prevents lorri from looping after every interactive shell command.
if readlink .pre-commit-hooks >/dev/null \
&& [[ $(readlink .pre-commit-hooks) == ${hooks} ]]; then
echo 1>&2 "nix-pre-commit-hooks: hooks up to date"
else
echo 1>&2 "nix-pre-commit-hooks: updating" ${lib.escapeShellArg srcStr}
echo 1>&2 "nix-pre-commit-hooks: updating $PWD"
[ -L .pre-commit-hooks ] && unlink .pre-commit-hooks
ln -s ${hooks} .pre-commit-hooks
[ -L .pre-commit-hooks ] && unlink .pre-commit-hooks
ln -s ${hooks} .pre-commit-hooks
# This can't be a symlink because its path is not constant,
# thus can not be committed and is invisible to pre-commit.
unlink .pre-commit-config.yaml
{ echo '# DO NOT MODIFY';
echo '# This file was generated by project.nix';
${pkgs.jq}/bin/jq . <${configFile}
} >.pre-commit-config.yaml
# This can't be a symlink because its path is not constant,
# thus can not be committed and is invisible to pre-commit.
unlink .pre-commit-config.yaml
{ echo '# DO NOT MODIFY';
echo '# This file was generated by nix-pre-commit-hooks';
${pkgs.jq}/bin/jq . <${configFile}
} >.pre-commit-config.yaml
pre-commit install
# this is needed as the hook repo configuration is cached
pre-commit clean
fi
}
)
pre-commit install
# this is needed as the hook repo configuration is cached
pre-commit clean
fi
''
];

fi
'';
};
}
11 changes: 11 additions & 0 deletions nix/project-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
This module is picked up by project.nix but is also used internally
to find these imported modules.
*/
{
imports =
[
../modules/pre-commit.nix
../modules/hooks.nix
];
}
45 changes: 22 additions & 23 deletions nix/run.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,35 @@
let
sources = import ./sources.nix;

# TODO upstream this and add tests to support this use case
project-nix-core =
map ( f: sources."project.nix" + ("/" + f) ) [
"modules/root.nix"
"modules/activation.nix"
"modules/shell.nix"
"modules/nixpkgs.nix"
];

project =
lib.evalModules {
modules =
project-nix-core ++ [
[
./project-module.nix
{
root = src;
nixpkgs.pkgs = pkgs;
pre-commit.tools = lib.mkDefault tools;
pre-commit.enable = true;
pre-commit.hooks = hooks;
options =
{
root =
lib.mkOption {
description = "Internal option";
default = src;
internal = true;
readOnly = true;
type = lib.types.unspecified;
};
};
config =
{
_module.args.pkgs = pkgs;
pre-commit.hooks = hooks;
pre-commit.tools = lib.mkDefault tools;
};
}
../modules/pre-commit.nix
../modules/hooks.nix
];
];
};
inherit (project.config.shell.shell) shellHook activationHook;
inherit (project.config.pre-commit) installationScript;

in
project.config.pre-commit.run // {
shellHook = ''
activationHook=${lib.escapeShellArg activationHook}
${shellHook}
'';
shellHook = installationScript;
}
12 changes: 0 additions & 12 deletions nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,5 @@
"type": "tarball",
"url": "https://github.com/tweag/ormolu/archive/a7076c0f83e5c06ea9067b71171859fa2ba8afd9.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"project.nix": {
"branch": "pre-commit",
"description": "A configuration manager for your projects",
"homepage": null,
"owner": "hercules-ci",
"repo": "project.nix",
"rev": "ec17ac79904a296f358961ffcff7a13948c2b92e",
"sha256": "1gkb9k995ly53d5sxn5hr4lyzh57sq25j3i2cw8zva92g8x69gb5",
"type": "tarball",
"url": "https://github.com/hercules-ci/project.nix/archive/ec17ac79904a296f358961ffcff7a13948c2b92e.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}

0 comments on commit 669b2f3

Please sign in to comment.