# AGENTS.md Guidance for humans and agents working in this repository. ## What this is This flake manages the full configuration for all of my machines: NixOS system configuration plus home-manager user configuration, generated from one flake. Currently there are two hosts: - `prospero` — laptop. Niri (Wayland compositor) with Noctalia providing the desktop shell, Emacs running as a systemd user daemon (package set managed with twist). - `uruk` — the other machine (ZFS, enabled via the `profiles.zfs` profile). The primary user is `joshua` (hardcoded in `flake.nix` when generating home configurations). ## Day-to-day commands - `just apply` — `nixos-rebuild switch --flake . --sudo`, then `home-manager switch --flake .` - `just apply-boot` — rebuild with boot activation instead of switch - `just hm` — home-manager switch only A devshell (`alejandra`, `just`, `nil`) is provided via `.envrc` + `use flake` (nix-direnv). Format Nix code with `alejandra`. ## Source control This repo uses **jj**, colocated with a git repo (`.jj` and `.git` both present). Do not use git commands; use `jj`. **Always run `jj util snapshot --quiet` before running any `nix` command.** Nix flakes only read git-tracked files from the working tree; without a snapshot, nix may silently evaluate against stale file contents (you will see "warning: Git tree ... is dirty" — that is normal and expected here). ## Repo layout ``` flake.nix inputs, overlays, nixConfig, flake-parts outputs lib/default.nix host auto-discovery (see below) hosts// one directory per machine modules/nixos/ NixOS modules (exposed as nixosModules.default) modules/home/ home-manager modules (exposed as homeModules.default) ``` ### Host discovery (`lib/default.nix`) Every subdirectory of `hosts/` containing a `default.nix` becomes a NixOS configuration named after the directory. The `default.nix` looks like: ```nix {...}: { system = "x86_64-linux"; modules = [./configuration.nix]; # NixOS-level modules home.modules = [./home.nix]; # home-manager-level modules } ``` - If the host attrset has a `home` attribute, a `homeConfigurations.@` entry is also generated (that is what `just hm` builds). - `hostname` is set automatically from the directory name. - To add a machine: create `hosts//` with `default.nix`, `configuration.nix`, `hardware-configuration.nix` (from `nixos-generate-config`) and optionally `home.nix`. Nothing else needs registering. ### The profile pattern Shared configuration lives in `modules/nixos/profiles/` and `modules/home/profiles/`. Every profile: - declares an option like `profiles..enable` (usually `mkEnableOption`), - gates its `config` with `lib.mkIf cfg.enable`, - is imported (and thus option-declared) by the corresponding `default.nix`. Hosts opt in via `profiles = { foo.enable = true; };` in `configuration.nix` / `home.nix`. Shared defaults inside profiles use `lib.mkDefault` so hosts can override without conflict. Note some profiles are nested (e.g. `profiles.desktop.niri.enable` — the desktop profile imports and declares the niri sub-profile). Both module trees are imported into *every* host (as `nixosModules.default` / `homeModules.default` in `flake.nix`), so profile options are always declared — enabling is what activates behavior. When adding a new profile, add it to the imports in the corresponding `default.nix`, and remember the NixOS/home split: system-level services and kernel/hardware settings go under `modules/nixos/`, user-level programs and home.file under `modules/home/`. ## The Emacs subsystem (`modules/home/profiles/emacs/`) This is the most special-cased part of the repo. Emacs and its package set are managed with [twist.nix](https://github.com/emacs-twist/twist.nix) via a **nested flake** inside the profile directory. ### Layout - `flake.nix` — nested flake. All of its inputs use `follows` pointing at the parent flake, so registry snapshots (melpa, gnu-elpa, nongnu, epkgs) are pinned in the **repo-root** `flake.lock`, not here. - `nix/emacs.nix` — the twist environment (`twist.lib.makeEnv`): emacs package, init files, lock dir, and the registry list. The `custom` registry (`recipes/`) is listed **first**, so it takes precedence over melpa/gnu-elpa. - `recipes/` — melpa-style recipe files, one line each, for packages not taken verbatim from an archive (custom forks, fast-moving packages). - `nix/input-overrides.nix`, `nix/package-overrides.nix` — build-time fixes for individual elisp packages (native-comp patches, dependency overrides). - `init.el`, `early-init.el`, `etc/` — the actual Emacs configuration, deployed to `~/.config/emacs/` via `home.file`. `var/` is runtime state (custom.el lives there; it is *not* in the repo). - `lock/` — a **subflake** holding the package pins: - `flake.nix` — auto-generated, one input per package, *no revs* - `flake.lock` — the actual git rev pins for every package - `archive.lock` — archive-version data; empty (`{}`) in this setup because every package here is git-sourced - `justfile` — `just lock`, `just update`, `just test` (try the config with `--init-directory .` without deploying). ### Testing Emacs Changes should not be tested by running `just hm` and restarting the emacs daemon. Instead, test changes by running `just test` in the emacs directory to test whether a change seems to approximately work correctly. If possible, attempt to test the change by running emacs in batch mode. Running emacs in interactive mode is acceptable for testing but should be avoided when possible. It is acceptable to build test infrastructure to make further and better testing feasible. If you wish to write unit tests, use the `buttercup` library for emacs as the test framework and put the testing code outside of init.el. Putting code that is only needed when testing into init.el makes emacs run slower and init.el more distracting to edit, to make sure to try extra hard to keep anything complex out of there. ### Updating Emacs packages — IMPORTANT PITFALL `just update` and `just lock` **do not and cannot bump package versions**: - `just update` only rewrites `lock/archive.lock`, which is legitimately empty for this all-git setup. It is a no-op here. - `just lock` regenerates `lock/flake.nix` (only changes if the package *set* changes) and runs `nix flake lock`, which only fills in *missing* pins. Existing revs are never moved. To actually update package versions, run `nix flake update` **inside the lock directory**: ```sh cd modules/home/profiles/emacs/lock nix flake update # everything nix flake update # one package (input name == package name) ``` The outer registry inputs (melpa, gnu-elpa, nongnu, epkgs, twist) are updated separately at the repo root (`nix flake update ...`); they control which recipes/repos packages resolve from, not the pinned revs. The twist lock writer shells out to `git` (it requires the lock dir to be inside a git working tree and `git add`s new files), which works here because of the jj/git colocated setup. ### Emacs runs as a daemon `services.emacs` (systemd user unit, `startWithUserSession = "graphical"`), started by the niri session. The build is the **X/Lucid variant of Emacs 31, not pgtk** — graphical frames go through Xwayland via xwayland-satellite, which niri spawns via its built-in integration (`xwayland-satellite.enable = true` in the home niri profile). Known issue: **suspend/resume kills the daemon's X connection** while a zombie terminal survives, after which `emacsclient -c` silently fails to open graphical frames (only `emacsclient -t` works) until the daemon is restarted (`systemctl --user restart emacs`). If graphical emacs frames misbehave after a suspend, restart the daemon before debugging anything else. ## Nix conventions and settings - Primary nixpkgs input is `nixos-unstable` (plus a `nixpkgs-stable` input that is currently unused downstream). `allowUnfree` is on globally. - Extra substituters and keys are declared in both `flake.nix` `nixConfig` (for flake consumers) and `modules/nixos/nix.nix` (for the system); keep them in sync when adding one. - Generation retention is handled by `services.angrr` (see `nix.nix`), not by aggressive GC — old generations are kept 14 days / 10 system gens. - `stateVersion` values (`25.05` in both trees) are pinned; do not bump them. They mean "initial install generation", not current version. - Coupling to be aware of: `virtualisation.docker.storageDriver` is chosen as `zfs` iff the `profiles.zfs` profile is enabled (`modules/nixos/default.nix`). ## Theming Stylix is applied at both the NixOS and home-manager level (modules passed in `flake.nix`), and targets (niri, fuzzel, gtk, ...) are enabled in the individual profiles. Colors should come from `config.lib.stylix.colors.withHashtag` rather than hardcoded values. ## Desktop stack (prospero) - NixOS side (`modules/nixos/profiles/desktop/`): PipeWire audio, plasma login manager for the niri session, kernel defaults. - Home side (`modules/home/profiles/desktop/niri.nix`): niri (pinned to `niri-unstable` from the niri flake), fuzzel launcher, foot terminal, cliphist, Noctalia (from its own flake input), xwayland-satellite. - Keybindings are generated with a small combinator (`binds` at the top of the home niri profile) that expands prefix/suffix key maps — add bindings to the `prefixes`/`suffixes` attrsets rather than hand-writing pairs.