Skill站外收录 @skilllib/skill-2117939930272413567
managing-flakes
实现 Nix flake 的高效管理与优化,涵盖依赖解析、输入更新及多 flake 架构组合。支持通过声明式配置完成模块化设计、开发环境构建与系统配置生成,适用于复杂项目的依赖隔离与性能调优。
效率工具 AI 技能
Skills.MD
# Managing Flakes
## Flake Schema & Evaluation
Master the Nix flake schema, evaluation mechanics, and advanced patterns.
### Evaluation Phases
1. **Input resolution**: Lock file generation (`flake.lock`)
2. **System parameters**: Flake function evaluation
3. **Output construction**: `packages`, `apps`, `devShells`
4. **Lazy boundaries**: Only evaluate what is requested
### Common Outputs
- `packages.${system}.default`: Main package
- `devShells.${system}.default`: Development environment
- `nixosConfigurations.${hostname}`: System config
- `apps.${system}.default`: Runnable applications
## Composition Patterns
### Multi-Flake Architecture
- **Separation of concerns**: Split monolithic flakes into logical components
- **Shared configuration**: Use centralized flakes for common settings
- **Inheritance**: Pass inputs from parent to child flakes
### Modular Design
- **Components**: Break flakes into `modules/`
- **Reusability**: Export reusable functions in `lib` output
- **Extension**: Use generic flake inputs for plugins
## Common Tasks
### Task 1: Update a single input
```bash
# Update specific input
nix flake update nixpkgs
# Or update all inputs
nix flake update
```
### Task 2: Add a new input
```nix
# In flake.nix
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
# Add new input
new-input = {
url = "github:owner/repo";
inputs.nixpkgs.follows = "nixpkgs"; # Deduplicate nixpkgs
};
};
```
**Why `follows`?**: Prevents multiple nixpkgs versions in closure, speeds up
evaluation.
### Task 3: Use a local development flake
```bash
# Point to local directory during development
nix registry add my-flake path:/home/user/my-flake
# Now can reference without full path
nix shell my-flake#package
```
### Task 4: Debug slow evaluation
```bash
# Show evaluation trace
nix eval .#nixosConfigurations.<host>.config --show-trace
# Profile evaluation
NIX_SHOW_STATS=1 nix eval .#nixosConfigurations.<host>.config
# Look for:
# - Large imports (can these be lazy-loaded?)
# - Repeated nixpkgs imports (use follows?)
# - Unnecessary full-package-set evaluations
```
## Input Management & Follows
### Input Management Decision Tree
**Adding a new dependency:**
1. **Is it in nixpkgs already?**
- Yes → No new input needed, just reference `pkgs.package-name`
- No → Continue to step 2
2. **Does it depend on nixpkgs?**
- Yes → Add `inputs.nixpkgs.follows = "nixpkgs"` to deduplicate
- No → Skip follows
3. **Is it from GitHub?**
- Yes → Use `github:owner/repo` (faster, cached)
- No → Use appropriate URI scheme (git+ssh, path, file)
4. **Is this for development only?**
- Yes → Consider registry instead of hard input
- No → Add as regular input
### Example: Adding home-manager
```nix
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs"; # Step 2: Deduplicate
};
};
```
### Optimization
- **Deduplication**: Use `inputs.<name>.follows` to share dependencies
- **Circular dependencies**: Avoid loops in follows chains
- **Registry**: Map local flakes for development (`nix registry add`)
### URI Schemes
- `github:owner/repo`: GitHub repository
- `git+ssh://...`: Private Git repository
- `path:.`: Local directory (requires absolute path or git tracking)
- `file:///...`: Local file system (no git requirement)
## Performance Optimization
### Best Practices
- **Laziness**: Avoid `import` of unused large files
- **Filtering**: Use `nix-filter` or `lib.cleanSource` to reduce context
- **Caching**: Utilize `eval-cache` where possible
- **Profiling**: Use `nix build --show-trace` to find bottlenecks
### Anti-Patterns
- Eagerly evaluating full package sets
- Importing `nixpkgs` multiple times without `follows`
- Checking in massive `flake.lock` files generated by poor input management
## See Also
- **Validation**: See [validating-nix](../validating-nix/) for debugging
evaluation errors and build failures
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「managing-flakes」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次