# Configure User Settings with Environment Variables

Set default values for user settings using server-side environment variables, such as defining an AI image count default.

**Canonical URL:** https://skillshunter.net/skills/github-lobehub-lobehub-agents-skills-add-setting-env-c43432501b55

**JSON URL:** https://skillshunter.net/api/public/v1/skills/github-lobehub-lobehub-agents-skills-add-setting-env-c43432501b55

**Markdown URL:** https://skillshunter.net/skills/github-lobehub-lobehub-agents-skills-add-setting-env-c43432501b55.md

**Instructions download URL:** https://skillshunter.net/skills/github-lobehub-lobehub-agents-skills-add-setting-env-c43432501b55/download

**Category:** Software & Development

**Source URL:** https://github.com/lobehub/lobehub/blob/54382b5c8e81544da454fa6d7b50f461a562c1ee/.agents/skills/add-setting-env/SKILL.md

**Source commit:** 54382b5c8e81544da454fa6d7b50f461a562c1ee

**Listing approved at:** 2026-11-22

**Created at:** 2026-11-21T10:51:53.000000Z

**Updated at:** 2026-11-22T11:17:05.000000Z

## Catalog information

The following descriptions are catalog information. Listing approval does not mean the skill has been tested or is safe for every environment.

### Editor's note

Ensure the `packages/env/src/<domain>.ts` file is created with the correct import path (`@/envs/<domain>`), and update `packages/types/src/serverConfig.ts` with the new domain type if necessary. Add the environment variable to `.env.example` and update the documentation in `docs/self-hosting/environment-variables/basic.mdx`.

### When to use it

- Set default image count for AI-generated images.
- Customize user preferences without modifying code.

## Skill instructions

---
name: add-setting-env
description: Add server-side environment variables that control default values for user settings.
disable-model-invocation: true
argument-hint: '[setting-name]'
---

# 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):

```typescript
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`:

```typescript
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`:

```typescript
import { <domain>Env } from '@/envs/<domain>';

export const getServerGlobalConfig = async () => {
  const config: GlobalServerConfig = {
    <domain>: cleanObject({
      <settingName>: <domain>Env.YOUR_ENV_VAR,
    }),
  };
  return config;
};
```

### 4. Merge to User Store (if new domain)

In `src/store/user/slices/common/action.ts`:

```typescript
const serverSettings: PartialDeep<UserSettings> = {
  <domain>: serverConfig.<domain>,
};
```

### 5. Update .env.example

```bash
# <Description> (range/options, default: X)
# YOUR_ENV_VAR=<example>
```

### 6. Update Documentation

- `docs/self-hosting/environment-variables/basic.mdx` (EN)
- `docs/self-hosting/environment-variables/basic.zh-CN.mdx` (CN)

## Example: AI\_IMAGE\_DEFAULT\_IMAGE\_NUM

```typescript
// packages/env/src/image.ts  (import as @/envs/image)
AI_IMAGE_DEFAULT_IMAGE_NUM: z.coerce.number().min(1).max(20).optional(),

// packages/types/src/serverConfig.ts
image?: PartialDeep<UserImageConfig>;

// apps/server/src/globalConfig/index.ts
image: cleanObject({ defaultImageNum: imageEnv.AI_IMAGE_DEFAULT_IMAGE_NUM }),

// src/store/user/slices/common/action.ts
image: serverConfig.image,

// .env.example
# AI_IMAGE_DEFAULT_IMAGE_NUM=4
```

