Skip to content

Apply best practices #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions .helix/languages.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[language]]
name = "nix"
formatter = { command = "nixfmt", args = ["--strict"] }
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
};
treefmt = treefmt-nix.lib.evalModule pkgs {
# nixfmt is nixfmt-rfc-style
programs.nixfmt.enable = true;
programs.nixfmt = {
enable = true;
strict = true;
};
};
in
{
Expand Down Expand Up @@ -65,23 +68,20 @@
);
in
pkgs.mkShell {
buildInputs = [
inherit (prisma) env;
packages = [
pkgs.nodejs-18_x
pkgs.pnpm
pkgs.bun
pkgs.stdenv.cc.cc.lib
prisma.package
pkgs.nixfmt-rfc-style
yarn-v1
yarn-berry
];
env = prisma.env;
};
}
)
// {
lib = {
inherit prisma-factory;
};
lib = { inherit prisma-factory; };
};
}
136 changes: 136 additions & 0 deletions fromCommit.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
# pkgs + lib
utils,
lib,
stdenv,
openssl,
fetchurl,
zlib,
autoPatchelfHook,

# info
system,
opensslVersion,
binaryTargetBySystem,

# hashes
prisma-fmt-hash,
query-engine-hash,
libquery-engine-hash,
introspection-engine-hash,
schema-engine-hash,
migration-engine-hash,
}:
commit:
if builtins.stringLength commit != 40 then
throw "invalid commit: got ${commit}"
else
let
hostname = "binaries.prisma.sh";
channel = "all_commits";
binaryTarget = binaryTargetBySystem.${system};
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;
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 = builtins.listToAttrs (
builtins.map (file: {
name = file.variable;
value = "${package}/${file.path}";
}) files
);
shellHook = utils.toExportStyle env;
in
{
inherit env package shellHook;
}
30 changes: 30 additions & 0 deletions parsers/bun-lock.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{ utils }:
path:
let
lockfile = utils.fromJSONWithTrailingCommas (
assert builtins.typeOf path == "path";
builtins.readFile path
);
bunLockParsers = {
# example:
# nu> open bun.lock | from json | get packages.@prisma/engines-version.0
# @prisma/[email protected]
"0" = bunLockParsers."1";
"1" =
lock:
utils.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
);
};
lockfileVersion = builtins.toString lockfile."lockfileVersion";
parser =
bunLockParsers.${lockfileVersion} or (throw ''
nix-prisma-utils: Unsupported lockfile version: ${lockfileVersion}
nix-prisma-utils currently supports bun.lock version of 0 and 1.
'');
in
parser lockfile
7 changes: 7 additions & 0 deletions parsers/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ callPackage, utils }:
{
npmLock = callPackage ./npm-lock.nix { };
yarnLock = callPackage ./yarn-lock.nix { inherit utils; };
pnpmLock = callPackage ./pnpm-lock.nix { };
bunLock = callPackage ./bun-lock.nix { inherit utils; };
}
12 changes: 12 additions & 0 deletions parsers/npm-lock.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ lib }:
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 = lib.lists.last (lib.strings.splitString "." version);
in
commit
49 changes: 49 additions & 0 deletions parsers/pnpm-lock.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Type
# path -> string (commit)
{ lib }:
path:
let
pnpmLock = builtins.readFile path;
lockfileVersion =
if lib.strings.hasPrefix "lockfileVersion: 5" pnpmLock then
"5"
else if lib.strings.hasPrefix "lockfileVersion: '6" pnpmLock then
"6"
else if lib.strings.hasPrefix "lockfileVersion: '9" pnpmLock then
"9"
else
throw ''
nix-prisma-utils: unknown pnpm lockfile version. please report this to nix-prisma-utils with your lockfile.
'';
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
lib.lists.last (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
lib.lists.last (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
lib.lists.last (lib.strings.splitString "." version);
};
pnpmLockParser = pnpmLockParsers.${lockfileVersion};
commit = pnpmLockParser pnpmLock;
in
commit
52 changes: 52 additions & 0 deletions parsers/yarn-lock.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{ lib, utils }:
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.
'')
(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 (utils.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 (utils.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
(parse lockfile)
Loading