Skip to content

Add nix flakes support #67

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

Closed
wants to merge 4 commits into from
Closed
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
72 changes: 68 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ The goal is to **manage commit hooks with Nix** and solve the following:
1. (optional) Use binary caches to avoid compilation:

```bash
$ nix-env -iA cachix -f https://cachix.org/api/v1/install
$ cachix use pre-commit-hooks
nix-env -iA cachix -f https://cachix.org/api/v1/install
cachix use pre-commit-hooks
```

2. Integrate hooks to be built as part of `default.nix`:
Expand All @@ -40,7 +40,7 @@ The goal is to **manage commit hooks with Nix** and solve the following:
}
```

Run `$ nix-build -A pre-commit-check` to perform the checks as a Nix derivation.
Run `nix-build -A pre-commit-check` to perform the checks as a Nix derivation.

3. Integrate hooks to prepare environment as part of `shell.nix`:
```nix
Expand All @@ -53,12 +53,76 @@ The goal is to **manage commit hooks with Nix** and solve the following:

Add `/.pre-commit-config.yaml` to `.gitignore`.

Run `$ nix-shell` to execute `shellHook` which will:
Run `nix-shell` to execute `shellHook` which will:

- build the tools and `.pre-commit-config.yaml` config file symlink which
references the binaries, for speed and safe garbage collection
- provide the `pre-commit` executable that `git commit` will invoke

## nix flakes

To initialise a project with a `flake.nix` and pre-commit hooks, run:

```bash
nix flake init -t github:cachix/pre-commit-hooks.nix
```

Alternatively, the following snippet shows how pre-commit hooks may be integrated into an already existing `flake.nix`:

```nix
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs-channels/nixos-20.03";

inputs.flake-utils.url = "github:numtide/flake-utils";

inputs.pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix/master";

outputs = { self, nixpkgs, flake-utils, pre-commit-hooks }:
flake-utils.lib.eachDefaultSystem
(
system: rec {
pre-commit-check = pre-commit-hooks.packages.${system}.run {
src = ./.;
# If your hooks are intrusive, avoid running on each commit with a default_states like this:
# default_stages = ["manual" "push"];
hooks = {
elm-format.enable = true;
ormolu.enable = true;
shellcheck.enable = true;
};
};

devShell =
nixpkgs.legacyPackages.${system}.mkShell {
inherit (pre-commit-check) shellHook;
};
}
);
}
```

Add `/.pre-commit-config.yaml` to the `.gitignore`.

With this in place you may run

```bash
nix build '.#pre-commit-check.{system}' --impure
```

(in which `{system}` is one of `aarch64-linux`, `i686-linux`, `x86_64-darwin`, and `x86_64-linux`)

to perform the checks as a Nix derivation or run

```bash
nix develop
```

to execute the `shellHook` which will:

- build the tools and `.pre-commit-config.yaml` config file symlink which
references the binaries, for speed and safe garbage collection
- provide the `pre-commit` executable that `git commit` will invoke

## Optional

### Direnv + Lorri
Expand Down
14 changes: 13 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
let pkgs = import ./nix {}; in pkgs.packages
(
import (
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
) {
src = ./.;
}
).defaultNix.outputs.packages.${builtins.currentSystem}
114 changes: 114 additions & 0 deletions flake.lock

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

88 changes: 88 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
description = "Seamless integration of pre-commit git hooks with Nix";

inputs.nixpkgs.url = "github:NixOS/nixpkgs-channels/nixos-20.03";

inputs.flake-utils.url = "github:numtide/flake-utils";

inputs.flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};

inputs.cabal-fmt-src = {
url = "github:phadej/cabal-fmt/master";
flake = false;
};

inputs.gitignore-nix-src = {
url = "github:hercules-ci/gitignore/master";
flake = false;
};

inputs.hindent-src = {
url = "github:chrisdone/hindent/master";
flake = false;
};

outputs = { self, nixpkgs, flake-utils, flake-compat, cabal-fmt-src, gitignore-nix-src, hindent-src }:
let
module = { config, lib, pkgs, ... }: let
inherit (lib)
mkDefault
;
inherit (import gitignore-nix-src { inherit lib; })
gitignoreSource
;
in
{
imports = [ ./modules/all-modules.nix ];

config = {
pre-commit.tools = mkDefault ((pkgs.callPackage ./nix { inherit hindent-src cabal-fmt-src; }).callPackage ./nix/tools.nix {});
pre-commit.rootSrc = mkDefault (gitignoreSource config.root);
};
};
in
flake-utils.lib.eachDefaultSystem
(
system:
let
pkgs = import ./nix {
inherit nixpkgs hindent-src cabal-fmt-src system;
pre-commit-hooks-module = module;
};
pre-commit-check = pkgs.packages.run {
src = ./.;
hooks = {
shellcheck.enable = true;
nixpkgs-fmt.enable = true;
};
excludes = [
# autogenerated by nix flake update
"flake.lock$"
];
};
in

{
packages = pkgs.packages;

projectModules.pre-commit-hooks = module;

inherit pre-commit-check;

devShell =
pkgs.mkShell {
inherit (pre-commit-check) shellHook;
};
}
)
// rec {
templates.pre-commit-hook = {
path = ./templates/pre-commit-hooks;
description = "Nix flake with pre-commit hooks";
};
defaultTemplate = templates.pre-commit-hook;
};
}
10 changes: 1 addition & 9 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }:
{ config, lib, pkgs, gitignore-nix, ... }:

let

Expand Down Expand Up @@ -165,10 +165,6 @@ let
[ $? -eq 0 ] && exit $exitcode
'';

# TODO: provide a default pin that the user may override
inherit (import (import ../nix/sources.nix)."gitignore.nix" { inherit lib; })
gitignoreSource
;
in
{
options.pre-commit =
Expand Down Expand Up @@ -198,9 +194,6 @@ in

nix-pre-commit comes with its own set of packages for this purpose.
'';
# This default is for when the module is the entry point rather than
# /default.nix. /default.nix will override this for efficiency.
default = (import ../nix { inherit (pkgs) system; }).callPackage ../nix/tools.nix {};
defaultText =
literalExample ''nix-pre-commit-hooks-pkgs.callPackage tools-dot-nix { inherit (pkgs) system; }'';
};
Expand Down Expand Up @@ -245,7 +238,6 @@ in
The source of the project to be checked.
'';
defaultText = literalExample ''gitignoreSource config.root'';
default = gitignoreSource config.root;
};

excludes =
Expand Down
18 changes: 11 additions & 7 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{ sources ? import ./sources.nix
{ nixpkgs
, hindent-src
, cabal-fmt-src
, pre-commit-hooks-module
, system ? builtins.currentSystem
}:

Expand All @@ -7,24 +10,25 @@ let
_: pkgs:
let
cabal-fmt =
pkgs.haskellPackages.callCabal2nix "cabal-fmt" sources.cabal-fmt {};
pkgs.haskellPackages.callCabal2nix "cabal-fmt" cabal-fmt-src {};
in
{
inherit (pkgs) nixfmt niv ormolu nixpkgs-fmt nix-linter;
hindent =
pkgs.haskellPackages.callCabal2nix "hindent" sources.hindent {};
pkgs.haskellPackages.callCabal2nix "hindent" hindent-src {};
cabal-fmt =
cabal-fmt.overrideScope (
self: super:
{
Cabal = self.Cabal_3_0_0_0;
}
);
packages = pkgs.callPackages ./packages.nix {};
packages = pkgs.callPackages ./packages.nix { inherit pre-commit-hooks-module; };
};
in
import sources.nixpkgs {
overlays = [ overlay ];
import nixpkgs {
overlays = [
overlay
];
config = {};
inherit system;
}
Loading