AgentSkillsCN

nix

熟悉 Nix 语言的惯用模式与反模式。在编写或审查任何 Nix 代码时,务必遵循以下最佳实践:避免使用 `with` 和 `rec`,合理访问 lib 模块,善用条件判断、命名规范与代码格式化规则。

SKILL.md
--- frontmatter
name: nix
description: "Idiomatic Nix language patterns and anti-patterns. Use when writing or reviewing any Nix code: avoiding `with` and `rec`, proper lib access, conditionals, naming conventions, and formatting."

Writing Nix

Anti-Patterns

Never use with

Breaks static analysis, LSP, and readability.

nix
# BAD
meta = with lib; { license = licenses.mit; };

# GOOD
meta = { license = lib.licenses.mit; };

Never use rec

Use let-in instead — rec risks infinite recursion.

nix
# BAD
rec { version = "1.0"; name = "pkg-${version}"; }

# GOOD
let version = "1.0";
in { inherit version; name = "pkg-${version}"; }

Library Access

SituationPattern
1-2 lib functionsInline lib.mkIf, lib.mkDefault
3+ lib functionsinherit (lib) mkIf mkOption types; in let
Module-levelNEVER with lib;

Destructure Arguments

nix
# BAD
args: { name = args.pname; }

# GOOD
{ pkgs, settings, ... }: { }

Conditionals

NeedUse
Conditional config blocklib.mkIf
Conditional list itemslib.optionals
Conditional stringlib.optionalString
Combine conditionalslib.mkMerge
Simple value selectionif x then a else b
nix
home.packages = [
  pkgs.coreutils
] ++ lib.optionals pkgs.stdenv.isDarwin [
  pkgs.darwin-tool
];

Naming

ElementStyleExample
VariablescamelCaseuserName, enableFeature
Files/dirskebab-casegit-absorb/, rename-utils/
Moduledefault.nixAlways

Validation

  • nixfmt via treefmt for formatting
  • statix for anti-pattern detection
  • shellcheck for embedded shell scripts
  • nix flake check --no-build for structure