Skill站外收录 @skilllib/skill-2014279394632609796
writing-nix
编写高效、可维护的 Nix 代码,遵循声明式、显式和隔离性原则,避免使用 with 和递归属性等反模式,通过合理的模块设计与选项类型定义实现配置的可扩展性,结合惰性求值与源码过滤优化性能,采用标准格式与命名规范提升代码一致性。
测试与安全 AI 技能
Skills.MD
# Writing Nix
## Core Principles
1. **Declarative over Imperative**: Describe _what_, not _how_.
2. **Explicit over Implicit**: Avoid magic scoping or hidden dependencies.
3. **Hermetic**: No side effects, no network access during build (except
fixed-output derivations).
## Critical Anti-Patterns
### 1. The `with` Statement
**NEVER use `with`**. It breaks static analysis, tools (LSP), and readability.
```nix
# BAD
meta = with lib; { license = licenses.mit; };
# GOOD
meta = { license = lib.licenses.mit; };
```
### 2. Recursive Attributes (`rec`)
Avoid `rec` when `let-in` suffices. `rec` can cause infinite recursion and
expensive evaluation.
```nix
# BAD
rec {
version = "1.0";
name = "pkg-${version}";
}
# GOOD
let
version = "1.0";
in {
inherit version;
name = "pkg-${version}";
}
```
## Module System
### Standard Pattern
All modules must follow this structure:
```nix
{ config, lib, pkgs, ... }:
let
cfg = config.khanelinix.path.to.module;
inherit (lib) mkIf mkEnableOption mkOption types;
in {
options.khanelinix.path.to.module = {
enable = mkEnableOption "module description";
# Other options...
};
config = mkIf cfg.enable {
# Configuration implementation
};
}
```
### Option Design
- **Namespacing**: Always prefix with `khanelinix.`.
- **Types**: Use strict types (`types.bool`, `types.str`,
`types.listOf types.package`).
- **Defaults**: Use `mkDefault` for overridable values.
## Performance Optimization
### Evaluation
- **Laziness**: Nix is lazy. Don't force evaluation of unused attributes.
- **Imports**: Avoid importing large files if only a small part is needed.
- **Regex**: Avoid expensive regex operations in hot loops.
### Build
- **Closure Size**: Split outputs (`dev`, `doc`, `lib`) to reduce runtime
dependencies.
- **Filters**: Use `lib.cleanSource` or `nix-filter` to avoid rebuilding on
irrelevant file changes (e.g., README updates).
## Idiomatic Functions
### Destructuring
Always destructure arguments in function headers.
```nix
# BAD
args: stdenv.mkDerivation { name = args.pname; ... }
# GOOD
{ stdenv, pname, version, ... }: stdenv.mkDerivation {
inherit pname version;
...
}
```
### Overrides
- **`override`**: Change function arguments (inputs).
- **`overrideAttrs`**: Change derivation attributes (steps, build inputs).
## Formatting
- Use `nixfmt` (or `treefmt`).
- `camelCase` for variables.
- `kebab-case` for attributes/files.
## See Also
- **Validation**: See [validating-nix](../validating-nix/) for checking syntax,
formatting, and building code
- **Module scaffolding**: See
[../../khanelinix/scaffolding-modules/](../../khanelinix/scaffolding-modules/)
for creating new modules with proper structure
评论 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
举报「writing-nix」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次