-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprisma.nix
383 lines (374 loc) · 13.2 KB
/
prisma.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
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
{
nixpkgs ? null,
# if both are set, prefer pkgs over nixpkgs
pkgs ? nixpkgs,
opensslVersion ? "3.0.x", # can be 3.0.x, 1.1.x or 1.0.x
openssl ? pkgs.openssl, # the openssl package to use
introspection-engine-hash ? null,
migration-engine-hash ? null,
prisma-fmt-hash,
query-engine-hash,
libquery-engine-hash,
schema-engine-hash ? null,
binaryTargetBySystem ? {
x86_64-linux = "debian";
aarch64-linux = "linux-arm64";
x86_64-darwin = "darwin";
aarch64-darwin = "darwin-arm64";
},
}:
let
inherit (pkgs) lib;
lines = s: lib.strings.splitString "\n" s;
# example:
# splitMultiple ["|" "," "-"] "a-|b,c-d"
# -> ["a" "" "b" "c" "d"]
splitMultiple = delims: s: _splitMultiple delims [ s ];
# example:
# _splitMultiple ["|" "," "-"] ["a-|b,c-d"]
# -> ["a" "" "b" "c" "d"]
_splitMultiple =
delims: list:
if builtins.length delims == 0 then
list
else
let
splitStr = map (str: lib.strings.splitString (builtins.elemAt delims 0) str) list;
in
_splitMultiple (lib.drop 1 delims) (lib.lists.concatLists splitStr);
splitMultipleAndFilterEmpty = delims: s: builtins.filter (str: str != "") (splitMultiple delims s);
# example:
# a.b123c.d.e12345
# => e12345
afterLastDot = text: lib.lists.last (lib.strings.splitString "." text);
readYAML = pkgs.callPackage ./lib/readYAML.nix { };
# polyfill: the function in nixpkgs is implemented on Dec 6, 2024. replace this with one from pkgs.lib after 24.11 reaches EOL.
concatMapAttrsStringSep =
let
inherit (pkgs) lib;
in
sep: f: attrs:
lib.concatStringsSep sep (lib.attrValues (lib.mapAttrs f attrs));
in
pkgs.lib.warnIf (nixpkgs != null)
''
`nixpkgs` argument in nix-prisma-utils is deprecated. please replace it with `pkgs`.
examples:
if your code has `inherit nixpkgs;`, replace it with `pkgs = nixpkgs;`.
if your code has `nixpkgs = pkgs;`, replace it with `pkgs = pkgs;` or `inherit pkgs;`.
''
rec {
fromCommit =
commit:
if builtins.stringLength commit != 40 then
throw "nvalid commit: got ${commit}"
else
let
hostname = "binaries.prisma.sh";
channel = "all_commits";
binaryTarget = binaryTargetBySystem.${pkgs.system};
isDarwin = pkgs.lib.strings.hasPrefix "darwin" binaryTarget;
target = if isDarwin then binaryTarget else "${binaryTarget}-openssl-${opensslVersion}";
baseUrl = "https://${hostname}/${channel}";
files =
[
{
name = "prisma-fmt";
hash = prisma-fmt-hash;
path = "bin/prisma-fmt";
variable = "PRISMA_FMT_BINARY";
}
{
name = "query-engine";
hash = query-engine-hash;
path = "bin/query-engine";
variable = "PRISMA_QUERY_ENGINE_BINARY";
}
{
name = if isDarwin then "libquery_engine.dylib.node" else "libquery_engine.so.node";
hash = libquery-engine-hash;
path = "lib/libquery_engine.node";
variable = "PRISMA_QUERY_ENGINE_LIBRARY";
}
]
++ (
if introspection-engine-hash == null then
[ ]
else
[
{
name = "introspection-engine";
hash = introspection-engine-hash;
path = "bin/introspection-engine";
variable = "PRISMA_INTROSPECTION_ENGINE_BINARY";
}
]
)
++ (
if migration-engine-hash == null then
[ ]
else
[
{
name = "migration-engine";
hash = migration-engine-hash;
path = "bin/migration-engine";
variable = "PRISMA_MIGRATION_ENGINE_BINARY";
}
]
)
++ (
if schema-engine-hash == null then
[ ]
else
[
{
name = "schema-engine";
hash = schema-engine-hash;
path = "bin/schema-engine";
variable = "PRISMA_SCHEMA_ENGINE_BINARY";
}
]
);
downloadedFiles = builtins.map (
file:
file
// {
file = pkgs.fetchurl {
name = "${baseUrl}/${commit}/${target}/${file.name}.gz";
url = "${baseUrl}/${commit}/${target}/${file.name}.gz";
hash = file.hash;
};
}
) files;
unzipCommands = builtins.map (file: "gunzip -c ${file.file} > $out/${file.path}") downloadedFiles;
mkEnv =
package:
builtins.listToAttrs (
builtins.map (file: {
name = file.variable;
value = "${package}/${file.path}";
}) files
);
/**
This function converts attrset to bash export style.
return value contains leading and trailing newlines.
# Example
```nix
toExportStyle { foo = "bar"; baz = "abc"; }
=>
''
export foo="bar"
export baz="abc"
''
```
# Type
toExportStyle :: Attrset<String> -> String
*/
toExportStyle =
attrset:
"\n" + (concatMapAttrsStringSep "\n" (name: value: "export ${name}=\"${value}\"") attrset) + "\n";
in
rec {
package = pkgs.stdenv.mkDerivation {
pname = "prisma-bin";
version = commit;
nativeBuildInputs = [
pkgs.zlib
openssl
pkgs.stdenv.cc.cc.lib
] ++ pkgs.lib.optionals (!isDarwin) [ pkgs.autoPatchelfHook ];
phases = [
"buildPhase"
"postFixupHooks"
];
buildPhase = ''
mkdir -p $out/bin
mkdir -p $out/lib
${pkgs.lib.concatStringsSep "\n" unzipCommands}
chmod +x $out/bin/*
'';
};
env = mkEnv package;
shellHook = toExportStyle env;
};
fromPnpmLock =
path:
let
textAfter = keyword: text: builtins.elemAt (builtins.split keyword text) 1;
textBefore = keyword: text: builtins.elemAt (builtins.split keyword text) 0;
parsePnpmLockVersion =
pnpmLock:
if pkgs.lib.strings.hasPrefix "lockfileVersion: 5" pnpmLock then
"5"
else if pkgs.lib.strings.hasPrefix "lockfileVersion: '6" pnpmLock then
"6"
else
"9";
pnpmLockParsers = {
# example line:
# /@prisma/engines-version/5.1.1-1.6a3747c37ff169c90047725a05a6ef02e32ac97e:
"5" =
pnpmLock:
let
version = builtins.elemAt (builtins.split ":" (builtins.elemAt (builtins.split ("@prisma/engines-version/") pnpmLock) 2)) 0;
in
pkgs.lib.lists.last (pkgs.lib.strings.splitString "." version);
# example line:
# /@prisma/[email protected]:
"6" =
pnpmLock:
let
version = builtins.elemAt (builtins.split ":" (builtins.elemAt (builtins.split ("@prisma/engines-version@") pnpmLock) 2)) 0;
in
pkgs.lib.lists.last (pkgs.lib.strings.splitString "." version);
# exmple line:
# '@prisma/engines-version@5.15.0-29.12e25d8d06f6ea5a0252864dd9a03b1bb51f3022':
"9" =
pnpmLock:
let
version = builtins.elemAt (builtins.split "'" (builtins.elemAt (builtins.split ("@prisma/engines-version@") pnpmLock) 2)) 0;
in
pkgs.lib.lists.last (pkgs.lib.strings.splitString "." version);
};
pnpmLock = builtins.readFile path;
pnpmLockVersion = parsePnpmLockVersion pnpmLock;
pnpmLockParser = pnpmLockParsers.${pnpmLockVersion};
commit = pnpmLockParser pnpmLock;
in
fromCommit commit;
fromNpmLock =
path:
let
packageLock = builtins.fromJSON (builtins.readFile path);
version =
if builtins.hasAttr "dependencies" packageLock then
packageLock.dependencies.${"@prisma/engines-version"}.version
else
packageLock.packages.${"node_modules/@prisma/engines-version"}.version;
commit = pkgs.lib.lists.last (pkgs.lib.strings.splitString "." version);
in
fromCommit commit;
fromYarnLock =
path:
let
# find this line from yarn.lock:
# "@prisma/engines-version@npm:6.3.0-17.acc0b9dd43eb689cbd20c9470515d719db10d0b0":
yarnLockParser1 =
file:
let
versionLine =
lib.lists.findFirst
(line: builtins.length (lib.strings.splitString "@prisma/engines-version" line) >= 2)
# else
(throw ''
nix-prisma-utils/yarnLockParser1: package @prisma/engines-version not found in lockfile ${path} .
please make sure you have installed `@prisma/client`.
if you have already installed `@prisma/client` and still see this, please report this to nix-prisma-utils.
'')
(lines file);
# "@prisma/engines-version@npm:6.3.0-17.acc0b9dd43eb689cbd20c9470515d719db10d0b0":
# -> ["@prisma/engines-version@npm" "6" "3" "0-17" "acc0b9dd43eb689cbd20c9470515d719db10d0b0"]
# -> acc0b9dd43eb689cbd20c9470515d719db10d0b0
version = lib.lists.last (
splitMultipleAndFilterEmpty [
"\""
":"
"."
] versionLine
);
in
version;
isYarnLockV1 =
file:
lib.lists.any (line: lib.strings.trim line == "# yarn lockfile v1") (
lib.strings.splitString "\n" file
);
# example line:
# "@prisma/engines-version@6.3.0-17.acc0b9dd43eb689cbd20c9470515d719db10d0b0":
yarnV1LockParser = yarnLockParser1;
# example line:
# "@prisma/engines-version@npm:6.3.0-17.acc0b9dd43eb689cbd20c9470515d719db10d0b0":
yarnBerryLockParsers = {
"8" = yarnLockParser1;
};
lockfile = builtins.readFile path;
parse =
if isYarnLockV1 lockfile then
yarnV1LockParser
else
let
lockfileVersion = builtins.toString (readYAML path).__metadata.version;
in
yarnBerryLockParsers.${lockfileVersion} or (throw ''
nix-prisma-utils: unknown lockfile version ${lockfileVersion}.
please report this to nix-prisma-utils with your lockfile.
'');
in
fromCommit (parse lockfile);
fromBunLock =
path:
let
# HACK: nix doesn't support JSONC parsing, so currently doing
# 1. remove whitespace and newline
# 2. replace ",}" with "}"
# 3. replace ",]" with "]"
# to support JSON with trailing comma.
# Keep in mind that this removes all whitespaces / tab / newline in the key / value
# and doesn't support comments.
fromJSONWithTrailingComma =
jsonc:
builtins.fromJSON (
builtins.replaceStrings
[
",}"
",]"
]
[
"}"
"]"
]
(
builtins.replaceStrings
[
" "
"\t"
"\n"
]
[
""
""
""
]
jsonc
)
);
bunLockParsers = {
# example:
# nu> open bun.lock | from json | get packages.@prisma/engines-version.0
# @prisma/[email protected]
"0" = bunLockParsers."1";
"1" =
lock:
afterLastDot (
builtins.elemAt (lock."packages"."@prisma/engines-version" or (throw ''
nix-prisma-utils: lockfile parsing error: package @prisma/engines-version not found.
please make sure that you have @prisma/client installed.
'')
) 0
);
};
lockfile = fromJSONWithTrailingComma (
assert builtins.typeOf path == "path";
builtins.readFile path
);
lockfileVersion = builtins.toString lockfile."lockfileVersion";
parse =
bunLockParsers.${lockfileVersion} or (throw ''
nix-prisma-utils: Unsupported lockfile version: ${lockfileVersion}
nix-prisma-utils currently supports bun.lock version of 0 and 1.
'');
commit = parse lockfile;
in
fromCommit commit;
}