Skip to content

Commit 28de217

Browse files
committed
feat: new fetcher with simplified API
1 parent 0d4f9a4 commit 28de217

File tree

8 files changed

+674
-458
lines changed

8 files changed

+674
-458
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: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,48 @@
55
treefmt-nix.url = "github:numtide/treefmt-nix";
66
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
77
};
8-
outputs =
9-
{
10-
self,
11-
nixpkgs,
12-
flake-utils,
13-
treefmt-nix,
14-
}:
15-
let
16-
prisma-factory = import ./prisma.nix;
17-
in
8+
outputs = {
9+
self,
10+
nixpkgs,
11+
flake-utils,
12+
treefmt-nix,
13+
}: let
14+
prisma-factory = import ./prisma.nix;
15+
in
1816
flake-utils.lib.eachDefaultSystem (
19-
system:
20-
let
17+
system: let
2118
pkgs = nixpkgs.legacyPackages.${system};
2219

2320
yarn-v1 = pkgs.writeShellApplication {
2421
name = "yarn-v1";
2522
checkPhase = "";
26-
runtimeInputs = [ pkgs.yarn ];
23+
runtimeInputs = [pkgs.yarn];
2724
text = "yarn $@";
2825
};
2926
yarn-berry = pkgs.writeShellApplication {
3027
name = "yarn-berry";
3128
checkPhase = "";
32-
runtimeInputs = [ pkgs.yarn-berry ];
29+
runtimeInputs = [pkgs.yarn-berry];
3330
text = "yarn $@";
3431
};
3532
treefmt = treefmt-nix.lib.evalModule pkgs {
3633
# nixfmt is nixfmt-rfc-style
3734
programs.nixfmt.enable = true;
3835
};
39-
in
40-
{
36+
in {
4137
formatter = treefmt.config.build.wrapper;
4238
checks =
4339
(pkgs.callPackages ./tests.nix {
40+
fetcherMode = "new";
41+
inherit
42+
pkgs
43+
prisma-factory
44+
yarn-v1
45+
yarn-berry
46+
;
47+
})
48+
// (pkgs.callPackages ./tests.nix {
49+
fetcherMode = "legacy";
4450
inherit
4551
pkgs
4652
prisma-factory
@@ -51,19 +57,19 @@
5157
// {
5258
format = treefmt.config.build.check self;
5359
};
54-
devShells.default =
55-
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-
);
66-
in
60+
packages.default =
61+
(prisma-factory {
62+
inherit pkgs;
63+
hash = "sha256-R9PG286KQTbzF0r/PPcShUkMiYam2prRh/JICjmhCZA=";
64+
_commit = "6a3747c37ff169c90047725a05a6ef02e32ac97e";
65+
}).package;
66+
devShells.default = let
67+
prisma = prisma-factory {
68+
inherit pkgs;
69+
hash = "sha256-R9PG286KQTbzF0r/PPcShUkMiYam2prRh/JICjmhCZA=";
70+
_commit = "6a3747c37ff169c90047725a05a6ef02e32ac97e";
71+
};
72+
in
6773
pkgs.mkShell {
6874
buildInputs = [
6975
pkgs.nodejs-18_x

lib/fetcher.nix

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

lib/legacyFetcher.nix

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

0 commit comments

Comments
 (0)