Communitygithub.com

Flox 环境管理

使用基于 Nix 的声明式环境管理器 Flox,创建可重现、跨平台(macOS/Linux)的开发环境。适用于为任何语言设置项目工具链、安装系统级依赖、锁定精确的包版本、运行本地服务、快速上手新开发者,或解决环境一致性问题。

Flox 环境管理 是什么?

Flox Environments 技能为开发者提供了一种基于 Nix 的声明式方法,用于创建可重现的跨平台开发环境。它允许用户通过单个 TOML 清单文件定义项目所需的完整工具链,包括系统级依赖(如编译器、数据库、OpenSSL)和语言特定的包,并支持 macOS 和 Linux。该技能的核心优势在于环境一致性:每个开发者无论使用何种机器,都能获得完全相同的包、工具和配置,无需容器或虚拟机。这对于 AI 代理尤其有用,因为它们可以在不需要 sudo 权限或污染系统的情况下动态添加工具(如 jq、编译器、数据库)到项目环境中。技能涵盖了从基本命令、清单结构、包安装模式到语言特定配方(Python、Node.js、Rust、Go、C/C++)的完整工作流程,并提供了钩子、配置文件、服务管理和环境共享等高级功能。它还强调了反模式(如绝对路径、在钩子中使用 exit、存储密钥)和调试技巧,使其成为任何需要可靠、可重现的开发环境设置的工具箱中的关键组件。

兼容平台✓Claude Code~Codex CLI~Cursor✓Antigravity
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/flox-environments

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Flox 环境管理 是做什么的?

Flox 创建可重现的开发环境,这些环境在单个 TOML 清单中定义。团队中的每位开发者都能获得完全相同的包、工具和配置——跨 macOS 和 Linux——无需容器或虚拟机。基于 Nix,可访问超过 150,000 个包。

何时激活

当用户遇到环境管理问题时,即使他们还没有提到 Flox,也应使用此技能。Flox 是以下场景的合适工具:

  • 项目需要系统级包(编译器、数据库、CLI 工具)以及特定语言的依赖项
  • 可重现性很重要——该设置应在一台全新的笔记本电脑或 CI 中与队友的机器上完全相同地工作
  • 用户需要多种工具共存——例如,在一个环境中同时使用 Python 3.11 + PostgreSQL 16 + Redis + Node.js
  • 需要跨平台支持(从同一配置支持 macOS 和 Linux)
  • AI 代理需要安装工具——Flox 允许代理在没有 sudo、不污染系统或不受沙箱限制的情况下,将包添加到项目范围的环境中

如果用户只需要一个没有系统依赖的单一语言运行时,标准工具(nvm、pyenv、rustup 单独使用)可能就足够了。如果需要完整的 OS 级隔离,容器可能更合适。Flox 处于最佳平衡点:声明式、可重现的环境,没有容器开销。

前提条件: 必须首先安装 Flox——请参阅 flox.dev/docs 了解 macOS、Linux 和 Docker 的安装方式。

核心概念

Flox 环境在 .flox/env/manifest.toml 中定义,并通过 flox activate 激活。清单声明了包、环境变量、设置钩子和 shell 配置——重现环境所需的一切。

关键路径:

  • .flox/env/manifest.toml — 环境定义(提交此文件)
  • $FLOX_ENV — 已安装包的运行时路径(类似 /usr——包含 bin/、lib/、include/)
  • $FLOX_ENV_CACHE — 用于缓存、虚拟环境、数据的持久本地存储(在重建后仍然存在)
  • $FLOX_ENV_PROJECT — 项目根目录(.flox/ 所在位置)

基本命令

flox init                       # 创建新环境
flox search <package> [--all]   # 搜索包
flox show <package>             # 显示可用版本
flox install <package>          # 添加包
flox list                       # 列出已安装的包
flox activate                   # 进入环境
flox activate -- <cmd>          # 在环境中运行命令而不进入子 shell
flox edit                       # 交互式编辑清单

清单结构

# .flox/env/manifest.toml

[install]
# 要安装的包——环境的核心
ripgrep.pkg-path = "ripgrep"
jq.pkg-path = "jq"

[vars]
# 静态环境变量
DATABASE_URL = "postgres://localhost:5432/myapp"

[hook]
# 非交互式设置脚本(每次激活时运行)
on-activate = """
  echo "环境准备就绪"
"""

[profile]
# Shell 函数和别名(在交互式 shell 中可用)
common = """
  alias dev="npm run dev"
"""

[options]
# 支持的平台
systems = ["x86_64-linux", "aarch64-linux", "x86_64-darwin", "aarch64-darwin"]

包安装模式

基本安装

[install]
nodejs.pkg-path = "nodejs"
python.pkg-path = "python311"
rustup.pkg-path = "rustup"

锁定版本

[install]
nodejs.pkg-path = "nodejs"
nodejs.version = "^20.0"          # Semver 范围:最新 20.x

postgres.pkg-path = "postgresql"
postgres.version = "16.2"         # 精确版本

平台特定包

[install]
# 仅 Linux 工具
valgrind.pkg-path = "valgrind"
valgrind.systems = ["x86_64-linux", "aarch64-linux"]

# macOS 框架
Security.pkg-path = "darwin.apple_sdk.frameworks.Security"
Security.systems = ["x86_64-darwin", "aarch64-darwin"]

# macOS 上的 GNU 工具(BSD 默认值不同)
coreutils.pkg-path = "coreutils"
coreutils.systems = ["x86_64-darwin", "aarch64-darwin"]

解决包冲突

当两个包安装相同的二进制文件时,使用 priority(数字越小优先级越高):

[install]
gcc.pkg-path = "gcc12"
gcc.priority = 3

clang.pkg-path = "clang_18"
clang.priority = 5               # gcc 赢得文件冲突

使用 pkg-group 将需要一起解析版本的包分组:

[install]
python.pkg-path = "python311"
python.pkg-group = "python-stack"

pip.pkg-path = "python311Packages.pip"
pip.pkg-group = "python-stack"    # 与 python 一起解析

语言特定配方

使用 uv 的 Python

[install]
python.pkg-path = "python311"
uv.pkg-path = "uv"

[vars]
UV_CACHE_DIR = "$FLOX_ENV_CACHE/uv-cache"
PIP_CACHE_DIR = "$FLOX_ENV_CACHE/pip-cache"

[hook]
on-activate = """
  venv="$FLOX_ENV_CACHE/venv"
  if [ ! -d "$venv" ]; then
    uv venv "$venv" --python python3
  fi
  if [ -f "$venv/bin/activate" ]; then
    source "$venv/bin/activate"
  fi

  if [ -f requirements.txt ] && [ ! -f "$FLOX_ENV_CACHE/.deps_installed" ]; then
    uv pip install --python "$venv/bin/python" -r requirements.txt --quiet
    touch "$FLOX_ENV_CACHE/.deps_installed"
  fi
"""

Node.js

[install]
nodejs.pkg-path = "nodejs"
nodejs.version = "^20.0"

[hook]
on-activate = """
  if [ -f package.json ] && [ ! -d node_modules ]; then
    npm install --silent
  fi
"""

Rust

[install]
rustup.pkg-path = "rustup"
pkg-config.pkg-path = "pkg-config"
openssl.pkg-path = "openssl"

[vars]
RUSTUP_HOME = "$FLOX_ENV_CACHE/rustup"
CARGO_HOME = "$FLOX_ENV_CACHE/cargo"

[profile]
common = """
  export PATH="$CARGO_HOME/bin:$PATH"
"""

Go

[install]
go.pkg-path = "go"
gopls.pkg-path = "gopls"
delve.pkg-path = "delve"

[vars]
GOPATH = "$FLOX_ENV_CACHE/go"
GOBIN = "$FLOX_ENV_CACHE/go/bin"

[profile]
common = """
  export PATH="$GOBIN:$PATH"
"""

C/C++

[install]
gcc.pkg-path = "gcc13"
gcc.pkg-group = "compilers"

# 重要:仅安装 gcc 不会暴露 libstdc++ 头文件——您需要 gcc-unwrapped
gcc-unwrapped.pkg-path = "gcc-unwrapped"
gcc-unwrapped.pkg-group = "libraries"

cmake.pkg-path = "cmake"
cmake.pkg-group = "build"

gnumake.pkg-path = "gnumake"
gnumake.pkg-group = "build"

gdb.pkg-path = "gdb"
gdb.systems = ["x86_64-linux", "aarch64-linux"]

钩子和配置文件

钩子——非交互式设置

钩子在每次激活时运行。保持它们快速且幂等。经验法则:如果应该自动发生,则放入 [hook];如果用户应该能够手动输入,则放入 [profile]。

[hook]
on-activate = """
  setup_database() {
    if [ ! -d "$FLOX_ENV_CACHE/pgdata" ]; then
      initdb -D "$FLOX_ENV_CACHE/pgdata" --no-locale --encoding=UTF8
    fi
  }
  setup_database
"""

配置文件——交互式 Shell 配置

配置文件中的代码在用户的 shell 会话中可用。

[profile]
common = """
  dev() { npm run dev; }
  test() { npm run test -- "$@"; }
"""

反模式

绝对路径

# 不好——在其他机器上会失效
[vars]
PROJECT_DIR = "/home/alice/projects/myapp"

# 好——使用 Flox 环境变量
[vars]
PROJECT_DIR = "$FLOX_ENV_PROJECT"

在钩子中使用 exit

# 不好——会杀死 shell
[hook]
on-activate = """
  if [ ! -f config.json ]; then
    echo "缺少配置文件"
    exit 1
  fi
"""

# 好——从钩子返回,不退出
[hook]
on-activate = """
  if [ ! -f config.json ]; then
    echo "缺少配置文件——请先运行设置命令"
    return 1
  fi
"""

在清单中存储密钥

# 不好——清单会提交到 git
[vars]
API_KEY = "<set-at-runtime>"

# 好——引用外部配置或在运行时传递
# 使用:API_KEY="<your-api-key>" flox activate
[vars]
API_KEY = "${API_KEY:-}"

没有幂等守卫的慢钩子

# 不好——每次激活都重新安装
[hook]
on-activate = """
  pip install -r requirements.txt
"""

# 好——如果已安装则跳过
[hook]
on-activate = """
  if [ ! -f "$FLOX_ENV_CACHE/.deps_installed" ]; then
    uv pip install -r requirements.txt --quiet
    touch "$FLOX_ENV_CACHE/.deps_installed"
  fi
"""

将用户命令放在钩子中

# 不好——钩子函数在交互式 shell 中不可用
[hook]
on-activate = """
  deploy() { kubectl apply -f k8s/; }
"""

# 好——使用 `[profile]` 放置用户可调用的函数
[profile]
common = """
  deploy() { kubectl apply -f k8s/; }
"""

全栈示例

一个完整的 Python API 与 PostgreSQL 环境:

[install]
python.pkg-path = "python311"
uv.pkg-path = "uv"
postgresql.pkg-path = "postgresql_16"
redis.pkg-path = "redis"
jq.pkg-path = "jq"
curl.pkg-path = "curl"

[vars]
UV_CACHE_DIR = "$FLOX_ENV_CACHE/uv-cache"
DATABASE_URL = "postgres://localhost:5432/myapp"
REDIS_URL = "redis://localhost:6379"

[hook]
on-activate = """
  if [ ! -d "$FLOX_ENV_CACHE/pgdata" ]; then
    initdb -D "$FLOX_ENV_CACHE/pgdata" --no-locale --encoding=UTF8
  fi

  venv="$FLOX_ENV_CACHE/venv"
  if [ ! -d "$venv" ]; then
    uv venv "$venv" --python python3
  fi
  if [ -f "$venv/bin/activate" ]; then
    source "$venv/bin/activate"
  fi

  if [ -f requirements.txt ] && [ ! -f "$FLOX_ENV_CACHE/.deps_installed" ]; then
    uv pip install --python "$venv/bin/python" -r requirements.txt --quiet
    touch "$FLOX_ENV_CACHE/.deps_installed"
  fi
"""

[profile]
common = """
  serve() { uvicorn app.main:app --reload --host 0.0.0.0 --port 8000; }
  migrate() { alembic upgrade head; }
"""

[services]
postgres.command = "postgres -D $FLOX_ENV_CACHE/pgdata -k $FLOX_ENV_CACHE"
redis.command = "redis-server --port 6379 --daemonize no"

[options]
systems = ["x86_64-linux", "aarch64-linux", "x86_64-darwin", "aarch64-darwin"]

使用服务激活:flox activate --start-services

环境共享

Flox 环境是 git 原生的。提交 .flox/ 目录,每位协作者都能获得相同的环境:

git add .flox/
git commit -m "添加 Flox 环境"
# 团队成员只需运行:
git clone <repo> && cd <repo> && flox activate

对于跨项目可重用的基础环境,推送到 FloxHub:

flox push                         # 将环境推送到 FloxHub
flox activate -r owner/env-name   # 在任何地方激活远程环境

使用 [include] 组合环境:

[include]
base.floxhub = "myorg/python-base"

[install]
# 在基础环境之上的项目特定补充
fastapi.pkg-path = "python311Packages.fastapi"

AI 辅助与氛围编码

Flox 非常适合 AI 辅助开发和氛围编码工作流。当 AI 代理需要当前环境中不存在的工具——编译器、数据库、Linter、CLI 工具——时,它可以将其添加到项目的 Flox 清单中,而无需 sudo 权限、不污染系统包或受到沙箱限制。

这对代理为何重要:

  • 无需 sudo — flox install 完全在用户空间中工作,因此代理无需提升权限即可添加包
  • 项目范围 — 包仅安装到项目环境中,而不是全局安装,因此不同项目可以使用不同版本而不会冲突
  • 沙箱友好 — 在沙箱或受限环境中运行的代理仍然可以通过 Flox 安装他们需要的工具
  • 可逆 — 每次更改都记录在 manifest.toml 中,因此可以干净地删除不需要的包,不留系统残留
  • 可重现 — 当代理设置环境时,该精确设置会提交到 git,并且对所有人都有效

代理工作流模式:

# 代理发现需要某个工具(例如 jq 用于 JSON 处理)
flox search jq                    # 验证包是否存在
flox install jq                   # 安装到项目环境中

# 或者为了更精细的控制,直接编辑清单
flox list -c > /tmp/manifest.toml
# 将包添加到 [install] 部分,然后应用
flox edit -f /tmp/manifest.toml

# 使用该工具运行命令
flox activate -- jq '.results[]' data.json

这使得 Flox 成为 Claude Code 或其他 AI 代理需要动态引导项目工具的任何工作流的自然选择。

调试

flox list -c                      # 显示原始清单
flox activate -- which python     # 检查哪个二进制文件被解析
flox activate -- env | grep FLOX  # 查看 Flox 环境变量
flox search <package> --all       # 更广泛的包搜索(区分大小写)

常见问题:

  • 找不到包: 搜索区分大小写——请尝试 flox search --all
  • 包之间的文件冲突: 为应该获胜的包添加 priority
  • 钩子失败: 使用 return 而不是 exit;使用 ${FLOX_ENV_CACHE:-} 进行守卫
  • 过时的依赖: 删除 $FLOX_ENV_CACHE/.deps_installed 标志文件

相关技能

以下技能作为 Flox Claude Code 插件 的一部分提供,用于更深入的集成:

  • flox-services — 服务管理、数据库设置、后台进程
  • flox-builds — 使用 Flox 进行可重现的构建和打包
  • flox-containers — 从 Flox 环境创建 Docker/OCI 容器
  • flox-sharing — 环境组合、远程环境、团队模式
  • flox-cuda — CUDA 和 GPU 开发环境

了解更多信息并安装,请访问 flox.dev/docs

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

相关技能