Skip to content

Commit

Permalink
feat(nix): Update lefthook nix hook configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
sudosubin committed Jan 22, 2023
1 parent a99984d commit 8e67af7
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# auto generated lefthook configuration file
/lefthook.yml
43 changes: 43 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
description = "Easily manage git hooks with Nix, internally using lefthook.";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
lib = import ./lib { inherit pkgs; };
pkgs = import nixpkgs { inherit system; };

in
{
inherit lib;

checks = {
lefthook-check = lib.run {
src = ./.;
config = {
pre-commit.commands = {
nixpkgs-fmt.run = "${pkgs.nixpkgs-fmt}/bin/nixpkgs-fmt {staged_files}";
};
};
};
};

devShell = nixpkgs.legacyPackages.${system}.mkShell {
inherit (self.checks.${system}.lefthook-check) shellHook;
};
}
);
}
116 changes: 116 additions & 0 deletions lib/config/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{ config, lib, pkgs, ... }:

let
inherit (lib) mkOption types;
cfg = config;

configFile = pkgs.runCommand "lefthook.yml"
{
buildInputs = [ pkgs.jq ];
passAsFile = [ "rawJSON" ];
rawJSON = builtins.toJSON cfg.config;
} ''
{
echo "# This file is generated by lefthook.nix in your project."
echo "# Manual changes might be lost."
jq . <"$rawJSONPath"
} >$out
'';

in
{
options = {
package = mkOption {
type = types.package;
description = lib.mdDoc ''
`lefthook` package to use.
'';
default = pkgs.lefthook;
};

config = mkOption {
type = types.attrs;
description = lib.mdDoc ''
lefthook configuration to use.
'';
};

rootSrc = mkOption {
type = types.path;
description = lib.mdDoc ''
The root path for lefthook.
'';
};

run = mkOption {
type = types.package;
readOnly = true;
};

installationScript = mkOption {
type = types.str;
readOnly = true;
};
};

config = {
run = pkgs.runCommand "lefthook-run" { buildInputs = with pkgs; [ git ]; } ''
set +e
export HOME="$PWD"
cp -R ${cfg.rootSrc} ./src
rm -rf ./src/.git
chmod -R +w ./src
ln -fs ${configFile} ./src/lefthook.yml
cd ./src
git init
git add .
${cfg.package}/bin/lefthook run pre-commit
exitcode=$?
touch $out # TODO
[ $? -eq 0 ] && exit $exitcode
'';

installationScript = ''
export PATH=${cfg.package}/bin:$PATH
function _log() { echo 1>&2 "$*"; }
if ! command -v git >/dev/null; then
_log "WARNING: lefthook.nix: git command not found, skipping installation."
elif [ ! -d .git ]; then
_log "WARNING: lefthook.nix: .git directory does not exist, skipping installation."
else
# These update procedures compare before they write, to avoid
# filesystem churn. This improves performance with watch tools like lorri
# and prevents installation loops by via lorri.
if readlink lefthook.yml >/dev/null \
&& [[ $(readlink lefthook.yml) == ${configFile} ]]; then
_log "lefthook.nix: lefthook configuration up to date"
else
_log "lefthook.nix: updating $PWD lefthook configuration"
if [ -L lefthook.yml ]; then
unlink lefthook.yml
fi
if [ -f lefthook.yml ]; then
_log "WARNING: lefthook.nix: lefthook.yml already exists. Please remove lefthook.yml and add lefthook.yml to .gitignore."
else
ln -s ${configFile} lefthook.yml
# Remove previously installed git hooks
lefthook uninstall
# Add lefthook git hooks
lefthook install
fi
fi
fi
unset -f _log
'';
};
}
5 changes: 5 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{ pkgs }:

{
run = pkgs.callPackage ./run.nix { inherit pkgs; };
}
15 changes: 15 additions & 0 deletions lib/run.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{ pkgs, lib }:
{ config ? { }, src }:

let
eval = lib.evalModules {
modules = [
(import ./config)
{ config = { inherit config; rootSrc = src; _module.args.pkgs = pkgs; }; }
];
};

in
eval.config.run // {
shellHook = eval.config.installationScript;
}

0 comments on commit 8e67af7

Please sign in to comment.