-
Notifications
You must be signed in to change notification settings - Fork 9
/
flake.nix
195 lines (183 loc) · 6.07 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
{
description = "A flake demonstrating how to build OCaml projects with Dune";
# Flake dependency specification
#
# To update all flake inputs:
#
# $ nix flake update --commit-lock-file
#
# To update individual flake inputs:
#
# $ nix flake lock --update-input <input> ... --commit-lock-file
#
inputs = {
# Externally extensible flake systems. See <https://github.com/nix-systems/nix-systems>.
systems.url = "github:nix-systems/default";
};
outputs = { self, nixpkgs, systems }:
let
# Nixpkgs library functions.
lib = nixpkgs.lib;
# Iterate over each system, configured via the `systems` input.
eachSystem = lib.genAttrs (import systems);
in
{
# Exposed packages that can be built or run with `nix build` or
# `nix run` respectively:
#
# $ nix build .#<name>
# $ nix run .#<name> -- <args?>
#
packages = eachSystem (system:
let
legacyPackages = nixpkgs.legacyPackages.${system};
ocamlPackages = legacyPackages.ocamlPackages;
in
{
# The package that will be built or run by default. For example:
#
# $ nix build
# $ nix run -- <args?>
#
default = self.packages.${system}.hello;
hello = ocamlPackages.buildDunePackage {
pname = "hello";
version = "0.1.0";
duneVersion = "3";
src = ./.;
buildInputs = [
# OCaml package dependencies go here.
];
strictDeps = true;
};
});
# Flake checks
#
# $ nix flake check
#
checks = eachSystem (system:
let
legacyPackages = nixpkgs.legacyPackages.${system};
ocamlPackages = legacyPackages.ocamlPackages;
in
{
# Run tests for the `hello` package
hello =
let
# Patches calls to dune commands to produce log-friendly output
# when using `nix ... --print-build-log`. Ideally there would be
# support for one or more of the following:
#
# In Dune:
#
# - have workspace-specific dune configuration files
#
# In NixPkgs:
#
# - allow dune flags to be set in in `ocamlPackages.buildDunePackage`
# - alter `ocamlPackages.buildDunePackage` to use `--display=short`
# - alter `ocamlPackages.buildDunePackage` to allow `--config-file=FILE` to be set
patchDuneCommand =
let
subcmds = [ "build" "test" "runtest" "install" ];
in
lib.replaceStrings
(lib.lists.map (subcmd: "dune ${subcmd}") subcmds)
(lib.lists.map (subcmd: "dune ${subcmd} --display=short") subcmds);
in
self.packages.${system}.hello.overrideAttrs
(oldAttrs: {
name = "check-${oldAttrs.name}";
doCheck = true;
buildPhase = patchDuneCommand oldAttrs.buildPhase;
checkPhase = patchDuneCommand oldAttrs.checkPhase;
# installPhase = patchDuneCommand oldAttrs.checkPhase;
});
# Check Dune and OCaml formatting
dune-fmt = legacyPackages.runCommand "check-dune-fmt"
{
nativeBuildInputs = [
ocamlPackages.dune_3
ocamlPackages.ocaml
legacyPackages.ocamlformat
];
}
''
echo "checking dune and ocaml formatting"
dune build \
--display=short \
--no-print-directory \
--root="${./.}" \
--build-dir="$(pwd)/_build" \
@fmt
touch $out
'';
# Check documentation generation
dune-doc = legacyPackages.runCommand "check-dune-doc"
{
ODOC_WARN_ERROR = "true";
nativeBuildInputs = [
ocamlPackages.dune_3
ocamlPackages.ocaml
ocamlPackages.odoc
];
}
''
echo "checking ocaml documentation"
dune build \
--display=short \
--no-print-directory \
--root="${./.}" \
--build-dir="$(pwd)/_build" \
@doc
touch $out
'';
# Check Nix formatting
nixpkgs-fmt = legacyPackages.runCommand "check-nixpkgs-fmt"
{ nativeBuildInputs = [ legacyPackages.nixpkgs-fmt ]; }
''
echo "checking nix formatting"
nixpkgs-fmt --check ${./.}
touch $out
'';
});
# Development shells
#
# $ nix develop .#<name>
# $ nix develop .#<name> --command dune build @test
#
# [Direnv](https://direnv.net/) is recommended for automatically loading
# development environments in your shell. For example:
#
# $ echo "use flake" > .envrc && direnv allow
# $ dune build @test
#
devShells = eachSystem (system:
let
legacyPackages = nixpkgs.legacyPackages.${system};
ocamlPackages = legacyPackages.ocamlPackages;
in
{
default = legacyPackages.mkShell {
# Development tools
packages = [
# Source file formatting
legacyPackages.nixpkgs-fmt
legacyPackages.ocamlformat
# For `dune build --watch ...`
legacyPackages.fswatch
# For `dune build @doc`
ocamlPackages.odoc
# OCaml editor support
ocamlPackages.ocaml-lsp
# Fancy REPL thing
ocamlPackages.utop
];
# Tools from packages
inputsFrom = [
self.packages.${system}.hello
];
};
});
};
}