Skip to content

Commit 1e5e45c

Browse files
authored
Merge pull request #22 from aster-void/feat/new-fetcher
feat: new fetcher with simplified API 🎉
2 parents 0d4f9a4 + 0dbbdee commit 1e5e45c

File tree

8 files changed

+655
-401
lines changed

8 files changed

+655
-401
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
22
.direnv
3+
result
4+
result-*

flake.nix

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
formatter = treefmt.config.build.wrapper;
4242
checks =
4343
(pkgs.callPackages ./tests.nix {
44+
fetcherMode = "new";
45+
inherit
46+
pkgs
47+
prisma-factory
48+
yarn-v1
49+
yarn-berry
50+
;
51+
})
52+
// (pkgs.callPackages ./tests.nix {
53+
fetcherMode = "legacy";
4454
inherit
4555
pkgs
4656
prisma-factory
@@ -51,18 +61,19 @@
5161
// {
5262
format = treefmt.config.build.check self;
5363
};
64+
packages.default =
65+
(prisma-factory {
66+
inherit pkgs;
67+
hash = "sha256-R9PG286KQTbzF0r/PPcShUkMiYam2prRh/JICjmhCZA=";
68+
_commit = "6a3747c37ff169c90047725a05a6ef02e32ac97e";
69+
}).package;
5470
devShells.default =
5571
let
56-
prisma = (
57-
(prisma-factory {
58-
inherit pkgs;
59-
prisma-fmt-hash = "sha256-4zsJv0PW8FkGfiiv/9g0y5xWNjmRWD8Q2l2blSSBY3s=";
60-
query-engine-hash = "sha256-6ILWB6ZmK4ac6SgAtqCkZKHbQANmcqpWO92U8CfkFzw=";
61-
libquery-engine-hash = "sha256-n9IimBruqpDJStlEbCJ8nsk8L9dDW95ug+gz9DHS1Lc=";
62-
schema-engine-hash = "sha256-j38xSXOBwAjIdIpbSTkFJijby6OGWCoAx+xZyms/34Q=";
63-
}).fromCommit
64-
"6a3747c37ff169c90047725a05a6ef02e32ac97e"
65-
);
72+
prisma = prisma-factory {
73+
inherit pkgs;
74+
hash = "sha256-R9PG286KQTbzF0r/PPcShUkMiYam2prRh/JICjmhCZA=";
75+
_commit = "6a3747c37ff169c90047725a05a6ef02e32ac97e";
76+
};
6677
in
6778
pkgs.mkShell {
6879
buildInputs = [

lib/fetcher.nix

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
lib,
3+
stdenv,
4+
zlib,
5+
curl,
6+
cacert,
7+
autoPatchelfHook,
8+
runCommand,
9+
gzip,
10+
# variables
11+
commit,
12+
openssl,
13+
opensslVersion,
14+
binaryTarget,
15+
hash,
16+
components,
17+
}:
18+
let
19+
componentsToFetch =
20+
if components != null then
21+
components
22+
else
23+
[
24+
{
25+
url = "prisma-fmt.gz";
26+
path = "bin/prisma-fmt";
27+
env = "PRISMA_FMT_BINARY";
28+
}
29+
{
30+
url = "query-engine.gz";
31+
path = "bin/query-engine";
32+
env = "PRISMA_QUERY_ENGINE_BINARY";
33+
}
34+
{
35+
url = if isDarwin then "libquery_engine.dylib.node.gz" else "libquery_engine.so.node.gz";
36+
path = "lib/libquery_engine.node";
37+
env = "PRISMA_QUERY_ENGINE_LIBRARY";
38+
}
39+
{
40+
url = "schema-engine.gz";
41+
path = "bin/schema-engine";
42+
env = "PRISMA_SCHEMA_ENGINE_BINARY";
43+
}
44+
];
45+
isDarwin = lib.strings.hasPrefix "darwin" binaryTarget;
46+
target = if isDarwin then binaryTarget else "${binaryTarget}-openssl-${opensslVersion}";
47+
toUrl = url: "https://binaries.prisma.sh/all_commits/${commit}/${target}/${url}";
48+
deps =
49+
runCommand "prisma-deps-bin"
50+
{
51+
nativeBuildInputs = [
52+
curl
53+
cacert
54+
gzip
55+
];
56+
outputHashAlgo = "sha256";
57+
outputHashMode = "recursive";
58+
outputHash = hash;
59+
}
60+
''
61+
export SSL_CERT_FILE=${cacert}/etc/ssl/certs/ca-bundle.crt
62+
export CURL_CA_BUNDLE=$SSL_CERT_FILE
63+
mkdir -p $out $out/lib $out/bin
64+
${lib.concatLines (
65+
map (component: ''
66+
curl "${toUrl component.url}" -L | gunzip > $out/${component.path}
67+
'') componentsToFetch
68+
)}
69+
'';
70+
package = stdenv.mkDerivation {
71+
pname = "prisma-bin";
72+
src = deps;
73+
version = commit;
74+
nativeBuildInputs = [
75+
zlib
76+
openssl
77+
stdenv.cc.cc.lib
78+
] ++ lib.optionals (!isDarwin) [ autoPatchelfHook ];
79+
phases = [
80+
"installPhase"
81+
"postFixupHooks"
82+
];
83+
installPhase = ''
84+
mkdir -p $out
85+
cp -r $src/. $out/
86+
mkdir -p $out/bin
87+
chmod -R u+w $out
88+
find $out/bin -type f -exec chmod +x {} +
89+
'';
90+
};
91+
toExportStyle =
92+
attrset:
93+
"\n"
94+
+ (lib.concatMapAttrsStringSep "\n" (name: value: "export ${name}=\"${value}\"") attrset)
95+
+ "\n";
96+
mkEnv =
97+
package:
98+
builtins.listToAttrs (
99+
builtins.map (c: {
100+
name = c.env;
101+
value = "${package}/${c.path}";
102+
}) componentsToFetch
103+
);
104+
env = mkEnv package;
105+
in
106+
{
107+
inherit package env;
108+
shellHook = toExportStyle env;
109+
}

lib/legacyFetcher.nix

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
{
2+
# dependencies
3+
lib,
4+
fetchurl,
5+
stdenv,
6+
zlib,
7+
autoPatchelfHook,
8+
# variables
9+
openssl,
10+
commit,
11+
opensslVersion,
12+
binaryTarget,
13+
# = hashes
14+
prisma-fmt-hash,
15+
query-engine-hash,
16+
libquery-engine-hash,
17+
introspection-engine-hash,
18+
migration-engine-hash,
19+
schema-engine-hash,
20+
}:
21+
let
22+
hostname = "binaries.prisma.sh";
23+
channel = "all_commits";
24+
isDarwin = lib.strings.hasPrefix "darwin" binaryTarget;
25+
target = if isDarwin then binaryTarget else "${binaryTarget}-openssl-${opensslVersion}";
26+
baseUrl = "https://${hostname}/${channel}";
27+
files =
28+
[
29+
{
30+
name = "prisma-fmt";
31+
hash = prisma-fmt-hash;
32+
path = "bin/prisma-fmt";
33+
variable = "PRISMA_FMT_BINARY";
34+
}
35+
{
36+
name = "query-engine";
37+
hash = query-engine-hash;
38+
path = "bin/query-engine";
39+
variable = "PRISMA_QUERY_ENGINE_BINARY";
40+
}
41+
{
42+
name = if isDarwin then "libquery_engine.dylib.node" else "libquery_engine.so.node";
43+
hash = libquery-engine-hash;
44+
path = "lib/libquery_engine.node";
45+
variable = "PRISMA_QUERY_ENGINE_LIBRARY";
46+
}
47+
]
48+
++ (
49+
if introspection-engine-hash == null then
50+
[ ]
51+
else
52+
[
53+
{
54+
name = "introspection-engine";
55+
hash = introspection-engine-hash;
56+
path = "bin/introspection-engine";
57+
variable = "PRISMA_INTROSPECTION_ENGINE_BINARY";
58+
}
59+
]
60+
)
61+
++ (
62+
if migration-engine-hash == null then
63+
[ ]
64+
else
65+
[
66+
{
67+
name = "migration-engine";
68+
hash = migration-engine-hash;
69+
path = "bin/migration-engine";
70+
variable = "PRISMA_MIGRATION_ENGINE_BINARY";
71+
}
72+
]
73+
)
74+
++ (
75+
if schema-engine-hash == null then
76+
[ ]
77+
else
78+
[
79+
{
80+
name = "schema-engine";
81+
hash = schema-engine-hash;
82+
path = "bin/schema-engine";
83+
variable = "PRISMA_SCHEMA_ENGINE_BINARY";
84+
}
85+
]
86+
);
87+
downloadedFiles = builtins.map (
88+
file:
89+
file
90+
// {
91+
file = fetchurl {
92+
name = "${baseUrl}/${commit}/${target}/${file.name}.gz";
93+
url = "${baseUrl}/${commit}/${target}/${file.name}.gz";
94+
hash = file.hash;
95+
};
96+
}
97+
) files;
98+
unzipCommands = builtins.map (file: "gunzip -c ${file.file} > $out/${file.path}") downloadedFiles;
99+
100+
mkEnv =
101+
package:
102+
builtins.listToAttrs (
103+
builtins.map (file: {
104+
name = file.variable;
105+
value = "${package}/${file.path}";
106+
}) files
107+
);
108+
# polyfill: the function in nixpkgs is implemented on Dec 6, 2024. replace this with one from pkgs.lib after 24.11 reaches EOL.
109+
concatMapAttrsStringSep =
110+
sep: f: attrs:
111+
lib.concatStringsSep sep (lib.attrValues (lib.mapAttrs f attrs));
112+
/**
113+
This function converts attrset to bash export style.
114+
return value contains leading and trailing newlines.
115+
116+
# Example
117+
```nix
118+
toExportStyle { foo = "bar"; baz = "abc"; }
119+
=>
120+
''
121+
export foo="bar"
122+
export baz="abc"
123+
''
124+
```
125+
126+
# Type
127+
toExportStyle :: Attrset<String> -> String
128+
*/
129+
toExportStyle =
130+
attrset:
131+
"\n" + (concatMapAttrsStringSep "\n" (name: value: "export ${name}=\"${value}\"") attrset) + "\n";
132+
in
133+
rec {
134+
package = stdenv.mkDerivation {
135+
pname = "prisma-bin";
136+
version = commit;
137+
nativeBuildInputs = [
138+
zlib
139+
openssl
140+
stdenv.cc.cc.lib
141+
] ++ lib.optionals (!isDarwin) [ autoPatchelfHook ];
142+
phases = [
143+
"buildPhase"
144+
"postFixupHooks"
145+
];
146+
buildPhase = ''
147+
mkdir -p $out/bin
148+
mkdir -p $out/lib
149+
${lib.concatStringsSep "\n" unzipCommands}
150+
chmod +x $out/bin/*
151+
'';
152+
};
153+
env = mkEnv package;
154+
shellHook = toExportStyle env;
155+
}

0 commit comments

Comments
 (0)