summaryrefslogtreecommitdiff
path: root/lib/lock_loader.ml
blob: c1648a0bd1d855376c526f29aa5f067d2abfee3c (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
(*─────────────────────────────────────────────────────────────────────────────┐
│ SPDX-FileCopyrightText: 2025–2026 toastal <https://toast.al/contact/>        │
│ SPDX-License-Identifier: LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception │
└─────────────────────────────────────────────────────────────────────────────*)
(* Loads the lockfile for Nix usage broadly *)
let filename = "default.nix"

module Features = struct
	type t = int [@@deriving show]

	let empty = 0

	(* only build features needed *)
	let file = 1 lsl 0
	let archive = 1 lsl 1
	let git = 1 lsl 2
	let darcs = 1 lsl 3
	let pijul = 1 lsl 4

	let [@inline]has mask v = (mask land v) <> 0
	let [@inline]add mask v = mask lor v
	let [@inline]drop mask v = mask land (lnot v)

	let value = ref empty

	let add_input (input : Input.t) : t -> t =
		match input.kind with
		| `File _ -> add file
		| `Archive _ -> add archive
		| `Git _ -> add git
		| `Darcs _ -> add darcs
		| `Pijul _ -> add pijul

	let drop_input (input : Input.t) : t -> t =
		match input.kind with
		| `File _ -> drop file
		| `Archive _ -> drop archive
		| `Git _ -> drop git
		| `Darcs _ -> drop darcs
		| `Pijul _ -> drop pijul
end

open Fmt

let pp_banner (ppf : Format.formatter) =
	let maker = "toastal"
	and year_range =
		let first = 2025 in
		(* replaced by Nix *)
		match int_of_string_opt "@release_year@" with
		| Some last when last > first -> Fmt.str "%a–%a" Fmt.int first Fmt.int last
		| _ -> Fmt.str "%a" Fmt.int first
	and margin = Format.pp_get_margin ppf ()
	in
	let hr =
		(*──────────────────────────────*)
		let uchar = Uchar.of_int 0x2500
		and buf = Buffer.create (margin * 3)
		in
		for _ = 1 to margin do
			Buffer.add_utf_8_uchar buf uchar
		done;
		Buffer.contents buf
	in
	pf ppf "/*@.";
	pf ppf "SPDX-FileCopyrightText: %a %a@." Fmt.string year_range Fmt.string maker;
	pf ppf "SPDX-License-Identifier: ISC@.";
	pf ppf "@.";
	pf ppf "@[<hov>Permission@ to@ use,@ copy,@ modify,@ and/or@ distribute@ ";
	pf ppf "this@ software@ for@ any@ purpose@ with@ or@ without@ fee@ is@ ";
	pf ppf "hereby@ granted,@ provided@ that@ the@ above@ copyright@ notice@ &@ ";
	pf ppf "this@ permission@ notice@ appear@ in@ all@ copies.@]@.";
	pf ppf "@.";
	pf ppf "@[<hov>THE@ SOFTWARE@ IS@ PROVIDED@ “AS@ IS”@ &@ ISC@ DISCLAIMS@ ";
	pf ppf "ALL@ WARRANTIES@ WITH@ REGARD@ TO@ THIS@ SOFTWARE@ INCLUDING@ ALL@ ";
	pf ppf "IMPLIED@ WARRANTIES@ OF@ MERCHANTABILITY@ &@ FITNESS.@ IN@ NO@ ";
	pf ppf "EVENT@ SHALL@ ISC@ BE@ LIABLE@ FOR@ ANY@ SPECIAL,@ DIRECT,@ ";
	pf ppf "INDIRECT,@ OR@ CONSEQUENTIAL@ DAMAGES@ OR@ ANY@ DAMAGES@ WHATSOEVER@ ";
	pf ppf "RESULTING@ FROM@ LOSS@ OF@ USE,@ DATA@ OR@ PROFITS,@ WHETHER@ IN@ ";
	pf ppf "AN@ ACTION@ OF@ CONTRACT,@ NEGLIGENCE@ OR@ OTHER@ TORTIOUS@ ACTION,@ ";
	pf ppf "ARISING@ OUT@ OF@ OR@ IN@ CONNECTION@ WITH@ THE@ USE@ OR@ ";
	pf ppf "PERFORMANCE@ OF@ THIS@ SOFTWARE.@]@.";
	pf ppf "@.";
	pf ppf "%a@." Fmt.string hr;
	pf ppf "┏┓╻+╻ ╱┏┳┓┏┓┏┳┓┏┓╻@.";
	pf ppf "┃┃┃┃┗━┓╹┃╹┣┫┃┃┃┣┫┃   This file was generated by Nixtamal.@.";
	pf ppf "╹┗┛╹╱ ╹ ╹ ╹╹╹ ╹╹╹┗┛  Do not edit as it will be overwritten.@.";
	pf ppf "%a@." Fmt.string hr;
	pf ppf "*/@."

let pp_nix_named_arg (ppf : Format.formatter) ((name, default): (string * string option)) =
	pf ppf "%a%a" string name (option (fun ppf v -> pf ppf " ? %s" v)) default

let pp_nix_named_args fmt args =
	let pp_args = list ~sep: (any ",@;") pp_nix_named_arg in
	let break = Format.pp_print_custom_break ~fits: ("", 0, "") ~breaks: (",", 0, "") in
	pf fmt "@[<hv>{@;<0 1>@[@[<hv>%a@]@]%t@]}:" pp_args args break

let pp_cfg (ppf : Format.formatter) =
	pp_nix_named_args ppf [
		("system", Some "builtins.currentSystem");
		("nixpkgs", Some "null");
		("bootstrap-nixpkgs-lock-name", Some "null");
	]

(* TODO: consider *not* doing manually as this is ugly AF, but would probably
	involve building a Nix AST to do properly *)
let pp_body ~version (ppf : Format.formatter) () =
	let feats = !Features.value in
	pf ppf {|let lock = builtins.fromJSON (builtins.readFile ./lock.json); in@.|};
	pf ppf {|assert (lock.v == "%a");@.|} string version;
	pf ppf {|let@.|};
	pf ppf {|	try-fetch = name: fetcher:@.|};
	pf ppf {|		let@.|};
	pf ppf {|			try-fetch' = failed-urls: url: urls:@.|};
	pf ppf {|				let result = builtins.tryEval (fetcher url); in@.|};
	pf ppf {|				if result.success then@.|};
	pf ppf {|					result.value@.|};
	pf ppf {|				else@.|};
	pf ppf {|					let failed-urls' = [ url ] ++ failed-urls; in@.|};
	pf ppf {|					if builtins.length urls <= 0 then@.|};
	pf ppf {|						let fus = builtins.concatStringsSep " " failed-urls'; in@.|};
	pf ppf {|						throw "Input 「${name}」fetchable @@ [ ${fus} ]"@.|};
	pf ppf {|					else@.|};
	pf ppf {|						try-fetch' failed-urls' (builtins.head urls) (builtins.tail urls);@.|};
	pf ppf {|		in@.|};
	pf ppf {|		try-fetch' [ ];@.|};
	pf ppf {|@.|};
	if Features.has Features.file feats then
		begin
			pf ppf {|	builtin-fetch-url = {name, kind, hash}:@.|};
			pf ppf {|		try-fetch name (url:@.|};
			pf ppf {|			builtins.fetchurl {@.|};
			pf ppf {|				inherit url name;@.|};
			pf ppf {|				${hash.al} = hash.vl;@.|};
			pf ppf {|			}@.|};
			pf ppf {|		) kind.ur kind.ms;@.|};
			pf ppf {|@.|};
		end;
	if Features.has Features.archive feats then
		begin
			pf ppf {|	builtin-fetch-tarball = {name, kind, hash}:@.|};
			pf ppf {|		try-fetch name (url:@.|};
			pf ppf {|			builtins.fetchTarball {@.|};
			pf ppf {|				inherit url;@.|};
			pf ppf {|				${hash.al} = hash.vl;@.|};
			pf ppf {|			}@.|};
			pf ppf {|		) kind.ur kind.ms;@.|};
			pf ppf {|@.|}
		end;
	if Features.has Features.git feats then
		begin
			pf ppf {|	builtin-fetch-git = {name, kind}:@.|};
			pf ppf {|		try-fetch name (url:@.|};
			pf ppf {|			builtins.fetchGit {@.|};
			pf ppf {|				inherit url;@.|};
			pf ppf {|				rev = kind.lr;@.|};
			pf ppf {|				submodules = kind.sm;@.|};
			pf ppf {|				lfs = kind.lf;@.|};
			pf ppf {|				shallow = true;@.|};
			pf ppf {|			}@.|};
			pf ppf {|		) kind.rp kind.ms;@.|};
			pf ppf {|@.|}
		end;
	pf ppf {|	builtin-to-input = name: input:@.|};
	pf ppf {|		let k = builtins.head input.kd; in@.|};
	pf ppf {|		|};
	let builtin_fetch_ifs = Dynarray.create () in
	if Features.has Features.file feats then
		Dynarray.add_last builtin_fetch_ifs (
			Fmt.str "@[<v 1>%a@]" (list ~sep: (any "@.") string) [
				{|if k == 0 then|};
				{|			builtin-fetch-url {|};
				{|				inherit name;|};
				{|				kind = builtins.elemAt input.kd 1;|};
				{|				hash = input.ha;|};
				{|			}|};
			]
		);
	if Features.has Features.archive feats then
		Dynarray.add_last builtin_fetch_ifs (
			Fmt.str "@[<v 1>%a@]" (list ~sep: (any "@.") string) [
				{|if k == 1 then|};
				{|			builtin-fetch-tarball {|};
				{|				inherit name;|};
				{|				kind = builtins.elemAt input.kd 1;|};
				{|				hash = input.ha;|};
				{|			}|};
			]
		);
	if Features.has Features.git feats then
		Dynarray.add_last builtin_fetch_ifs (
			Fmt.str "@[<v 1>%a@]" (list ~sep: (any "@.") string) [
				{|if k == 2 then|};
				{|			builtin-fetch-git {|};
				{|				inherit name;|};
				{|				kind = builtins.elemAt input.kd 1;|};
				{|			}|};
			]
		);
	pf ppf "@[%a@]@." (list ~sep: (any "@.		else ") string) (Dynarray.to_list builtin_fetch_ifs);
	Dynarray.clear builtin_fetch_ifs;
	pf ppf {|		else@.|};
	pf ppf {|			throw "Unsupported input kind “${builtins.toString k}”.";@.|};
	pf ppf {|@.|};
	pf ppf {|	nixpkgs' = if builtins.isNull nixpkgs then@.|};
	pf ppf {|		builtin-to-input "nixpkgs-for-nixtamal" (@.|};
	pf ppf {|			if builtins.isString bootstrap-nixpkgs-lock-name then@.|};
	pf ppf {|				lock.i.${bootstrap-nixpkgs-lock-name}@.|};
	pf ppf {|			else@.|};
	pf ppf {|				lock.i.nixpkgs-nixtamal or lock.i.nixpkgs@.|};
	pf ppf {|		)@.|};
	pf ppf {|	else@.|};
	pf ppf {|		nixpkgs;@.|};
	pf ppf {|@.|};
	pf ppf {|	pkgs = import nixpkgs' {inherit system;};@.|};
	pf ppf {|@.|};
	pf ppf {|	inherit (pkgs) lib;@.|};
	pf ppf {|@.|};
	if Features.has Features.file feats then
		begin
			pf ppf {|	fetch-url = {name, kind, hash}: pkgs.fetchurl {@.|};
			pf ppf {|		inherit name;@.|};
			pf ppf {|		url = kind.ur;@.|};
			pf ppf {|		${hash.al}