-
Notifications
You must be signed in to change notification settings - Fork 6
/
prisma.nix
190 lines (188 loc) · 5.89 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
{
nixpkgs,
opensslVersion ? "3.0.x", # can be 3.0.x, 1.1.x or 1.0.x
openssl ? nixpkgs.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";
},
}:
rec {
fromCommit =
commit:
let
hostname = "binaries.prisma.sh";
channel = "all_commits";
binaryTarget = binaryTargetBySystem.${nixpkgs.system};
target = "${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 = "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 = nixpkgs.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;
exportCommands =
package: builtins.map (file: "export ${file.variable}=${package}/${file.path}") files;
in
rec {
package = nixpkgs.stdenv.mkDerivation {
pname = "prisma-bin";
version = commit;
nativeBuildInputs = [
nixpkgs.autoPatchelfHook
nixpkgs.zlib
openssl
nixpkgs.stdenv.cc.cc.lib
];
phases = [
"buildPhase"
"postFixupHooks"
];
buildPhase = ''
mkdir -p $out/bin
mkdir -p $out/lib
${nixpkgs.lib.concatStringsSep "\n" unzipCommands}
chmod +x $out/bin/*
'';
};
shellHook = nixpkgs.lib.concatStringsSep "\n" (exportCommands package);
};
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 nixpkgs.lib.strings.hasPrefix "lockfileVersion: 5" pnpmLock then
"5"
else if nixpkgs.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
nixpkgs.lib.lists.last (nixpkgs.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
nixpkgs.lib.lists.last (nixpkgs.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
nixpkgs.lib.lists.last (nixpkgs.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 = nixpkgs.lib.lists.last (nixpkgs.lib.strings.splitString "." version);
in
fromCommit commit;
}