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

Commit a3da443

Browse files
committed
Add activation.hooks for pre-commit install
1 parent bdb655c commit a3da443

File tree

4 files changed

+77
-7
lines changed

4 files changed

+77
-7
lines changed

default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ let
1717
./modules/shell.nix
1818
./modules/niv.nix
1919
./modules/pre-commit.nix
20+
./modules/activation.nix
2021
];
2122

2223
in

modules/activation.nix

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{ config, lib, ... }:
2+
3+
let
4+
5+
inherit (lib)
6+
concatStringsSep
7+
hasPrefix
8+
literalExample
9+
mkIf
10+
mkOption
11+
mkOptionType
12+
mkOrder
13+
types
14+
;
15+
16+
cfg = config.activation;
17+
18+
isOutsideStore = p: !(hasPrefix builtins.storeDir (toString p));
19+
20+
in
21+
{
22+
options = {
23+
24+
activation.hooks = mkOption {
25+
type = types.listOf types.str;
26+
description = ''
27+
bash snippets to run on activation.
28+
29+
This is quite distinct from a shell hook:
30+
- the working directory is always the project root.
31+
- variables are not propagated to the shell.
32+
- activation hooks may be run separately or before most shell.hooks.
33+
'';
34+
default = [];
35+
};
36+
37+
activation.enableShellHook = mkOption {
38+
type = types.bool;
39+
description = ''
40+
Whether to run the activation hooks whenever the project shell is opened.
41+
'';
42+
default = true;
43+
};
44+
45+
};
46+
47+
config =
48+
mkIf cfg.enableShellHook {
49+
50+
shell.extraAttrs.activationHook =
51+
concatStringsSep "\n" cfg.hooks;
52+
53+
shell.hooks = mkIf (isOutsideStore config.root) (mkOrder 300 [''
54+
(
55+
echo 1>&2 project.nix: activating in ${lib.escapeShellArg config.root}
56+
cd ${lib.escapeShellArg config.root}
57+
runHook activationHook
58+
)
59+
'']);
60+
};
61+
62+
}

modules/pre-commit.nix

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,9 @@ in
163163

164164
config = mkIf cfg.enable {
165165

166-
# TODO: generalize the setup logic
167-
# TODO: only autoinstall when in the project root
168-
# TODO: always ignore when src is in the store
169-
shell.hooks = mkIf cfg.enableAutoInstall [
166+
shell.packages = [ cfg.package ];
167+
168+
activation.hooks = mkIf cfg.enableAutoInstall [
170169
''
171170
export PATH=$PATH:${cfg.package}/bin
172171
if ! type -t git >/dev/null; then

modules/shell.nix

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ in
2828
example = [''if ! type git >/dev/null; then echo 1>&2 "git command not found! Please install git on your system or user profile"; fi''];
2929
};
3030

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

3341
shell = mkOption {
3442
type = types.package;
@@ -39,9 +47,9 @@ in
3947
};
4048

4149
config = {
42-
shell.shell = pkgs.mkShell {
50+
shell.shell = pkgs.mkShell (cfg.extraAttrs // {
4351
nativeBuildInputs = cfg.packages;
4452
shellHook = lib.concatStringsSep "\n" cfg.hooks;
45-
};
53+
});
4654
};
4755
}

0 commit comments

Comments
 (0)