Next.js integration
Use Varlock's Next.js plugin for validation, log redaction, and leak prevention
Varlock's @varlock/nextjs-integration package is a drop-in replacement for
@next/env (the internal package that handles .env loading) plus a config
plugin that injects additional security features. It adds validation, log
redaction, leak detection, and encrypted deployments on top of everything
varlock run already provides.
The integration works the same way as varlock run locally but removes the need
for the varlock run -- wrapper in Next.js scripts.
See the official Next.js integration guide for full details.
Install the package
npm install @varlock/nextjs-integrationOverride @next/env
Varlock's Next.js integration replaces the built-in @next/env module. Add an
override to your workspace root so the resolution is consistent:
overrides:
"@next/env": "npm:@varlock/nextjs-integration"Other package managers
The override syntax differs per package manager. See step 3 of the official guide for other package managers.
Wrap your Next.js config
Import the plugin and wrap your existing config:
import { varlockNextConfigPlugin } from "@varlock/nextjs-integration/plugin";
import type { NextConfig } from "next";
const withVarlock = varlockNextConfigPlugin();
const nextConfig: NextConfig = {
// ...
};
export default withVarlock(nextConfig);Remove varlock run from Next.js scripts
Since the plugin handles env resolution, you can drop the varlock run --
prefix from your Next.js scripts:
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}Non-Next.js scripts that read environment variables still need the
varlock run -- prefix.
Enable encrypted deployments
Without encryption, deploying to Vercel ships resolved env vars as plaintext
JSON in the build artifact. Add the @encryptInjectedEnv decorator to your
schema header to encrypt them:
# @encryptInjectedEnvUse a different key per environment so that a leaked preview build cannot
decrypt production secrets. Run varlock generate-key once per environment,
copy the output, and add it to Vercel:
vercel env add _VARLOCK_ENV_KEY preview --sensitive
vercel env add _VARLOCK_ENV_KEY production --sensitive--plain flag not implemented
The generate-key --plain flag mentioned in the official docs is not currently implemented, so
you will need to copy the key manually from the interactive output. See
varlock#921.
Turborepo
Add _VARLOCK_ENV_KEY to the env array of your app's build task in turbo.json so the cache
invalidates when the key rotates.
See the encrypted deployments guide for full setup instructions.