
Add Setting Env
This walks you through wiring up environment variables that set default values for user settings in a Next.js app using t3-oss/env-nextjs and Zod validation. You define the env var with schema validation, optionally create new domain types in your server config, merge them into the user store, and document everything in both English and Chinese. The priority hierarchy is clear: user customs override server env vars, which override hardcoded defaults. It's thorough enough that you won't forget the .env.example update or the docs, which honestly happens more often than it should. The AI_IMAGE_DEFAULT_IMAGE_NUM example shows the full flow from validation (1-20 range) through to the store slice.
- skills.sh installs
- 1.3K
- GitHub stars
- 79.8K
- last updated
- Jul 14, 2026
Install
$ npx -y skills add lobehub/lobe-chat --skill add-setting-env --agent claude-codeInstalls into .claude/skills of the current project.
Files
Adding Environment Variable for User Settings
Add server-side environment variables to configure default values for user settings.
Priority: User Custom > Server Env Var > Hardcoded Default
Steps
1. Define Environment Variable
Create packages/env/src/<domain>.ts (import path stays @/envs/<domain> — tsconfig maps to packages/env/src/* first):
import { createEnv } from '@t3-oss/env-core';
import { z } from 'zod';
export const get<Domain>Config = () => {
return createEnv({
server: {
YOUR_ENV_VAR: z.coerce.number().min(MIN).max(MAX).optional(),
},
runtimeEnv: {
YOUR_ENV_VAR: process.env.YOUR_ENV_VAR,
},
});
};
export const <domain>Env = get<Domain>Config();
2. Update Type (if new domain)
Add to packages/types/src/serverConfig.ts:
import { User<Domain>Config } from './user/settings';
export interface GlobalServerConfig {
<domain>?: PartialDeep<User<Domain>Config>;
}
Prefer reusing existing types from packages/types/src/user/settings.
3. Assemble Server Config (if new domain)
In apps/server/src/globalConfig/index.ts:
Sponsors
Details
- Repository
- github.com/lobehub/lobe-chat
- First seen
- May 16, 2026