Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Adding Modules

How to create a new NixOS module in this repository.

Steps

1. Create the Module File

Modules live at modules/{category}/{name}.nix, where the category is one of system, desktop, hardware, services or apps. Application modules sit one level further down, under modules/apps/cli/ or modules/apps/gui/.

2. Define Options and Config

# modules/{category}/{name}.nix
# One line saying what this module is for
{
  config,
  lib,
  pkgs,
  ...
}: let
  cfg = config.othrys.{category}.{name};
in {
  options.othrys.{category}.{name} = {
    enable = lib.mkEnableOption "Description of the module";
  };

  config = lib.mkIf cfg.enable {
    othrys.internal.persist."{category}.{name}" = {
      user.directories = [
        ".config/{name}"
      ];
    };

    othrys.internal.homeConfig."{category}.{name}" = {
      home.packages = with pkgs; [package-name];
    };
  };
}

This is the template in CONTRIBUTING.md, and modules/apps/cli/gh.nix is a module that follows it.

There is no username module argument. A module that needs the primary user reads config.othrys.system.user.name, and puts every account write behind a guard on othrys.system.users.enable, since the option has no default and a headless host can leave it unset. The template needs neither, because both of its per-user writes go through a registry.

User configuration goes into othrys.internal.homeConfig, under a key that is the module’s own option path. modules/system/home.nix, exported as nixosModules.home, splices every entry into home-manager.users.<name> behind othrys.system.users.homeManaged, so the module spells no Home Manager guard of its own. A module never writes home-manager.users directly and never reads config.home-manager, and the contract-guards hook rejects one that does. Those options exist only on a host that imports the Home Manager module, which nixosModules.default does not require. A module whose user configuration depends on a second option wraps the whole entry in a top-level lib.mkIf, as modules/services/docs.nix does.

othrys.internal.homeConfig."services.docs" = lib.mkIf cfg.desktopEntry {
  # User-level configuration
};

Persistent paths go into othrys.internal.persist, under the same key. Paths in the primary user’s home go under user, relative to it, and a system path goes in directories or files, as in modules/services/traefik.nix.

othrys.internal.persist."services.{service}" = {
  directories = [
    "/var/lib/{service}"
  ];
};

The module spells no guard here either. modules/system/impermanence.nix, exported as nixosModules.impermanence, splices the registry into environment.persistence when impermanence is on, and leaves the paths under user out on a host with no managed account. A module never writes environment.persistence directly. That option exists only on a host that imports the impermanence module, so a direct write fails on every other host, and the contract-guards hook rejects it.

3. Import in Category default.nix

Add the import to modules/{category}/default.nix:

imports = [
  ./{name}.nix
  # ... other modules
];

4. Enable in the Consuming Flake

Modules stay inert until a consuming flake turns them on. Where the enable goes is that flake’s business, whether in a shared profile imported by several hosts or in one host’s own configuration:

# e.g. hosts/<hostname>/default.nix in your flake
othrys.{category}.{name}.enable = true;

5. Test

just check

That is the whole of what this repository can verify, since it holds no hosts. Building an actual machine happens in the flake that consumes this one, against its own host definitions.

Conventions

Comments here earn their place or they get deleted. Four kinds are load-bearing.

File header. Every .nix file under modules/ and flake/ opens with two lines, the repository-relative path and a one-line statement of purpose. The path line matters because these files are read as {{#include}} fragments in the documentation, stripped of their filename.

# modules/services/example.nix
# What this module is for, in one line

Anchor markers. # ANCHOR: <name> and # ANCHOR_END: <name> mark the regions that documentation pages embed. Naming follows two rules. An options block anchors as <basename>-options. A config excerpt anchors as <basename>-<what> with a suffix describing what the excerpt shows, as in disko-partitions or hyprland-keybindings.

Add an anchor only when a documentation page actually embeds it. Every option is already published in docs/src/reference/options.md, generated from the module tree, so an anchor that no page includes is duplication with a maintenance cost and nothing reading it. The comment-hygiene check fails on one.

Prose that must exist in two places. Anchor it and add the pair to contract-mirror in flake/checks/. The consumer contract is the worked example. CONTRIBUTING.md holds the canonical copy, docs/src/architecture/ embeds it through mdbook, and AGENTS.md carries an enforced duplicate because an agent reads that file automatically and would otherwise have to be told to go and fetch the contract. Duplication is only dangerous when the copies can disagree unnoticed, so the hook diffs the two regions and turns the copy into a mirror. Only one such block exists today, and the hook stays specific to it until a second one appears.

Rationale. Comments that answer why, never what. A guard that has to sit at the attrset level, an assertion that needs its own mkIf, an upstream defect and the check that guards against its return. These are the comments worth writing at length, since they carry knowledge the code cannot.

Attribution. Where a module is adapted from another project, say so and name it.

Everything else goes. No banner rules, no # ==== separators and no boxed headers. No comment that restates the line beneath it. No NOTE:, TODO: or FIXME: prefixes. No em dashes anywhere, in comments or in prose.

Section labels inside long literal configuration blocks are the exception that survives, since a # Cursor label over five cursor settings in a hundred-line terminal config is navigation rather than restatement.