Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
.direnv
result
result-*
31 changes: 21 additions & 10 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@
formatter = treefmt.config.build.wrapper;
checks =
(pkgs.callPackages ./tests.nix {
fetcherMode = "new";
inherit
pkgs
prisma-factory
yarn-v1
yarn-berry
;
})
// (pkgs.callPackages ./tests.nix {
fetcherMode = "legacy";
inherit
pkgs
prisma-factory
Expand All @@ -51,18 +61,19 @@
// {
format = treefmt.config.build.check self;
};
packages.default =
(prisma-factory {
inherit pkgs;
hash = "sha256-R9PG286KQTbzF0r/PPcShUkMiYam2prRh/JICjmhCZA=";
_commit = "6a3747c37ff169c90047725a05a6ef02e32ac97e";
}).package;
devShells.default =
let
prisma = (
(prisma-factory {
inherit pkgs;
prisma-fmt-hash = "sha256-4zsJv0PW8FkGfiiv/9g0y5xWNjmRWD8Q2l2blSSBY3s=";
query-engine-hash = "sha256-6ILWB6ZmK4ac6SgAtqCkZKHbQANmcqpWO92U8CfkFzw=";
libquery-engine-hash = "sha256-n9IimBruqpDJStlEbCJ8nsk8L9dDW95ug+gz9DHS1Lc=";
schema-engine-hash = "sha256-j38xSXOBwAjIdIpbSTkFJijby6OGWCoAx+xZyms/34Q=";
}).fromCommit
"6a3747c37ff169c90047725a05a6ef02e32ac97e"
);
prisma = prisma-factory {
inherit pkgs;
hash = "sha256-R9PG286KQTbzF0r/PPcShUkMiYam2prRh/JICjmhCZA=";
_commit = "6a3747c37ff169c90047725a05a6ef02e32ac97e";
};
in
pkgs.mkShell {
buildInputs = [
Expand Down
109 changes: 109 additions & 0 deletions lib/fetcher.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
lib,
stdenv,
zlib,
curl,
cacert,
autoPatchelfHook,
runCommand,
gzip,
# variables
commit,
openssl,
opensslVersion,
binaryTarget,
hash,
components,
}:
let
componentsToFetch =
if components != null then
components
else
[
{
url = "prisma-fmt.gz";
path = "bin/prisma-fmt";
env = "PRISMA_FMT_BINARY";
}
{
url = "query-engine.gz";
path = "bin/query-engine";
env = "PRISMA_QUERY_ENGINE_BINARY";
}
{
url = if isDarwin then "libquery_engine.dylib.node.gz" else "libquery_engine.so.node.gz";
path = "lib/libquery_engine.node";
env = "PRISMA_QUERY_ENGINE_LIBRARY";
}
{
url = "schema-engine.gz";
path = "bin/schema-engine";
env = "PRISMA_SCHEMA_ENGINE_BINARY";
}
];
isDarwin = lib.strings.hasPrefix "darwin" binaryTarget;
target = if isDarwin then binaryTarget else "${binaryTarget}-openssl-${opensslVersion}";
toUrl = url: "https://binaries.prisma.sh/all_commits/${commit}/${target}/${url}";
deps =
runCommand "prisma-deps-bin"
{
nativeBuildInputs = [
curl
cacert
gzip
];
outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = hash;
}
''
export SSL_CERT_FILE=${cacert}/etc/ssl/certs/ca-bundle.crt
export CURL_CA_BUNDLE=$SSL_CERT_FILE
mkdir -p $out $out/lib $out/bin
${lib.concatLines (
map (component: ''
curl "${toUrl component.url}" -L | gunzip > $out/${component.path}
'') componentsToFetch
)}
'';
package = stdenv.mkDerivation {
pname = "prisma-bin";
src = deps;
version = commit;
nativeBuildInputs = [
zlib
openssl
stdenv.cc.cc.lib
] ++ lib.optionals (!isDarwin) [ autoPatchelfHook ];
phases = [
"installPhase"
"postFixupHooks"
];
installPhase = ''
mkdir -p $out
cp -r $src/. $out/
mkdir -p $out/bin
chmod -R u+w $out
find $out/bin -type f -exec chmod +x {} +
'';
};
toExportStyle =
attrset:
"\n"
+ (lib.concatMapAttrsStringSep "\n" (name: value: "export ${name}=\"${value}\"") attrset)
+ "\n";
mkEnv =
package:
builtins.listToAttrs (
builtins.map (c: {
name = c.env;
value = "${package}/${c.path}";
}) componentsToFetch
);
env = mkEnv package;
in
{
inherit package env;
shellHook = toExportStyle env;
}
155 changes: 155 additions & 0 deletions lib/legacyFetcher.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
# dependencies
lib,
fetchurl,
stdenv,
zlib,
autoPatchelfHook,
# variables
openssl,
commit,
opensslVersion,
binaryTarget,
# = hashes
prisma-fmt-hash,
query-engine-hash,
libquery-engine-hash,
introspection-engine-hash,
migration-engine-hash,
schema-engine-hash,
}:
let
hostname = "binaries.prisma.sh";
channel = "all_commits";
isDarwin = 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 = 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
);
# polyfill: the function in nixpkgs is implemented on Dec 6, 2024. replace this with one from pkgs.lib after 24.11 reaches EOL.
concatMapAttrsStringSep =
sep: f: attrs:
lib.concatStringsSep sep (lib.attrValues (lib.mapAttrs f attrs));
/**
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 = stdenv.mkDerivation {
pname = "prisma-bin";
version = commit;
nativeBuildInputs = [
zlib
openssl
stdenv.cc.cc.lib
] ++ lib.optionals (!isDarwin) [ autoPatchelfHook ];
phases = [
"buildPhase"
"postFixupHooks"
];
buildPhase = ''
mkdir -p $out/bin
mkdir -p $out/lib
${lib.concatStringsSep "\n" unzipCommands}
chmod +x $out/bin/*
'';
};
env = mkEnv package;
shellHook = toExportStyle env;
}
Loading