chore(dev): 配置本地开发环境

引入 devenv 与 starship 配置,统一 Go/Node/Postgres 开发依赖与快捷命令。
同时让脚本在使用本地数据库环境时跳过 Docker 创建步骤。
This commit is contained in:
chensipeng 2026-05-13 06:44:26 +00:00
parent 2685450f3e
commit 2a9a833cd7
6 changed files with 304 additions and 1 deletions

11
.gitignore vendored
View File

@ -13,3 +13,14 @@ coverage/
.idea
.gitignore
# Devenv
.devenv*
devenv.local.nix
devenv.local.yaml
# direnv
.direnv
# pre-commit
.pre-commit-config.yaml

65
devenv.lock Normal file
View File

@ -0,0 +1,65 @@
{
"nodes": {
"devenv": {
"locked": {
"dir": "src/modules",
"lastModified": 1778613747,
"narHash": "sha256-+FdF9iIvBQIC391Xkoso3IFIl/Iqp2NolSvCOgEIm78=",
"owner": "cachix",
"repo": "devenv",
"rev": "c9ee1d61986a6dde1cf45e738b01395cd5bce470",
"type": "github"
},
"original": {
"dir": "src/modules",
"owner": "cachix",
"repo": "devenv",
"type": "github"
}
},
"nixpkgs": {
"inputs": {
"nixpkgs-src": "nixpkgs-src"
},
"locked": {
"lastModified": 1778507786,
"narHash": "sha256-HzSQCKMsMr8r55LwM1JuzIOB+8bzk0FEv6sItKvsfoY=",
"owner": "cachix",
"repo": "devenv-nixpkgs",
"rev": "8f24a228a782e24576b155d1e39f0d914b380691",
"type": "github"
},
"original": {
"owner": "cachix",
"ref": "rolling",
"repo": "devenv-nixpkgs",
"type": "github"
}
},
"nixpkgs-src": {
"flake": false,
"locked": {
"lastModified": 1778274207,
"narHash": "sha256-I4puXmX1iovcCHZlRmztO3vW0mAbbRvq4F8wgIMQ1MM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b3da656039dc7a6240f27b2ef8cc6a3ef3bccae7",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"devenv": "devenv",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

104
devenv.nix Normal file
View File

@ -0,0 +1,104 @@
{
pkgs,
lib,
config,
inputs,
...
}:
{
starship = {
enable = true;
config = {
enable = true;
path = ./starship.toml;
};
};
env = {
AI_GATEWAY_DATABASE_NAME = "easyai_ai_gateway";
AI_GATEWAY_DATABASE_URL = "host=${config.env.DEVENV_RUNTIME}/postgres dbname=easyai_ai_gateway sslmode=disable";
AI_GATEWAY_SKIP_DB_CREATE = "1";
};
packages = with pkgs; [
curl
docker-client
git
jq
lsof
postgresql_18
ripgrep
watchexec
];
scripts = {
dev.exec = "pnpm dev";
build.exec = "pnpm build";
test-all.exec = "pnpm test";
lint.exec = "pnpm lint";
migrate.exec = "pnpm migrate";
db-create.exec = "pnpm db:create";
api-test.exec = "pnpm nx run api:test";
web-build.exec = "pnpm nx run web:build";
};
services.postgres = {
enable = true;
package = pkgs.postgresql_18.withPackages (postgresPackages: [
postgresPackages.pgvector
]);
listen_addresses = "";
initialDatabases = [
{
name = "easyai_ai_gateway";
initialSQL = ''
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE EXTENSION IF NOT EXISTS vector;
'';
}
];
};
# https://devenv.sh/languages/
languages.go = {
enable = true;
package = pkgs.go;
};
languages.javascript = {
enable = true;
package = pkgs.nodejs_22;
nodejs.enable = true;
lsp.enable = true;
pnpm = {
enable = true;
install.enable = true;
};
};
enterShell = ''
echo ""
echo "EasyAI AI Gateway "
echo "$PWD"
echo ""
echo ""
echo " go: $(go version | awk '{print $3}')"
echo " node: $(node --version)"
echo " pnpm: $(pnpm --version)"
echo " psql: $(psql --version | awk '{print $3}')"
echo ""
echo ""
echo " dev / API Web"
echo " test-all API Web "
echo " build API Web"
echo " lint Web contracts "
echo " migrate API "
echo " db-create AI Gateway "
echo " api-test Go API "
echo " web-build Web "
echo ""
echo " package.json devenv scripts "
echo ""
'';
}

18
devenv.yaml Normal file
View File

@ -0,0 +1,18 @@
# yaml-language-server: $schema=https://devenv.sh/devenv.schema.json
inputs:
nixpkgs:
url: github:cachix/devenv-nixpkgs/rolling
# If you're using non-OSS software, you can set allowUnfree to true.
# allowUnfree: true
# If you're not willing to allow unsupported packages:
# allowUnsupportedSystem: false
# If you're willing to use a package that's vulnerable
# permittedInsecurePackages:
# - "openssl-1.1.1w"
# If you have more than one devenv you can merge them
#imports:
# - ./backend

View File

@ -68,7 +68,11 @@ export AI_GATEWAY_DATABASE_URL="${AI_GATEWAY_DATABASE_URL:-postgresql://${AI_GAT
echo "[ai-gateway] using database: ${AI_GATEWAY_DATABASE_URL}"
scripts/create-database.sh
if [[ "${AI_GATEWAY_SKIP_DB_CREATE:-}" == "1" ]]; then
echo "[ai-gateway] skipping Docker database creation"
else
scripts/create-database.sh
fi
pnpm nx run api:migrate
stop_stale_api_processes
exec pnpm nx run-many -t dev -p api web --parallel=2

101
starship.toml Normal file
View File

@ -0,0 +1,101 @@
# --- 全局结构 (极致紧凑高信息密度AI友好) ---
format = """
$directory\
$git_branch\
$git_status\
$nix_shell\
$nodejs\
$bun\
$rust\
$golang\
$cmd_duration\
$memory_usage\
$status\
$line_break\
$character"""
# --- 目录 ---
[directory]
style = "bold cyan"
truncation_length = 3
truncate_to_repo = false
truncation_symbol = ".../"
home_symbol = "~"
read_only = " [RO]"
# --- Git 状态 (纯文本,避免解析错误与乱码) ---
[git_branch]
symbol = "git:"
style = "bold purple"
format = "[$symbol$branch]($style) "
[git_status]
format = "[$all_status$ahead_behind]($style) "
style = "bold red"
conflicted = "="
ahead = ">"
behind = "<"
diverged = "<>"
untracked = "?"
stashed = "*"
modified = "!"
staged = "+"
renamed = "»"
deleted = "x"
# --- 编程语言与环境 (紧凑标签格式) ---
[nodejs]
symbol = "node:"
style = "bold green"
format = "[$symbol$version]($style) "
detect_files = ["package.json", ".node-version"]
[bun]
symbol = "bun:"
style = "bold blue"
format = "[$symbol$version]($style) "
[rust]
symbol = "rust:"
style = "bold 208"
format = "[$symbol$version]($style) "
[golang]
symbol = "go:"
style = "bold cyan"
format = "[$symbol$version]($style) "
[nix_shell]
symbol = "nix:"
style = "bold blue"
format = "[$symbol$state]($style) "
impure_msg = "impure"
pure_msg = "pure"
# --- AI 辅助决策信息 (性能与状态反馈) ---
[cmd_duration]
min_time = 2_000
format = "took [$duration]($style) "
style = "bold yellow"
[memory_usage]
symbol = "mem:"
disabled = false
threshold = 75
format = "[$symbol$ram_pct]($style) "
style = "bold dimmed white"
[status]
disabled = false
format = "[ERR:$status]($style) "
style = "bold red"
# --- 交互符号 (带明确状态码) ---
[character]
success_symbol = "[>](bold green)"
error_symbol = "[>](bold red)"
vicmd_symbol = "[<](bold green)"
# --- 兼容性补充 ---
[package]
disabled = true