the published commit on main
docs(recovery): restructure lockout guide around a mental model
- Rewrite recovery.md with a numbered flow: cause, glossary, preflight, decision tree, then both paths. - Add "you should see" checkpoints after key steps so readers can confirm progress before continuing. - Collapse one-command-per-block runs into grouped blocks and add a troubleshooting table. - Move laptop-specific devices and UUIDs into a single appendix section instead of inline. - Rename the jj message aliases from jjmsg to jj-msg and refresh their comments to match.
- author
- DAEMON
- date
- commit
1826f467e3e5ff41d1d88d472da18551ee5bdca2- parent
ae3be459
Changes
2 files changed +487 −371| docs/recovery.md | 836 | +++++++++++++++++++++++----------------- |
| dotfiles/home/.dotfiles/config/jj.zsh | 22 | +- |
docs/recovery.md
@@ -1,47 +1,158 @@
-# Recover a missing or locked account from a NixOS live USB
+# Recover a locked-out account from a NixOS live USB
-Switching a machine to a different flake host with `nh os switch -H <host>`
-can remove the accounts that host does not declare. `-H` selects a whole
-`nixosConfigurations` host, not a username: the generic host is `nixdaemon`,
-with default user `operator`. **Only switch a machine to its own host.**
+> **Locked out right now?** Read [section 1](#1-what-actually-happened) first —
+> it is 40 lines and it explains why rolling back does not fix this. Then
+> [pick your path](#4-pick-your-path).
-The home directory can survive while the account and its password hash are
-removed. Booting an older generation may recreate the account with a locked
-password. **Rollback does not restore the old `/etc/shadow`**, so every older
-boot entry can show the same login failure.
+Your account exists, your files are on the disk, and every entry in the boot
+menu rejects your password. This guide gets you back in.
-Use the [recovery script](#download-and-run-from-the-usb), or follow the
-[manual steps](#manual-recovery-from-the-usb) below. Both use a saved system
-generation already on the disk.
+**Contents**
-## Download and run from the USB
+1. [What actually happened](#1-what-actually-happened)
+2. [Words that trip people up](#2-words-that-trip-people-up)
+3. [Will this work on my machine?](#3-will-this-work-on-my-machine)
+4. [Pick your path](#4-pick-your-path)
+5. [Path A — the recovery script](#5-path-a--the-recovery-script)
+6. [Path B — by hand](#6-path-b--by-hand)
+7. [When something goes wrong](#7-when-something-goes-wrong)
+8. [This laptop's values](#8-this-laptops-values)
+9. [Prevent a repeat](#9-prevent-a-repeat)
-Boot a **NixOS installer USB of the same CPU architecture as the installed
-system**, open a terminal, and connect to the internet. The script works on
-the installed disk while the live USB is running. It does not install NixOS,
-format partitions, delete homes, recursively change ownership, or reboot automatically.
-NixOS activation itself can create accounts and adjust their home directories.
+---
-The GitLab commands below become available **after these changes are
-published to `main`**. A 404 means the script has not been published at this
-URL; stop rather than executing an error page. Download with either curl:
+## 1. What actually happened
+
+NixOS keeps two kinds of state, and only one of them is in your flake:
+
+- **Declared state** is rebuilt into every generation: which users exist,
+ their UIDs, groups and home paths.
+- **Mutable state** is written once, by whichever activation created it, and
+ is never rebuilt. `/etc/shadow` — your password hash — lives here.
+
+Rebuilding to a host that does not declare your user removes the account.
+Your home directory is not declared, so it stays. Then the trap closes:
+
+```text
+ DECLARED in the flake MUTABLE on the disk
+ (rebuilt into every generation) (written once, never rolled back)
+
+gen 41 users.daemonsec = { … } ──────▶ /etc/shadow: daemonsec hash ✓
+ │ /home/daemonsec ✓
+ │
+ │ nh os switch -H nixdaemon ← a host that declares only `operator`
+ ▼
+gen 42 users.operator = { … } ──────▶ /etc/shadow: daemonsec GONE ✗
+ /home/daemonsec still there ✓
+
+boot gen 41 again?
+ the account comes back, from the generation ──▶ but /etc/shadow is NOT
+ rolled back with it
+ = login fails on every entry
+```
+
+`-H` selects a whole `nixosConfigurations` host, not a username. In this repo
+`nixdaemon` is the generic host with user `operator`, and `nixos` is the
+owner's laptop with user `daemonsec`. **Only switch a machine to its own host.**
+
+**The one idea to take from this:** a generation restores the *configuration*,
+not the *password*. So you cannot fix it from the boot menu. You need to be
+root without logging in, which is what the live USB gives you — then you
+activate an old generation (that recreates the account) and set the password
+yourself (that fills in the mutable state).
+
+Both paths below do exactly those two things, using a saved generation that is
+already on your disk. Nothing is downloaded, built or reinstalled.
+
+---
+
+## 2. Words that trip people up
+
+| Word | What it means here |
+|---|---|
+| **generation** | One complete saved system build, kept as `/nix/var/nix/profiles/system-<N>-link`. Every entry in your boot menu is a generation. |
+| **activation** | The step that makes a generation *live*: it creates users, links `/etc`, starts services. This is the part that brings your account back. |
+| **profile** | The symlink recording which generation is current. `nix-env --switch-generation` moves it. |
+| **store path** | `/nix/store/<hash>-nixos-system-<host>-<version>` — one generation's immutable build directory. The `<host>` in the middle tells you **which machine it was built for**, which is how you avoid restoring the wrong host's config. |
+| **mutable state** | What NixOS writes once and never rebuilds: `/etc/shadow`, `/var/lib/nixos/uid-map`. Not in any generation, so not covered by rollback. |
+| **`mutableUsers`** | `true`: `passwd` changes stick. `false`: every boot rewrites passwords from the flake, so a manual reset is silently undone. |
+| **`hashedPassword` / `hashedPasswordFile`** | A password declared in the flake. Same problem — it overwrites your reset at the next boot. |
+| **subvolume** | A named sub-filesystem inside a single Btrfs partition (`@`, `nix`, `home`). They are mounted separately, all from the same device. |
+| **`nixos-enter`** | Runs a command inside the installed system mounted at `/mnt`. **It activates the generation first** — that activation is the whole point, not a side effect. |
+
+---
+
+## 3. Will this work on my machine?
+
+Check this **before** you start. Both paths make the same assumptions.
+
+| | Works | Does not work |
+|---|---|---|
+| Filesystems | ext4, ext2, Btrfs, XFS, FAT (for `/boot`) | ZFS |
+| Account layout | standard NixOS `users-groups.json` | userborn, or custom user managers |
+| Disk layout | plain partitions, Btrfs subvolumes | impermanence / bind-mounted `/etc` or `/var` |
+| Encryption | LUKS — if **you** unlock it first | anything the live USB cannot open |
+| Generations | still on disk | garbage-collected or deleted |
+
+Read-only inspection has to disable journal and log replay, so a **dirty
+filesystem may be unreadable**. If that happens, assess it — do not force a
+repair to get past this guide.
+
+You also need a **NixOS installer USB of the same CPU architecture as the
+installed system**, a terminal on it, and (for Path A) a network connection.
+
+---
+
+## 4. Pick your path
+
+```text
+Can you still log in as root on a text console (Ctrl+Alt+F2)?
+├── yes ────────────────────────────────────────────▶ you don't need this guide.
+│ Just run: passwd <user>
+└── no
+ ├── does your machine appear in the "Does not work"
+ │ column in section 3?
+ │ └── yes ─────────────────────────────────────▶ STOP. Repair it by hand.
+ │ These steps can make an
+ │ impermanence/ZFS box worse.
+ └── no
+ ├── happy to download and read a script ────▶ Path A — it finds the right
+ │ generation for you and
+ │ refuses the wrong ones
+ └── want to type every command yourself ────▶ Path B — same result, more
+ steps, no download
+```
+
+Path A is the shorter route and it applies the safety checks for you. Path B is
+there when you cannot or will not download anything, and as the explanation of
+what Path A is doing.
+
+---
+
+## 5. Path A — the recovery script
+
+The script works on the installed disk while the live USB is running. It does
+**not** install NixOS, format partitions, delete home directories, recursively
+change ownership, or reboot. It mounts read-only until you type an explicit
+confirmation phrase.
+
+### 5.1 Download it
+
+The script lives at `scripts/recover-user.py` in this repo. Use GitLab:
```sh
curl --fail --show-error --location --proto '=https' \
https://gitlab.com/DAEMON-404/NixDaemon/-/raw/main/scripts/recover-user.py \
--output /tmp/nixdaemon-recover.py
-```
-Or wget:
-
-```sh
+# no curl on the installer? use wget instead:
wget -O /tmp/nixdaemon-recover.py \
https://gitlab.com/DAEMON-404/NixDaemon/-/raw/main/scripts/recover-user.py
```
-Or from the mirror at `git.daemon-sec.xyz`, which serves every file in the
-repo raw — useful when GitLab is unreachable. It follows GitLab on a
-15-minute timer, so GitLab remains the source of truth:
+Or the `git.daemon-sec.xyz` mirror, which serves every file in the repo raw and
+is useful when GitLab is unreachable. It follows GitLab on a 15-minute timer,
+so GitLab stays the source of truth:
```sh
curl --fail --show-error --location --proto '=https' \
@@ -49,193 +160,242 @@ curl --fail --show-error --location --proto '=https' \
--output /tmp/nixdaemon-recover.py
```
-The same file with a download button, to read before running it:
+**You should see** a saved file and no error. A **404 means the script is not
+published at that URL** — stop, rather than executing an error page. To read it
+in a browser first, with a download button:
<https://git.daemon-sec.xyz/NixDaemon/file/scripts/recover-user.py.html>.
-Read the downloaded script, then enter a shell with Python available:
+Already have it on a second USB? Use that path everywhere below instead;
+recovery uses generations already on the disk and never fetches a replacement
+system.
+
+### 5.2 Read it, then get a Python
```sh
less /tmp/nixdaemon-recover.py
+
+# the installer may already have python3 — check before downloading one:
+command -v python3 || nix-shell -p python3
```
-```sh
-nix-shell -p python3
-```
+### 5.3 Inspect, read-only
-Inspect the installed disk and search for the missing user (read-only):
+Nothing is written in this mode. Run it first, every time:
```sh
sudo "$(command -v python3)" /tmp/nixdaemon-recover.py
```
-To inspect and then perform recovery:
+It asks for the **installed root partition — not the installer USB**:
+
+1. It prints every disk with its filesystem type, label and UUID. Enter the
+ root device. It refuses a device already mounted outside the script, so
+ unmount any earlier rescue mounts first.
+ **For LUKS, unlock the volume yourself first** and give the resulting
+ `/dev/mapper/...` device; the script never asks for or stores a key.
+2. Enter the root Btrfs subvolume (`@`, `root`, …), or press **Enter** for the
+ default subvolume or a non-Btrfs filesystem. If the Nix store is on its own
+ subvolume or device, it asks for that next.
+3. It then reads the installed `/etc/fstab` and **asks before mounting** each
+ separate `/home`, `/var`, `/nix` and boot filesystem, read-only.
+
+**You should see** the report described in 5.4, and then:
+
+```text
+Inspection complete. No activation or password changes. Re-run with --recover to restore.
+All script-owned mounts unmounted.
+```
+
+### 5.4 Read the report
+
+This is the part worth slowing down for. The report has four blocks:
+
+```text
+Installed accounts (no password hashes printed):
+ root: uid=0, home=/root, locked
+ operator: uid=1000, home=/home/operator, hash present (expiry/PAM may still prevent login)
+
+Remaining home directories (names are candidates, not proof of an account):
+ operator: uid=1000, account present
+ daemonsec: uid=1000, ACCOUNT MISSING
+
+Previous passwd backup (may contain the former username/UID):
+ daemonsec: uid=1000, home=/home/daemonsec
+
+Saved generations: number | link date | build | declared regular users
+ 40 | 2026-10-08T21:15 | <hash>-nixos-system-nixos-25.11.20261008 | daemonsec
+ 41 | 2026-10-09T18:42 | <hash>-nixos-system-nixos-25.11.20261009 | daemonsec
+ 42 | 2026-10-10T11:07 | <hash>-nixos-system-nixdaemon-25.11.20261010 | operator
+
+Needs recovery (declared by a saved build, cannot log in now). Installed host: nixos
+ daemonsec: recommended generation 41 (2026-10-09T18:42, <hash>-nixos-system-nixos-25.11.20261009) -- the newest build that can restore it
+```
+
+> This shows the **shape** of the output, assembled from the script's own
+> messages. Your numbers, dates and hashes will differ — read yours, do not
+> pattern-match against this.
+
+How to read it:
+
+- **`ACCOUNT MISSING`** marks a home directory with no matching account. That
+ is your lost user. Names and numeric owners are clues, not proof of who
+ owned it.
+- Here both `operator` and the orphaned home show **`uid=1000`**: NixOS gave
+ the replacement user the first free UID. Recovery continues only when the
+ generation's declared UID agrees with the home directory's actual owner.
+- **Password hashes are never printed**, in any block.
+- The generation list shows **which host each build was for**. Generation 42 is
+ a `nixdaemon` build; 40 and 41 are `nixos` builds — this laptop's own host.
+- **`Needs recovery`** is the block to act on. For each account a saved build
+ declared that cannot log in now, it names the **last build where the user was
+ active**: the newest generation that can actually restore it.
+
+The search deliberately skips generations that would fail or be undone — ones
+that never declared the user, ones built for a different host than the
+installed one, and ones with `mutableUsers = false` or a declared
+`hashedPassword`/`hashedPasswordFile`, where a `passwd` reset is overwritten at
+the next boot. If nothing can restore the account, it says so **with the
+reason** instead of naming a generation it would later refuse.
+
+### 5.5 Recover
+
+Same questions as before, then the recovery prompts:
```sh
sudo "$(command -v python3)" /tmp/nixdaemon-recover.py --recover
```
-If you already have a copy on a second USB, substitute its path for
-`/tmp/nixdaemon-recover.py`; recovery itself uses existing generations and
-does not fetch or build a replacement system. An existing Python 3 on the
-installer also avoids the `nix-shell` download.
+1. Enter your **exact lost username**.
+2. At the generation prompt, the recommendation from section 5.4 is the
+ default — press **Enter** to accept it, or type another number to override.
+ Link dates help you identify builds, but they are not a substitute for the
+ hostname and user checks the script already applied.
+3. It prints the username, expected UID, home and generation, and warns that
+ activation can change other services' persistent state.
+4. Type the exact phrase **`RECOVER <username> <number>`**. Nothing has been
+ written before this point.
+5. It remounts writable, backs up the account files, and prints where:
+ `Account backup on installed disk: /var/lib/nixdaemon-recovery/<UTC timestamp>`
+6. It asks whether to **also set a root password** for emergency console
+ access. Say `yes` — it is how you get in next time without a USB.
+7. It activates the selected generation, runs `passwd` for your user (and for
+ root if you said yes), switches the system profile and updates the boot
+ loader.
-## What to enter
+That backup directory is root-only and **contains password hashes** — keep it
+private. It holds the `/etc` account files, the NixOS user/group mapping and the
+previous profile target. It is **not a backup of your data**; if you want one,
+copy your files to another disk before you get here.
-1. The script displays disks, filesystem types, UUIDs and existing mounts.
- Enter the **installed root partition** (not the installer USB). It refuses
- devices already mounted outside the script. Unmount any earlier manual
- rescue mounts first. For LUKS, unlock the volume first, then give its
- `/dev/mapper/...` device; the script never asks for or stores encryption keys.
-2. Enter the root Btrfs subvolume name (`@`, `root`, etc.), or press Enter for
- the default subvolume or a non-Btrfs filesystem. If the Nix store is
- separate and unavailable, enter its device and subvolume when prompted.
- The script reads the installed filesystem table and asks before mounting
- separate `/home`, `/var`, `/nix` and boot filesystems read-only.
-3. Read the account list, password status, remaining home directories and
- previous passwd backup. `ACCOUNT MISSING` identifies a home directory
- whose name has no matching account. Names and numeric owners are clues;
- they do not prove which person owned it. Password hashes are never printed.
-4. Read the saved generation list, including declared usernames, then the
- **`Needs recovery`** block below it. For every account a saved build
- declared that cannot log in now, that block names the **last build where
- the user was active**: the newest generation that can actually restore it.
- The search deliberately skips generations that would fail or be undone —
- ones that never declared the user, ones built for a different host name
- than the installed one (so `nixos-system-nixos-...` wins on this laptop),
- and ones with `mutableUsers = false` or a declared `hashedPassword` /
- `hashedPasswordFile`, where a `passwd` reset is overwritten at next boot.
- If nothing can restore the account, it says so with the reason instead of
- naming a generation that will be refused.
+**You should see**, at the end:
- With `--recover`, enter your lost username; the generation prompt then
- offers that recommendation as its default, so **Enter** accepts it. Type a
- number instead to override it. Link dates help identify builds but are not
- a substitute for the hostname and user checks the tool already applies.
-5. Review the selection and type the exact `RECOVER username number` phrase.
- Only then does the script remount writable, back up account files, and
- activate the selected old system. `passwd` asks for a new user password;
- the script also offers to set a root password for emergency console login.
-6. The script selects that generation in the system profile and updates the
- boot loader. Wait for successful completion and unmounting, then reboot:
-
- ```sh
- sudo reboot
- ```
-
-Remove the USB and choose the recovered generation. Test a text-console
-login (Ctrl+Alt+F2) before troubleshooting the desktop. Check `id` and
-`ls -ldn "$HOME"`; the UID should agree. Do not recursively change ownership
-as a blanket fix: files may legitimately have other owners.
-
-### This laptop's layout
-
-These values describe the reported recovery on 2026-10-10. **Other machines
-must use their own devices and subvolumes.** Verify with `lsblk -f`.
-
-| Prompt | Laptop value |
-|---|---|
-| Root device | `/dev/disk/by-uuid/e2664576-4e65-4c79-ac1c-9e2fa9e12a26` |
-| Root subvolume | Enter (default) |
-| Separate `/nix` device, if prompted | Same device as root |
-| `/nix` subvolume | `nix` |
-| `/home` | Same device, `home` subvolume; read from installed fstab |
-| Boot | `/dev/disk/by-uuid/D3FC-22C2`; read from installed fstab |
-| Lost user | `daemonsec` |
-
-## Manual recovery from the USB
-
-You can do this without downloading the recovery script. Boot a NixOS
-installer USB with the same CPU architecture as the installed system.
-Run each command separately and check its output before continuing. **If a
-command fails, stop at that step.**
-
-The user-settings check uses `jq`. If it is not already available, enter626 more lines not shown.
dotfiles/home/.dotfiles/config/jj.zsh
@@ -36,15 +36,17 @@ alias jpushc='jj git push --change @' # auto-name a bookmark for this change
alias jcm='jj-commit'
alias jcmp='jj-commit --push'
-# Let the message write itself: jjmsg (~/.local/bin) reads the diff of @,
-# gets a conventional-commit subject + bullets from `claude -p` (or a plain
-# file-list summary with --no-ai / JJMSG_AI=0) and stamps Commit-Date and
-# Commit-Host (user@host) underneath. -t "subject" keeps your own first line.
-alias jm='jjmsg' # just print it
-alias jmd='jjmsg describe' # describe @ with it
-alias jme='jjmsg edit' # describe, then polish it in $EDITOR
-alias jmc='jjmsg commit' # seal @ with it
-alias jmcp='jjmsg commit --push'
+# Let the message write itself: jj-msg (~/.local/bin) reads the diff of @, gets
+# a conventional-commit subject + bullets from `claude -p`, strips any
+# attribution so the commit is signed as you, and commits through jj-commit so
+# the Commit-Date/Commit-Host trailers come from one place. -t "subject" keeps
+# your own first line; --no-ai (or VCS_MSG_AI=0) summarises the file list
+# instead. The message is the only thing on stdout: jj commit -m "$(jm)"
+alias jm='jj-msg' # just print it
+alias jmd='jj-msg describe' # describe @ with it
+alias jme='jj-msg edit' # describe, then polish it in $EDITOR
+alias jmc='jj-msg commit' # seal @ with it
+alias jmcp='jj-msg commit --push'
# ── operation log : jj's undo history for the whole repo ─────────────────────
alias jop='jj op log'
@@ -67,7 +69,7 @@ jjhelp() {
jj describe -m "x" (re)write @'s message — no --amend needed
jj-commit "x" / jcm commit with Commit-Date + Commit-Host trailers
jcmp "x" commit, then push the completed change (@-)
- jm / jmd / jme / jmc jjmsg: write the message from the diff — print / describe / edit / commit
+ jm / jmd / jme / jmc the message, written from the diff: print / describe / edit / commit
jj squash / jsq fold @ into its parent
jj edit <rev> / je make any change the working copy
jj bookmark move ... bookmarks replace branches; nothing is checked out