cachix/devenv
GitHub: cachix/devenv
基于 Nix 的声明式开发环境管理工具,解决跨机器、跨团队的开发环境一致性和可复现性问题。
Stars: 7194 | Forks: 528
# [devenv.sh](https://devenv.sh) - 快速、声明式、可复现且可组合的开发者环境
[](https://devenv.sh)
[](https://builtwithnix.org)
[](https://discord.gg/naMgvexb6q)

[](https://github.com/cachix/devenv/releases)
[](https://github.com/cachix/devenv/actions/workflows/release.yml?branch=main)
## 功能
### 开发者体验
- **[终端 UI](https://devenv.sh/blog/2026/03/05/devenv-20-a-fresh-interface-to-nix/#terminal-ui)**:提供实时构建进度、任务层级和错误详情
- **[原生 shell 重载](https://devenv.sh/blog/2026/03/05/devenv-20-a-fresh-interface-to-nix/#native-shell-reloading)**:在保持 shell 交互性的同时在后台进行重新构建
- **[即时环境](https://devenv.sh/blog/2024/10/03/devenv-13-instant-developer-environments-with-nix-caching/)**:借助增量的 Nix 求值缓存(无更改时低于 100ms)
- **[devenv.nix 的 LSP](https://devenv.sh/lsp/)**:通过内置的 nixd 提供自动补全、悬停文档和跳转到定义功能
- **[临时环境](https://devenv.sh/ad-hoc-developer-environments/)**:通过 CLI 创建,无需任何配置文件(`--option languages.rust.enable:bool true`)
- **[外部树 devenvs](https://devenv.sh/ad-hoc-developer-environments/)**:使用来自其他仓库的配置(`--from github:myorg/configs`)
### 语言、包和服务
- **[50 多种语言](https://devenv.sh/languages/)**:内置工具链,包括编译器、LSP 服务器、格式化工具、代码检查工具和版本选择
- **[100,000 多个包](https://devenv.sh/packages/)**:来自 Nixpkgs,支持 Linux、macOS、x64 和 ARM64(包括 WSL2)
- **[40 多种服务](https://devenv.sh/services/)**:如 PostgreSQL、Redis、MySQL、MongoDB、Elasticsearch、Caddy 等
### 进程和任务
- **[原生进程管理器](https://devenv.sh/processes/)**:使用 Rust 编写,支持依赖排序、重启策略、就绪探针(exec、HTTP、systemd notify)、socket 激活、看门狗心跳和文件监视
- **[自动端口分配](https://devenv.sh/processes/#automatic-port-allocation)**:查找空闲端口,确保并行环境互不冲突
- **[任务](https://devenv.sh/tasks/)**:支持基于 DAG 的执行、缓存、并行运行和 namespace 支持
- **[脚本](https://devenv.sh/scripts/)**:可访问所有环境包
### 打包和部署
- **[OCI 容器](https://devenv.sh/containers/)**:无需 Docker 即可从您的环境中构建
- **[Outputs](https://devenv.sh/outputs/)**:使用每种语言的最佳工具(crate2nix、uv2nix...)打包应用程序
- **[多仓库支持](https://devenv.sh/guides/polyrepo/)**:跨仓库引用 outputs 和选项
### 组合和配置
- **[Profiles](https://devenv.sh/profiles/)**:用于创建环境变体(`--profile backend --profile testing`)
- **[通过 imports 组合](https://devenv.sh/composing-using-imports/)**:在项目之间共享和复用环境
- **[Inputs](https://devenv.sh/inputs/)**:用于锁定和覆盖 Nix 依赖
### 安全和集成
- **[SecretSpec](https://devenv.sh/integrations/secretspec/)**:用于声明式、与提供商无关的密钥管理(keyring、1Password、dotenv)
- **[Git 钩子](https://devenv.sh/git-hooks/)**:通过 git-hooks.nix 实现,预配置了格式化工具和代码检查工具
- **[测试](https://devenv.sh/tests/)**:通过 `devenv test` 自动启动和停止进程
- **[direnv 集成](https://devenv.sh/integrations/direnv/)**:进入目录时自动激活 shell
- **[MCP 服务器](https://devenv.sh/mcp/)**:用于 AI 助手集成(包和选项搜索)
- **[AI 生成](https://devenv.new)**:通过自然语言描述来搭建环境脚手架
## 快速开始
运行 ``devenv init`` 会生成 ``devenv.nix``:
```
{ pkgs, lib, config, inputs, ... }:
{
# https://devenv.sh/basics/
env.GREET = "devenv";
# https://devenv.sh/packages/
packages = [ pkgs.git ];
# https://devenv.sh/languages/
# languages.rust.enable = true;
# https://devenv.sh/processes/
# processes.dev.exec = "${lib.getExe pkgs.watchexec} -n -- ls -la";
# https://devenv.sh/services/
# services.postgres.enable = true;
# https://devenv.sh/scripts/
scripts.hello.exec = ''
echo hello from $GREET
'';
# https://devenv.sh/basics/
enterShell = ''
hello # Run scripts directly
git --version # Use packages
'';
# https://devenv.sh/tasks/
# tasks = {
# "myproj:setup".exec = "mytool build";
# "devenv:enterShell".after = [ "myproj:setup" ];
# };
# https://devenv.sh/tests/
enterTest = ''
echo "Running tests"
git --version | grep --color=auto "${pkgs.git.version}"
'';
# https://devenv.sh/outputs/
# outputs = {
# rust-app = config.languages.rust.import ./rust-app {};
# python-app = config.languages.python.import ./python-app {};
# };
# https://devenv.sh/git-hooks/
# git-hooks.hooks.shellcheck.enable = true;
# See full reference at https://devenv.sh/reference/options/
}
```
然后 ``devenv shell`` 会激活该环境。
## 命令
```
$ devenv
https://devenv.sh 2.0.0: Fast, Declarative, Reproducible, and Composable Developer Environments
Usage: devenv [OPTIONS] [COMMAND]
Commands:
init Scaffold devenv.yaml, devenv.nix, and .gitignore.
generate Generate devenv.yaml and devenv.nix using AI
shell Activate the developer environment. https://devenv.sh/basics/
update Update devenv.lock from devenv.yaml inputs. http://devenv.sh/inputs/
search Search for packages and options in nixpkgs. https://devenv.sh/packages/#searching-for-a-file
info Print information about this developer environment.
up Start processes in the foreground. https://devenv.sh/processes/
processes Start or stop processes. https://devenv.sh/processes/
tasks Run tasks. https://devenv.sh/tasks/
test Run tests. http://devenv.sh/tests/
container Build, copy, or run a container. https://devenv.sh/containers/
inputs Add an input to devenv.yaml. https://devenv.sh/inputs/
changelogs Show relevant changelogs.
repl Launch an interactive environment for inspecting the devenv configuration.
gc Delete previous shell generations. See https://devenv.sh/garbage-collection
build Build any attribute in devenv.nix.
eval Evaluate any attribute in devenv.nix and return JSON.
direnvrc Print a direnvrc that adds devenv support to direnv. See https://devenv.sh/integrations/direnv/.
version Print the version of devenv.
mcp Launch Model Context Protocol server for AI assistants
lsp Start the nixd language server for devenv.nix.
help Print this message or the help of the given subcommand(s)
Input overrides:
--from
Source for devenv.nix.
Can be either a filesystem path (with path: prefix) or a flake input reference.
Examples:
--from github:cachix/devenv
--from github:cachix/devenv?dir=examples/simple
--from path:/absolute/path/to/project
--from path:./relative/path
-o, --override-input
Override inputs in devenv.yaml.
Examples:
--override-input nixpkgs github:NixOS/nixpkgs/nixos-unstable
--override-input nixpkgs path:/path/to/local/nixpkgs
-O, --option
Override configuration options with typed values.
OPTION must include a type: :
Supported types: string, int, float, bool, path, pkg, pkgs
List types (pkgs) append to existing values by default.
Add a ! suffix to replace instead: pkgs!
Examples:
--option languages.rust.channel:string beta
--option services.postgres.enable:bool true
--option languages.python.version:string 3.10
--option packages:pkgs "ncdu git" (appends to packages)
--option packages:pkgs! "ncdu git" (replaces all packages)
Nix options:
-j, --max-jobs
Maximum number of Nix builds to run concurrently.
Defaults to 1/4 of available CPU cores (minimum 1).
[env: DEVENV_MAX_JOBS=]
-u, --cores
Number of CPU cores available to each build.
Defaults to available cores divided by max-jobs (minimum 1).
[env: DEVENV_CORES=]
-s, --system
Override the target system.
Defaults to the host system (e.g. aarch64-darwin, x86_64-linux).
-i, --impure
Relax the hermeticity of the environment.
--no-impure
Force a hermetic environment, overriding config.
--offline
Disable substituters and consider all previously downloaded files up-to-date.
--nix-option
Pass additional options to nix commands.
These options are passed directly to Nix using the --option flag.
See `man nix.conf` for the full list of available options.
Examples:
--nix-option sandbox false
--nix-option keep-outputs true
--nix-option system x86_64-darwin
--nix-debugger
Enter the Nix debugger on failure.
Shell options:
-c, --clean [...]
Ignore existing environment variables when entering the shell. Pass a list of comma-separated environment variables to let through.
-P, --profile
Activate one or more profiles defined in devenv.nix.
Profiles allow you to define different configurations that can be merged with your base configuration.
See https://devenv.sh/profiles for more information.
Examples:
--profile python-3.14
--profile backend --profile fast-startup
--reload
Enable auto-reload when config files change (default).
--no-reload
Disable auto-reload when config files change.
Cache options:
--eval-cache
Enable caching of Nix evaluation results (default).
--no-eval-cache
Disable caching of Nix evaluation results.
--refresh-eval-cache
Force a refresh of the Nix evaluation cache.
--refresh-task-cache
Force a refresh of the task cache.
Secretspec options:
--secretspec-provider
Override the secretspec provider
[env: SECRETSPEC_PROVIDER=]
--secretspec-profile
Override the secretspec profile
[env: SECRETSPEC_PROFILE=]
Tracing options:
--trace-output
Enable tracing and set the output destination: stdout, stderr, or file:. Tracing is disabled by default.
[env: DEVENV_TRACE_OUTPUT=]
--trace-format
Set the trace output format. Only takes effect when tracing is enabled via --trace-output.
Possible values:
- full: A verbose structured log format used for debugging
- json: A JSON log format used for machine consumption
- pretty: A pretty human-readable log format used for debugging
[env: DEVENV_TRACE_FORMAT=]
[default: json]
Global options:
-v, --verbose
Enable additional debug logs.
-q, --quiet
Silence all logs
--tui
Enable the interactive terminal interface (default when interactive).
[env: DEVENV_TUI=]
--no-tui
Disable the interactive terminal interface.
-h, --help
Print help (see a summary with '-h')
-V, --version
Print version information and exit
```
## 文档
- [入门指南](https://devenv.sh/getting-started/)
- [基础知识](https://devenv.sh/basics/)
- [路线图](https://devenv.sh/roadmap/)
- [博客](https://devenv.sh/blog/)
- [`devenv.yaml` 参考](https://devenv.sh/reference/yaml-options/)
- [`devenv.nix` 参考](https://devenv.sh/reference/options/)
- [贡献指南](https://devenv.sh/community/contributing/)
标签:Nix, SOC Prime, 可复现构建, 声明式配置, 开发工具, 开发环境, 环境管理, 通知系统