summaryrefslogtreecommitdiff
path: root/lib/nixtamal.ml
blob: 9ef08e764cca0a1034708e5b2a5312ab9ec06c35 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
(*─────────────────────────────────────────────────────────────────────────────┐
│ SPDX-FileCopyrightText: 2025 toastal <https://toast.al/contact/>             │
│ SPDX-License-Identifier: LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception │
└─────────────────────────────────────────────────────────────────────────────*)
module Error = Error
module Name = Name
module Manifest = Manifest
module Lockfile = Lockfile
module Input = Input
module Input_foreman = Input_foreman
module Working_directory = Working_directory
module KDL = KDL
module Schema = Schema

type error = Error.error

let meld_input_with_lock (input : Input.t) (lock : Lockfile.Input'.t) : Input.t = {input with
	kind = (
		match input.kind, lock.kind with
		| `File file, `File _ ->
			`File file
		| `Archive archive, `Archive _ ->
			`Archive archive
		| `Git git, `Git({datetime; latest_revision; _}: Lockfile.Git.t) ->
			`Git {git with datetime; latest_revision}
		| `Darcs darcs, `Darcs({datetime; reference; latest_weak_hash; _}: Lockfile.Darcs.t) ->
			`Darcs {darcs with
				datetime;
				latest_weak_hash;
				reference = (
					match darcs.reference, reference with
					| `Context (`Assumed None), `Context (`Assumed Some _) -> reference
					| _ -> darcs.reference
				);
			}
		| `Pijul pijul, `Pijul({datetime; latest_state; _}: Lockfile.Pijul.t) ->
			`Pijul {pijul with datetime; latest_state}
		| `Nilla nilla, `Nilla({datetime; latest_revision; _}: Lockfile.Nilla.t) ->
			`Nilla {nilla with datetime; latest_revision}
		| `Fossil fossil, `Fossil({datetime; latest_checkin; _}: Lockfile.Fossil.t) ->
			`Fossil {fossil with date = datetime; latest_checkin}
		| _, _ -> failwith "Input kind mismatch."
	);
	hash = {input.hash with value = lock.hash.value};
	latest = {input.latest with value = lock.latest_value}
}

let read_manifest_and_lockfile () : (Name.Name.t list, error) result =
	let (let*) = Result.bind in
	let* manifest =
		Error.tag_manifest @@ begin
			match Manifest.read () with
			| Ok(kdl : KDL.t) ->
				Manifest.document_to_t kdl
				|> Result.map_error (fun err -> `Parsing err)
			| Error err -> Error err
		end
	in
	let* lockfile_opt =
		Error.tag_lockfile @@ begin
			Lockfile.read ()
			|> Result.map_error (fun e -> `Parsing e)
		end
	in
	match lockfile_opt with
	| Some lock when not (String.equal manifest.version lock.version) ->
		Error (`Version_mismatch (manifest.version, lock.version))
	| _ ->
		let lockfile_opt = match lockfile_opt with
			| Some lock -> Some lock
			| None ->
				Logs.info (fun m -> m "Lockfile missing, creating new empty lockfile");
				match Lockfile.make ~version: manifest.version () with
				| Ok lock -> Some lock
				| Error e ->
					Logs.warn (fun m -> m "Failed to create lockfile: %a" Error.pp_lockfile_error e);
					None
		in
		let to_input d =
			let input = Manifest.Input'.of_manifest d in
			let found_input =
				Option.bind lockfile_opt (fun lock -> Name.NameMap.find_opt input.name lock.inputs)
			in
			match found_input with
			| None -> input
			| Some locked -> meld_input_with_lock input locked
		in
		Error.tag_input_foreman @@ begin
			let rec set_input_htbl ret mnfsts =
				match ret, mnfsts with
				| Error err, _ -> Error err
				| Ok names, [] -> Ok names
				| Ok names, d :: ds ->
					let input : Input.t = to_input d in
					let res =
						Input_foreman.add input.name input
						|> Result.map (fun () -> input.name :: names)
					in
					set_input_htbl res ds
			in
			let* names = set_input_htbl (Ok []) manifest.inputs in
			Logs.debug (fun m -> m "Names from reckoning manifest × lockfile: %a" Fmt.(brackets (list ~sep: semi Name.Name.pp)) names);
			Ok names
		end

let set_up ~env ?nixpkgs: nixpkgs_opt () : (unit, error) result =
	let (let*) = Result.bind in
	Eio.Switch.run @@ fun sw ->
	let proc_mgr = Eio.Stdenv.process_mgr env in
	Logs.app (fun m -> m "%t@." Banner.pp);
	Working_directory.set_up_root ();
	if Manifest.exists () then
		begin
			Logs.warn (fun m ->
				m
					"Found existing “%s” file, so project is already set up. Skipping."
					Manifest.filename
			);
			Ok ()
		end
	else
		(* TODO: returns a bool for success, so what to do? *)
		let* () =
			match nixpkgs_opt with
			| None -> Ok ()
			| Some npkgs ->
				let* () =
					Error.tag_input_foreman @@ begin
						let* () = Input_foreman.add Input.Nixpkgs.name npkgs in
						Input_foreman.lock_one ~env ~sw ~proc_mgr ~force: false ~name: Input.Nixpkgs.name
					end
				in
				let _lockfile = Lockfile.make () in
				Ok ()
		in
		let* () =
			Error.tag_manifest @@ begin
				let () = Manifest.make () in
				Manifest.write ()
			end
		in
		let* () =
			Error.tag_lockfile @@ begin
				Lockfile.write ~create: (`Exclusive 0o644) ()
			end
		in
		Lock_loader.write ();
		Ok ()

let check_soundness ~env: _ () : (unit, error) result =
	let (let*) = Result.bind in
	let* _all_names = read_manifest_and_lockfile () in
	Logs.app (fun m -> m "All sound.");
	Ok ()

let tweak ~env () : (unit, error) result =
	let working_dir = Working_directory.get () in
	let manifest_file : string =
		let path = Eio.Path.(working_dir / Manifest.filename) in
		Eio.Path.native_exn path
	in
	let () = Editor.run_on manifest_file in
	check_soundness ~env ()

let show ~env: _ () : (unit, error) result =
	let (let*) = Result.bind in
	let* _all_names = read_manifest_and_lockfile () in
	Logs.app (fun m -> m "%t" Input_foreman.pp_for_earthlings);
	Ok ()

let lock ~env ~domain_count ?(force = false) ?names () : (unit, error) result =
	Eio.Switch.run @@ fun sw ->
	let (let*) = Result.bind in
	let proc_mgr = Eio.Stdenv.process_mgr env in
	let* all_names = read_manifest_and_lockfile () in
	let* () =
		Error.tag_input_foreman @@ begin
			let names =
				match names with
				| None | Some [] -> all_names
				| Some ns -> ns
			in
			Input_foreman.lock ~env ~sw ~proc_mgr ~domain_count ~force ~names ()
		end
	in
	let* () = Error.tag_lockfile @@ Lockfile.write () in
	Lock_loader.write ();
	Input_foreman.clean_unlisted_from_silo ();
	Ok ()

let list_stale ~env ~domain_count : (unit, error) result =
	Eio.Switch.run @@ fun sw ->
	let (let*) = Result.bind in
	let proc_mgr = Eio.Stdenv.process_mgr env in
	let* all_names = read_manifest_and_lockfile () in
	Error.tag_input_foreman @@ begin
		Input_foreman.list_stale ~env ~sw ~proc_mgr ~domain_count ~names: all_names
	end

let refresh ~env ~domain_count ?names () : (unit, error) result =
	Eio.Switch.run @@ fun sw ->
	let (let*) = Result.bind in
	let proc_mgr = Eio.Stdenv.process_mgr env in
	let* all_names = read_manifest_and_lockfile () in
	let* () =
		Error.tag_input_foreman @@ begin
			let names =
				match names with
				| None | Some [] -> all_names
				| Some ns -> ns
			in
			Input_foreman.refresh ~env ~sw ~proc_mgr ~domain_count ~names ()
		end
	in
	let* () = Error.tag_lockfile @@ Lockfile.write () in
	Lock_loader.write ();
	Input_foreman.clean_unlisted_from_silo ();
	Ok ()

let backup_path (filename : string) : string = filename ^ ".bak"

let upgrade ?from ?(to_ = Schema.Version.current) ?(dry_run = false) () : (unit, error) result =
	let (let*) = Result.bind in
	let working_dir = Working_directory.get () in
	let lockfile_path = Eio.Path.(working_dir / Lockfile.filename) in
	let manifest_path = Eio.Path.(working_dir / Manifest.filename) in
	let manifest_content =
		Eio.Path.with_open_in manifest_path @@ fun flow ->
		let buf = Eio.Buf_read.of_flow flow ~max_size: max_int in
		Eio.Buf_read.take_all buf
	in
	Logs.info (fun m -> m "Current schema version: %a" Schema.Version.pp Schema.Version.current);
	let* manifest_version =
		let open KDL.L in
		let open KDL.Valid in
		match Eio.Path.with_open_in manifest_path KDL.of_flow with
		| Error _ -> Error (`Manifest `Not_set_up)
		| Ok kdl_doc ->
			match ll @@ kdl_doc.@(node "version" // arg 0 // string_value) with
			| Error _ -> Error (`Upgrade "Manifest version not found")
			| Ok version_str ->
				match Schema.Version.of_string version_str with
				| Some v -> Ok v
				| None -> Error (`Upgrade (