Skip to content

Commit 223a5af

Browse files
author
Théophane Hufschmitt
committed
Make the hooks more verbose
Print the hook name when it runs. Makes it easier to understand what's going on
1 parent 6395b35 commit 223a5af

File tree

5 files changed

+88
-28
lines changed

5 files changed

+88
-28
lines changed

Diff for: lib/files.ncl

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ let nix = import "./nix-interop/nix.ncl" in
6363
shells.build.hooks =
6464
if filegen_hook.enable then
6565
{
66-
filegen_hook = nix-s%"
66+
filegen_hook.content = nix-s%"
6767
%{regenerate_files files}/bin/regenerate-files
6868
"%
6969
}

Diff for: lib/nix-interop/builders.ncl

-23
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,6 @@ in
7676
}
7777
| NickelPkg,
7878

79-
Shell
80-
| doc m%"
81-
A derivation that is to be used as a shell, e.g. with `nix develop`.
82-
Analogous to `mkShell`.
83-
"%
84-
=
85-
NixpkgsPkg
86-
& {
87-
hooks | doc "Bash scripts to run when entering the shell" = {},
88-
89-
name | default = "shell",
90-
version | default = "dev",
91-
packages | doc "Packages to be added to the shell, setting PATH, LD_LIBRARY_PATH and other variables as needed" = {},
92-
93-
env.buildCommand = nix-s%"
94-
echo "This derivation is not supposed to be built" 1>&2 1>/dev/null
95-
exit 1
96-
"%,
97-
env.shellHook = concat_strings_sep "\n" (std.record.values hooks),
98-
structured_env.nativeBuildInputs = packages,
99-
}
100-
| NickelPkg,
101-
10279
ShellApplication
10380
| doc m%"
10481
A derivation that writes contents of `content.file` to `bin/$name` in the output and makes it executable.

Diff for: lib/nix-interop/shells.ncl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
let builders = import "builders.ncl" in
21
let nix_builtins = import "builtins.ncl" in
32

43
let concat_strings_sep = fun sep values =>
@@ -9,6 +8,7 @@ let concat_strings_sep = fun sep values =>
98
in
109

1110
{
11+
Base = import "./shells/base.ncl",
1212
Bash = import "./shells/bash.ncl",
1313
Rust = import "./shells/rust.ncl",
1414

@@ -95,9 +95,9 @@ in
9595
build.packages.opam = nix_builtins.import_nix "nixpkgs#opam",
9696
dev.packages.ocaml-lsp = nix_builtins.import_nix "nixpkgs#ocamlPackages.ocaml-lsp",
9797

98-
dev.hooks.enter_opam_env = m%"
98+
dev.hooks.enter_opam_env.content = m%"
9999
echo "==> Reloading the OPAM environment."
100-
echo "==> Set 'shells.dev.hooks.enter_opam_env' to false to disable."
100+
echo "==> Set 'shells.dev.hooks.enter_opam_env' to `""` to disable."
101101
eval $(opam env)
102102
"%,
103103
},

Diff for: lib/nix-interop/shells/base.ncl

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
let builders = import "../builders.ncl" in
2+
let derivation = import "../derivation.ncl" in
3+
4+
let Hook = {
5+
name
6+
| doc m%"
7+
The name of the hook.
8+
If null, defaults to the attribute name.
9+
"%
10+
| String
11+
| optional,
12+
enabled
13+
| doc m%"
14+
Whether to run the hook
15+
"%
16+
| Bool
17+
| default
18+
= true,
19+
verbose
20+
| doc m%"
21+
Whether to announce when the hook runs
22+
"%
23+
| Bool
24+
| default
25+
= true,
26+
content
27+
| doc m%"
28+
The bash snippet to run
29+
"%
30+
| derivation.NixString,
31+
}
32+
in
33+
let concat_strings_sep = fun sep values =>
34+
if std.array.length values == 0 then
35+
""
36+
else
37+
std.array.reduce_left (fun acc value => nix-s%"%{acc}%{sep}%{value}"%) values
38+
in
39+
40+
let hook_to_string | Hook -> derivation.NixString = fun hook =>
41+
if hook.enabled then
42+
let header =
43+
if hook.verbose then
44+
nix-s%"
45+
echo -e '\033[34;1m==> Running hook %{hook.name}\033[0m'
46+
"%
47+
else
48+
nix-s%""%
49+
in
50+
51+
nix-s%"
52+
%{header}
53+
%{hook.content}
54+
"%
55+
else
56+
nix-s%""%
57+
in
58+
let NormaliseNames = fun label items =>
59+
items
60+
|> std.record.map (fun item_name item => item & { name | default = item_name })
61+
in
62+
63+
builders.NixpkgsPkg
64+
& {
65+
hooks
66+
| doc "Bash scripts to run when entering the shell"
67+
| { _ : Hook }
68+
| NormaliseNames
69+
= {},
70+
71+
name | default = "shell",
72+
version | default = "dev",
73+
packages | doc "Packages to be added to the shell, setting PATH, LD_LIBRARY_PATH and other variables as needed" = {},
74+
75+
env.buildCommand = nix-s%"
76+
echo "This derivation is not supposed to be built" 1>&2 1>/dev/null
77+
exit 1
78+
"%,
79+
env.shellHook = concat_strings_sep "\n" (std.record.values hooks |> std.array.map hook_to_string),
80+
structured_env.nativeBuildInputs = packages,
81+
}
82+
| builders.NickelPkg

Diff for: lib/nix-interop/shells/bash.ncl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
let builders = import "../builders.ncl" in
2+
let base = import "./base.ncl" in
23
let nix_builtins = import "../builtins.ncl" in
34
{
45
build =
5-
builders.Shell
6+
base
67
& {
78
packages = {
89
bash = nix_builtins.import_nix "nixpkgs#bash",

0 commit comments

Comments
 (0)