Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Pre commit #10

Merged
merged 19 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from 17 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
8 changes: 8 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
eval "$(lorri direnv)"

# Use system PKI
unset SSL_CERT_FILE
unset NIX_SSL_CERT_FILE

# Install pre-commit hooks
eval "$shellHook"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# Nix package written by the pre-commit integration
.pre-commit-hooks
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# DO NOT MODIFY
# This file was generated by nix-pre-commit-hooks
{
"exclude": "(nix/sources.nix$)",
"repos": [
{
"hooks": [
{
"id": "nixpkgs-fmt"
}
],
"repo": ".pre-commit-hooks/",
"rev": "master"
}
]
}
5 changes: 3 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{ lib ?
import ((import ./nix/sources.nix).nixpkgs + "/lib")
{ lib ? import ((import ./nix/sources.nix).nixpkgs + "/lib")
, ...
}:

Expand All @@ -16,6 +15,8 @@ let
./modules/nixpkgs.nix
./modules/shell.nix
./modules/niv.nix
./modules/pre-commit.nix
./modules/activation.nix
];

in
Expand Down
69 changes: 69 additions & 0 deletions modules/activation.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{ config, lib, ... }:

let

inherit (lib)
concatStringsSep
hasPrefix
literalExample
mkIf
mkOption
mkOptionType
mkOrder
types
;

cfg = config.activation;

isOutsideStore = p: !(hasPrefix builtins.storeDir (toString p));

in
{
options = {

activation.hooks = mkOption {
type = types.listOf types.str;
description = ''
bash snippets to run on activation.

This is quite distinct from a shell hook:
- the working directory is always the project root.
- variables are not propagated to the shell.
- activation hooks may be run separately or before most shell.hooks.
'';
default = [];
};

activation.enableShellHook = mkOption {
type = types.bool;
description = ''
Whether to run the activation hooks whenever the project shell is opened.
'';
default = true;
};

};

config =
mkIf cfg.enableShellHook {

shell.extraAttrs.activationHook =
concatStringsSep "\n" cfg.hooks;

shell.hooks = mkIf (isOutsideStore config.root) (
mkOrder 300 [
''
# lorri runs the shellHook in the sandbox first, so skip if that's happening.
if ! [[ "$name" =~ lorri-.*-hack ]]; then
(
echo 1>&2 project.nix: activating in ${lib.escapeShellArg config.root}
cd ${lib.escapeShellArg config.root}
runHook activationHook
)
fi
''
]
);
};

}
18 changes: 11 additions & 7 deletions modules/niv.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }:
{ config, lib, options, pkgs, ... }:

let
inherit (lib) mkIf mkOption types;
Expand Down Expand Up @@ -38,9 +38,13 @@ in

};

config = mkIf cfg.enable {
shell.packages = [
cfg.package
];
};
}
config = mkIf cfg.enable (
{
shell.packages = [
cfg.package
];
} // lib.optionalAttrs (options ? pre-commit.excludes) {
pre-commit.excludes = [ "nix/sources.nix$" ];
}
);
}
49 changes: 49 additions & 0 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{ config, lib, pkgs, ... }:

let

inherit (lib)
mkIf
mkOption
types
;

cfg = config.pre-commit;

in
{
options.pre-commit = {

enable = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable pre-commit integration.

https://pre-commit.com/
'';
};

enableAutoInstall = mkOption {
roberth marked this conversation as resolved.
Show resolved Hide resolved
type = types.bool;
default = true;
description = ''
Whether to auto install pre-commit when invoking nix-shell in the
project root.

Unused if not pre-commit.enabled.
'';
};

};

config = mkIf cfg.enable {

shell.packages = [ cfg.package ];

activation.hooks = mkIf cfg.enableAutoInstall [
config.pre-commit.installationScript
];

};
}
13 changes: 4 additions & 9 deletions modules/root.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ in
The root of the project.

This must be defined as ../. in nix/project.nix.

When reading the option, take care not to accidentally add it to
the store in its entirety. In particular, use

config.root + "/somepath"

and avoid interpolation, toString etc until you want to add paths
to the store.
'';

# Strings are harder to accidentally add to the store.
apply = toString;
};
};
}
}
34 changes: 29 additions & 5 deletions modules/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,30 @@ in
'';
};

# TODO shellHook hooks
hooks = mkOption {
type = types.listOf types.str;
description = ''
bash snippets to run when entering the project's nix-shell.
'';
default = [];
example = [
''
if ! type git >/dev/null; then
echo 1>&2 "git command not found! Please install git on your system or user profile";
fi
''
];
};

# TODO environment variables
# TODO: can we specify merge functions in an extensible way?
extraAttrs = mkOption {
type = types.attrsOf types.str;
description = ''
Extra variables to set in the project's nix-shell.
'';
default = {};
example = { LANG = "en_US.UTF-8"; };
};

shell = mkOption {
type = types.package;
Expand All @@ -32,8 +53,11 @@ in
};

config = {
shell.shell = pkgs.mkShell {
nativeBuildInputs = cfg.packages;
};
shell.shell = pkgs.mkShell (
cfg.extraAttrs // {
nativeBuildInputs = cfg.packages;
shellHook = lib.concatStringsSep "\n" cfg.hooks;
}
);
};
}
7 changes: 7 additions & 0 deletions nix/ci.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ projectNix ? import ../default.nix {}
, project ? projectNix.evalProject { modules = [ ./project.nix ]; }
}:

{
pre-commit = project.config.pre-commit.run;
}
19 changes: 18 additions & 1 deletion nix/project.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
{
/*

This is a _project.nix configuration_
for project.nix _itself_.

*/
{ config, lib, pkgs, ... }: {

imports = [
((import ./sources.nix).nix-pre-commit-hooks + "/nix/project-module.nix")
];

root = ../.;
pinning.niv.enable = true;
pre-commit.enable = true;
pre-commit.tools.nixpkgs-fmt = lib.mkForce pkgs.nixpkgs-fmt;
pre-commit.hooks.nixpkgs-fmt.enable = true;

# TODO: upstream the command line change, remove this
pre-commit.hooks.nixpkgs-fmt.entry = lib.mkForce "${config.pre-commit.tools.nixpkgs-fmt}/bin/nixpkgs-fmt";
}
24 changes: 24 additions & 0 deletions nix/sources.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
{
"gitignore": {
"branch": "master",
"description": "Nix function for filtering local git sources",
"homepage": "",
"owner": "hercules-ci",
"repo": "gitignore",
"rev": "f9e996052b5af4032fe6150bba4a6fe4f7b9d698",
"sha256": "0jrh5ghisaqdd0vldbywags20m2cxpkbbk5jjjmwaw0gr8nhsafv",
"type": "tarball",
"url": "https://github.com/hercules-ci/gitignore/archive/f9e996052b5af4032fe6150bba4a6fe4f7b9d698.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"niv": {
"branch": "master",
"description": "Easy dependency management for Nix projects",
Expand All @@ -11,6 +23,18 @@
"url": "https://github.com/nmattia/niv/archive/efce82e4ba77a84835f9febefe3b2fe52a8c7ede.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nix-pre-commit-hooks": {
"branch": "global-exclude",
"description": "Seamless integration of https://pre-commit.com git hooks with Nix.",
"homepage": "",
"owner": "hercules-ci",
"repo": "nix-pre-commit-hooks",
"rev": "605a39d10b2ce3a8885a18c1b97aeea9d21316b1",
"sha256": "1aal2y05g20d7ny7z1hmsfmd0syfqadfr8g9sfzs7v9h6q5z8jv3",
"type": "tarball",
"url": "https://github.com/hercules-ci/nix-pre-commit-hooks/archive/605a39d10b2ce3a8885a18c1b97aeea9d21316b1.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs": {
"branch": "nixos-19.09",
"description": "A read-only mirror of NixOS/nixpkgs tracking the released channels. Send issues and PRs to",
Expand Down
6 changes: 2 additions & 4 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{ projectNix ?
import ./default.nix {}
, project ?
projectNix.evalProject { modules = [ ./nix/project.nix ]; }
{ projectNix ? import ./default.nix {}
, project ? projectNix.evalProject { modules = [ ./nix/project.nix ]; }
}:

project.config.shell.shell