Comment Style Guide #
For .nix files only. Not for prose docs, copied upstream option descriptions, or commit messages.
Rules #
- Keep comments short, local, ASCII, and indented to the code they describe.
- Write explanatory comments as sentence fragments in present tense, start with
#, and end with.. - Bare heading labels inside grouped lists may omit the period.
- Prefer standalone comments above code, not trailing comments.
- Comment intent, rationale, constraints, non-obvious transformations, generated structure, or meaningful grouping.
- Skip comments for self-explanatory assignments, simple
inheritblocks unless a subgroup needs a label, obvious enable flags, boilerplate module structure, and syntax boundaries likelet,in,{},with, or lambda heads.
Placement #
- Put comments directly above the smallest useful binding, attrset, list, or expression.
- Group comments directly above the grouped block.
- Comments inside lists or attrsets are fine when labeling a subsection.
- Do not put comments after
{ ... }:or_:beforelet, or anywhere between a module or lambda head andlet. - Do not use comments as spacers above
let. - If a whole-block comment would otherwise go there, put it as the first meaningful line inside
in {.
Allowed Exceptions #
- Preserve
keep-sortedand similar tool-control comments. - Rare TODOs may use
# TODO(<owner or reason>): .... - Existing emphatic or humorous comments are fine if they still help readability.
- Upstream-style package headings or labels are fine when preserving imported structure helps.
Let Binding Order #
For top-level and module let ... in expressions, use this order:
- One contiguous library/helper import group:
builtins, directlib, upstreamlib.<namespace>sources in a sensible order,lib.self, then helper APIs exposed throughpkgs. Do not separate source groups with blank lines. - Actual package derivations inherited or aliased from package sets.
- Format constructors such as
jsonFormatandtomlFormat. - External configuration or imported values such as
varsandconfig. - Configuration aliases such as
cfg,colors,secrets, andtemplates. - Primitive constants and paths.
- Package or executable aliases not already inherited.
- Simple derived values.
- Helper, constructor, and transformation functions.
- Large generated scripts, lists, and attribute sets.
Use one blank line between semantic groups, not between every binding, and never
immediately after let. In particular, separate format constructors from the
preceding library/helper or package group with a blank line. Classify bindings by
purpose rather than source path. Package-building and platform APIs are helpers;
this includes runCommand, writeShellApplication, writeText,
makeDesktopItem, mkShell, symlinkJoin, nixosOptionsDoc, buildVimPlugin,
toPythonApplication, and similar APIs. Runtime/installable derivations such as jq, json5, onefetch,
fonts, plugins, and NUR packages are packages. Split mixed inherit (pkgs)
statements at the helper/package boundary. Keep nested and small lets dependency-
or concept-ordered unless these groups clearly apply.
Keep-Sorted Patterns #
- Use matching
keep-sortedstart/end control comments for multi-item module argument sets,inheritgroups, lists, and compact attrsets. - Add the
block=yes newline_separated=yesflags for attrsets or lists whose entries are separated by blank lines. - Put control comments inside the list or attrset they sort, indented to the entries.
- Do not add
keep-sortedaround single-item groups or every localletbinding unless the surrounding file already does. - Keep sorted dependency groups alphabetical, such as
after,requires,wantedBy, andwants.
Review Standard #
- Delete comments that only restate code.
- Move misplaced file or block comments below
in {inlet-using files. - Keep comments attached to the exact code they explain.
- Preserve required control comments.
- Prefer deleting a weak comment over keeping noise.