-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltins.ts
594 lines (469 loc) · 12.9 KB
/
builtins.ts
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
import { err, errType, errTypes, highlighted } from "./errors";
import { abortError } from "./errors/abort";
import { otherError } from "./errors/other";
import { typeMismatchError } from "./errors/typeError";
import {
Attrset,
EvalCtx,
FALSE,
Lambda,
NULL,
NixBool,
NixFloat,
NixInt,
NixList,
NixNull,
NixString,
NixType,
NixTypeClass,
Path,
TRUE,
nixBoolFromJs,
} from "./lib";
import { dirOf, isAbsolutePath, normalizePath } from "./utils";
type BuiltinsRecord = Record<string, (param: NixType) => NixType>;
function builtinBasicTypeMismatchError(
fnName: string,
got: NixType,
expects: NixTypeClass | NixTypeClass[],
) {
if (!Array.isArray(expects)) {
expects = [expects];
}
return typeMismatchError(
got,
expects,
err`${fnName} expects ${errTypes(...expects)}, got ${errType(got)}.`,
);
}
export function getBuiltins() {
// Builtins are sorted by the order they appear in the Nix manual
// https://nixos.org/manual/nix/stable/language/builtins.html
const builtins: BuiltinsRecord = {
derivation: (arg) => {
throw new Error("unimplemented");
},
abort: (message) => {
throw abortError(message.asString());
},
add: (lhs) => {
return new Lambda((rhs) => {
let lhsStrict = lhs.toStrict();
if (!(lhsStrict instanceof NixInt || lhsStrict instanceof NixFloat)) {
let expected = [NixInt, NixFloat];
throw builtinBasicTypeMismatchError("add", lhsStrict, expected);
}
let rhsStrict = rhs.toStrict();
if (!(rhsStrict instanceof NixInt || rhsStrict instanceof NixFloat)) {
let expected = [NixInt, NixFloat];
throw builtinBasicTypeMismatchError("add", rhsStrict, expected);
}
return lhsStrict.add(rhsStrict);
});
},
addDrvOutputDependencies: (arg) => {
throw new Error("unimplemented");
},
all: (pred) => {
const lambdaStrict = pred.toStrict();
if (!(lambdaStrict instanceof Lambda)) {
throw builtinBasicTypeMismatchError("all", lambdaStrict, Lambda);
}
return new Lambda((list) => {
const listStrict = list.toStrict();
if (!(listStrict instanceof NixList)) {
throw builtinBasicTypeMismatchError("all", listStrict, NixList);
}
for (const element of listStrict.values) {
const result = lambdaStrict.apply(element);
if (!result.asBoolean()) {
return FALSE;
}
}
return TRUE;
});
},
any: (pred) => {
const lambdaStrict = pred.toStrict();
if (!(lambdaStrict instanceof Lambda)) {
throw builtinBasicTypeMismatchError("any", lambdaStrict, Lambda);
}
return new Lambda((list) => {
const listStrict = list.toStrict();
if (!(listStrict instanceof NixList)) {
throw builtinBasicTypeMismatchError("any", listStrict, NixList);
}
for (const element of listStrict.values) {
const result = lambdaStrict.apply(element);
if (result.asBoolean()) {
return TRUE;
}
}
return FALSE;
});
},
attrNames: (attrset) => {
const attrsetStrict = attrset.toStrict();
if (!(attrsetStrict instanceof Attrset)) {
throw builtinBasicTypeMismatchError(
"attrNames",
attrsetStrict,
Attrset,
);
}
const keys = Array.from(attrsetStrict.keys());
keys.sort();
return new NixList(keys.map((key) => new NixString(key)));
},
attrValues: (attrset) => {
const attrsetStrict = attrset.toStrict();
if (!(attrsetStrict instanceof Attrset)) {
throw builtinBasicTypeMismatchError(
"attrValues",
attrsetStrict,
Attrset,
);
}
const keys = Array.from(attrsetStrict.keys());
keys.sort();
return new NixList(
keys.map((key) => attrset.select([new NixString(key)], NULL)),
);
},
baseNameOf: (path) => {
// Can take a string or path
const pathStrict = path.toStrict();
if (!(pathStrict instanceof Path || pathStrict instanceof NixString)) {
const expected = [Path, NixString];
throw builtinBasicTypeMismatchError("baseNameOf", pathStrict, expected);
}
let pathValue = pathStrict.toJs();
if (pathValue.endsWith("/")) {
pathValue = pathValue.slice(0, -1);
}
const parts = pathValue.split("/");
return new NixString(parts[parts.length - 1]);
},
bitAnd: (arg) => {
throw new Error("unimplemented");
},
bitOr: (arg) => {
throw new Error("unimplemented");
},
bitXor: (arg) => {
throw new Error("unimplemented");
},
break: (arg) => {
throw new Error("unimplemented");
},
catAttrs: (arg) => {
throw new Error("unimplemented");
},
ceil: (arg) => {
throw new Error("unimplemented");
},
compareVersions: (arg) => {
throw new Error("unimplemented");
},
concatLists: (arg) => {
throw new Error("unimplemented");
},
concatMap: (arg) => {
throw new Error("unimplemented");
},
concatStringsSep: (arg) => {
throw new Error("unimplemented");
},
convertHash: (arg) => {
throw new Error("unimplemented");
},
deepSeq: (arg) => {
throw new Error("unimplemented");
},
dirOf: (arg) => {
throw new Error("unimplemented");
},
div: (arg) => {
throw new Error("unimplemented");
},
elem: (arg) => {
throw new Error("unimplemented");
},
elemAt: (arg) => {
throw new Error("unimplemented");
},
fetchClosure: (arg) => {
throw new Error("unimplemented");
},
fetchGit: (arg) => {
throw new Error("unimplemented");
},
fetchTarball: (arg) => {
throw new Error("unimplemented");
},
fetchTree: (arg) => {
throw new Error("unimplemented");
},
fetchurl: (arg) => {
throw new Error("unimplemented");
},
filter: (arg) => {
throw new Error("unimplemented");
},
filterSource: (arg) => {
throw new Error("unimplemented");
},
findFile: (arg) => {
throw new Error("unimplemented");
},
flakeRefToString: (arg) => {
throw new Error("unimplemented");
},
floor: (arg) => {
throw new Error("unimplemented");
},
foldl: (arg) => {
throw new Error("unimplemented");
},
fromJSON: (arg) => {
throw new Error("unimplemented");
},
fromTOML: (arg) => {
throw new Error("unimplemented");
},
functionArgs: (arg) => {
throw new Error("unimplemented");
},
genList: (arg) => {
throw new Error("unimplemented");
},
genericClosure: (arg) => {
throw new Error("unimplemented");
},
getAttr: (arg) => {
throw new Error("unimplemented");
},
getContext: (arg) => {
throw new Error("unimplemented");
},
getEnv: (arg) => {
throw new Error("unimplemented");
},
getFlake: (arg) => {
throw new Error("unimplemented");
},
groupBy: (arg) => {
throw new Error("unimplemented");
},
hasAttr: (arg) => {
throw new Error("unimplemented");
},
hasContext: (arg) => {
throw new Error("unimplemented");
},
hashFile: (arg) => {
throw new Error("unimplemented");
},
hashString: (arg) => {
throw new Error("unimplemented");
},
head: (list) => {
const listStrict = list.toStrict();
if (!(listStrict instanceof NixList)) {
throw typeMismatchError(
listStrict,
NixList,
err`Cannot apply the 'head' function on '${errType(listStrict)}', expected ${errType(NixList)}.`,
);
}
if (listStrict.values.length === 0) {
throw otherError(
"Cannot fetch the first element in an empty list.",
"builtins-head-on-empty-list",
);
}
return listStrict.values[0];
},
import: (path) => {
const pathStrict = path.toStrict();
if (!(pathStrict instanceof Path || pathStrict instanceof NixString)) {
const expected = [Path, NixString];
throw builtinBasicTypeMismatchError("import", pathStrict, expected);
}
let pathValue = "";
if (pathStrict instanceof NixString) {
pathValue = normalizePath(pathStrict.toJs());
} else if (pathStrict instanceof Path) {
pathValue = pathStrict.toJs();
}
// Check if it's an absolute path. Relative paths are not allowed.
// Path data types are always automatically absolute.
if (!isAbsolutePath(pathValue)) {
throw otherError(
err`string ${highlighted(JSON.stringify(pathValue))} doesn't represent an absolute path. Only absolute paths are allowed for imports.`,
"builtins-import-non-absolute-path",
);
}
const resultingFn = importNixModule(pathValue);
const newCtx = new EvalCtx(dirOf(pathValue));
return resultingFn(newCtx);
},
intersectAttrs: (arg) => {
throw new Error("unimplemented");
},
isAttrs: (arg) => {
return nixBoolFromJs(arg instanceof Attrset);
},
isBool: (arg) => {
return nixBoolFromJs(arg instanceof NixBool);
},
isFloat: (arg) => {
return nixBoolFromJs(arg instanceof NixFloat);
},
isFunction: (arg) => {
return nixBoolFromJs(arg instanceof Lambda);
},
isInt: (arg) => {
return nixBoolFromJs(arg instanceof NixInt);
},
isList: (arg) => {
return nixBoolFromJs(arg instanceof NixList);
},
isNull: (arg) => {
return nixBoolFromJs(arg instanceof NixNull);
},
isPath: (arg) => {
return nixBoolFromJs(arg instanceof Path);
},
isString: (arg) => {
return nixBoolFromJs(arg instanceof NixString);
},
length: (arg) => {
throw new Error("unimplemented");
},
lessThan: (arg) => {
throw new Error("unimplemented");
},
listToAttrs: (arg) => {
throw new Error("unimplemented");
},
map: (arg) => {
throw new Error("unimplemented");
},
mapAttrs: (arg) => {
throw new Error("unimplemented");
},
match: (arg) => {
throw new Error("unimplemented");
},
mul: (arg) => {
throw new Error("unimplemented");
},
outputOf: (arg) => {
throw new Error("unimplemented");
},
parseDrvName: (arg) => {
throw new Error("unimplemented");
},
parseFlakeRef: (arg) => {
throw new Error("unimplemented");
},
partition: (arg) => {
throw new Error("unimplemented");
},
path: (arg) => {
throw new Error("unimplemented");
},
pathExists: (arg) => {
throw new Error("unimplemented");
},
placeholder: (arg) => {
throw new Error("unimplemented");
},
readDir: (arg) => {
throw new Error("unimplemented");
},
readFile: (arg) => {
throw new Error("unimplemented");
},
readFileType: (arg) => {
throw new Error("unimplemented");
},
removeAttrs: (arg) => {
throw new Error("unimplemented");
},
replaceStrings: (arg) => {
throw new Error("unimplemented");
},
seq: (arg) => {
throw new Error("unimplemented");
},
sort: (arg) => {
throw new Error("unimplemented");
},
split: (arg) => {
throw new Error("unimplemented");
},
splitVersion: (arg) => {
throw new Error("unimplemented");
},
storePath: (arg) => {
throw new Error("unimplemented");
},
stringLength: (arg) => {
throw new Error("unimplemented");
},
sub: (arg) => {
throw new Error("unimplemented");
},
substring: (arg) => {
throw new Error("unimplemented");
},
tail: (arg) => {
throw new Error("unimplemented");
},
throw: (arg) => {
throw new Error("unimplemented");
},
toFile: (arg) => {
throw new Error("unimplemented");
},
toJSON: (arg) => {
throw new Error("unimplemented");
},
toPath: (arg) => {
throw new Error("unimplemented");
},
toString: (arg: NixType) => {
if (arg instanceof NixString) {
return arg;
} else if (arg instanceof Path) {
return new NixString(arg.path);
}
// TODO: Expand on this
throw new Error("unimplemented");
},
toXML: (arg) => {
throw new Error("unimplemented");
},
trace: (arg) => {
throw new Error("unimplemented");
},
traceVerbose: (arg) => {
throw new Error("unimplemented");
},
tryEval: (arg) => {
throw new Error("unimplemented");
},
typeOf: (arg) => {
throw new Error("unimplemented");
},
unsafeDiscardOutputDependency: (arg) => {
throw new Error("unimplemented");
},
zipAttrsWith: (arg) => {
throw new Error("unimplemented");
},
};
return builtins;
}