Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ivan/1be7d26e86a4be048fdc5c205c3cb394 to your computer and use it in GitHub Desktop.

Select an option

Save ivan/1be7d26e86a4be048fdc5c205c3cb394 to your computer and use it in GitHub Desktop.
Tips for using Prisma on NixOS and with pnpm --ignore-scripts --no-optional
#!/usr/bin/env bash
# If you give `prisma db pull` a DATABASE_URL like
# "postgresql:///test?host=%2Ftmp%2Fephemeralpg.jymMG4", it will fail:
#
# ✖ Introspecting based on datasource defined in prisma/schema.prisma
# Error: P1001
# Can't reach database server at `/tmp/ephemeralpg.jymMG4`:`5432`
# Please make sure your database server is running at `/tmp/ephemeralpg.jymMG4`:`5432`.
#
# As per https://github.com/prisma/prisma/issues/11171, we need to convert the
# postgresql:// URI to one that actually works with Prisma:
# "postgresql:///test?host=%2Ftmp%2Fephemeralpg.jymMG4" ->
# "postgresql://$USER@localhost/test?host=%2Ftmp%2Fephemeralpg.jymMG4"
echo -n "$1" | sed -r "s,^postgres(ql)?:///,postgresql://$USER@localhost/,"
## in your NixOS config:
environment.systemPackages = [ prisma-engines ];
## in your ~/.zshrc:
# We use the prisma-engines in nixpkgs; Prisma's downloaded binaries don't work on NixOS
export PRISMA_QUERY_ENGINE_LIBRARY=/run/current-system/sw/lib/libquery_engine.node
export PRISMA_QUERY_ENGINE_BINARY=/run/current-system/sw/bin/query-engine
export PRISMA_MIGRATION_ENGINE_BINARY=/run/current-system/sw/bin/migration-engine
export PRISMA_FMT_BINARY=/run/current-system/sw/bin/prisma-fmt
## in your project:
pnpm install --ignore-scripts --no-optional --save-dev prisma
pnpm install --ignore-scripts --no-optional @prisma/client
# Import an existing PostgreSQL schema to your prisma/schema.prisma
./node_modules/.bin/prisma init --datasource-provider postgresql
export DATABASE_URL="$SOME_POSTGRES_URI?schema=your_postgresql_schema"
./node_modules/.bin/prisma db pull --force
# Actually make @prisma/client work and not error with
# error TS2305: Module '"@prisma/client"' has no exported member 'PrismaClient'.
./node_modules/.bin/prisma generate
@ivan
Copy link
Author

ivan commented Oct 21, 2022

@emivespa
Copy link

Thanks so much for this. Saved me a bunch of tinkering around.

@ivan
Copy link
Author

ivan commented Jun 26, 2023

😊

@emivespa
Copy link

emivespa commented Jul 1, 2023

Actually ended up having to use this: prisma/prisma#3120 (comment)

(In case someone runs into this.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment