code-style
Write Nix code following repository conventions
Installation
Follow the repository instructions for this skill and your agent. Review the files and dependencies before adding it to your workflow.
View source ↗GuidesSkill instructions
Code Style
Critical Requirement
MUST run nix fmt <paths...> after making any changes to ensure consistent formatting.
Indentation
- Use 2 spaces for indentation
- No tabs allowed
Naming Conventions
| Context | Convention | Example |
|---|---|---|
| Files and directories | kebab-case | my-module.nix, home-manager/ |
| Nix attributes | camelCase | myOption, enableFeature |
| Option paths | camelCase | services.myService.enable |
Comments
- Minimal comments preferred
- Code should be self-explanatory
- Use comments to explain why, not what
# Good: explains why
# Workaround for upstream bug #1234
extraConfig = "...";
# Bad: describes what (obvious from code)
# Set extraConfig to the value
extraConfig = "...";
Imports
- Prefer relative imports:
./modules/foo.nix - Group imports at the top of the file
- Use list format for multiple imports:
{
imports = [
./hardware.nix
./services.nix
./networking.nix
];
}
Structured Data
When generating JSON, YAML, or other structured formats, define as Nix attribute sets and convert:
# Good: define as Nix, convert to JSON
environment.etc."config.json".text = builtins.toJSON {
setting = "value";
nested = { key = 123; };
};
# Avoid: inline JSON strings
environment.etc."config.json".text = ''
{"setting": "value", "nested": {"key": 123}}
'';
Common Patterns
Module Structure
{
config,
lib,
pkgs,
...
}:
let
inherit (lib) mkEnableOption mkOption mkIf types;
cfg = config.services.myService;
in
{
options.services.myService = {
enable = mkEnableOption "my service";
};
config = mkIf cfg.enable {
# configuration here
};
}
Let Bindings
let
inherit (lib) mkIf mkEnableOption;
cfg = config.myModule;
in
{
# use cfg, mkIf, etc.
}
Examples
Read the usage examples in the source repository.
View source ↗Source history
- Indexed on
- 2026-01-20
- Index updated
- 2026-01-26
- Branch
- main
Index and sync dates describe this directory. View the source repository for code history.
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!