Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions agents/skills/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ prompt themselves. Migrations must be idempotent: if one user already applied a
machine-wide repair, the same migration running for another user should detect
that and no-op.

The marker is in the home directory, which a snapshot restore of the root filesystem leaves alone, while much of what migrations change goes back with the root. So the runner keeps two more empty files for each migration it completes:

```text
/var/lib/omarchy/migration-records/<uid>/<migration filename> (root record, on the root filesystem)
~/.local/state/omarchy/migrations-receipts/<machine-id>/<migration filename> (receipt: a root record was written on this machine)
```

A migration whose receipt for this machine is there while its root record is gone has been rolled back: the root filesystem was restored to a point before it ran. It is pending again, `omarchy-migrate --pending` lists it, and the next run announces it as running again after a snapshot restore. A marker without a receipt is trusted as before, which covers every marker written before the records existed, the markers seeded from `/etc/skel`, and a home directory carried to another machine or a reinstall.

The records directory belongs to the user but sits under root-owned directories, so creating it takes root once. The runner creates it only in a process that inherited the update lock, which is what `omarchy update` gives it along with its sudo authorization, and it uses `sudo -n`, so it never asks for a password. Any other run writes records when the directory is there and otherwise completes without them. Records and receipts are best effort: failing to write one never fails a migration, it only means a later restore will not be noticed for that migration. This is one more reason migrations must be idempotent: a rolled-back migration runs a second time against a home directory that already has its changes.

## When migrations run

### During `omarchy update`
Expand Down Expand Up @@ -165,6 +176,12 @@ rm ~/.local/state/omarchy/migrations/<migration>.sh
omarchy-migrate
```

To accept a rolled-back migration as done without running it again, remove its receipt and leave its marker:

```bash
rm ~/.local/state/omarchy/migrations-receipts/"$(cat /etc/machine-id)"/<migration>.sh
```

Don't add a test for a migration under `test/`. It runs once per machine and is frozen once it ships, so the test stops guarding anything. If a migration needs logic worth testing, put that logic in a `bin/` command the migration calls and test the command. Tests of `omarchy-migrate`, the login notifier, and `omarchy-upgrade-to-quattro` stay.

Omarchy 4.0 is upgraded through `bin/omarchy-upgrade-to-quattro`, not through the
Expand Down
54 changes: 51 additions & 3 deletions bin/omarchy-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ OMARCHY_PATH="${OMARCHY_PATH:-/usr/share/omarchy}"
STATE_DIR="${OMARCHY_MIGRATION_STATE:-$HOME/.local/state/omarchy/migrations}"
MIGRATIONS_DIR="$OMARCHY_PATH/migrations"

# Markers live in $HOME, which a snapshot restore leaves alone, while much of
# what migrations change is on the root filesystem and goes back with it. So a
# completed migration also gets a record on the root filesystem, and a receipt
# next to the markers saying that record was written on this machine. A receipt
# whose record is gone means the root filesystem was restored to a point before
# the migration ran, so the migration is pending again. Markers without a
# receipt (older runs, another machine's home) are trusted as before.
RECORDS_DIR="${OMARCHY_MIGRATION_RECORDS:-/var/lib/omarchy/migration-records/$UID}"
machine_id=""
IFS= read -r machine_id 2>/dev/null <"${OMARCHY_MIGRATION_MACHINE_ID:-/etc/machine-id}" || true
[[ $machine_id =~ ^[0-9a-f]{32}$ ]] || machine_id=""
RECEIPTS_DIR="$STATE_DIR-receipts/$machine_id"

rolled_back() {
[[ -n $machine_id && -f $RECEIPTS_DIR/$1 && ! -f $RECORDS_DIR/$1 ]]
}

# The records directory is on the root filesystem but owned by the user, so it
# takes root once to create and nothing after that. Only a run that inherited
# the update lock creates it, which is what omarchy update gives it along with
# its authorization, and sudo -n never asks. Any other run records when the
# directory is there and otherwise goes on without, which leaves its markers
# trusted as before.
record_on_root() {
[[ -n $machine_id ]] || return 1
if [[ ! -d $RECORDS_DIR ]]; then
omarchy-update-lock held 2>/dev/null || return 1
sudo -n install -d -m 755 -o "$UID" "$RECORDS_DIR" 2>/dev/null || return 1
fi
touch "$RECORDS_DIR/$1" 2>/dev/null && [[ -f $RECORDS_DIR/$1 ]]
}

migration_entries() {
[[ -d $MIGRATIONS_DIR ]] || return 0

Expand All @@ -49,7 +81,7 @@ pending_migrations() {

while IFS=$'\t' read -r name file marker; do
[[ -n $name ]] || continue
if [[ ! -f $marker ]]; then
if [[ ! -f $marker ]] || rolled_back "$name"; then
printf '%s\n' "$name"
fi
done < <(migration_entries)
Expand Down Expand Up @@ -89,8 +121,17 @@ deferred=0
while IFS=$'\t' read -r name file marker <&3; do
[[ -n $name ]] || continue

if [[ ! -f $marker ]]; then
echo -e "\e[32m\n$(omarchy-i18n update.migration "${name%.sh}")\e[0m"
again=0
if [[ -f $marker ]] && rolled_back "$name"; then
again=1
fi

if [[ ! -f $marker ]] || (( again )); then
if (( again )); then
echo -e "\e[32m\n$(omarchy-i18n update.migrationAgain "${name%.sh}")\e[0m"
else
echo -e "\e[32m\n$(omarchy-i18n update.migration "${name%.sh}")\e[0m"
fi
status=0
OMARCHY_PATH="$OMARCHY_PATH" bash -euo pipefail "$file" 3<&- || status=$?

Expand All @@ -106,6 +147,13 @@ while IFS=$'\t' read -r name file marker <&3; do
fi

mkdir -p "$(dirname "$marker")"
# Receipts are best effort: one that cannot be written only loses the
# rerun after a restore, and must not fail a migration that succeeded.
if record_on_root "$name"; then
{ mkdir -p "$RECEIPTS_DIR" && touch "$RECEIPTS_DIR/$name"; } 2>/dev/null || true
elif [[ -n $machine_id ]]; then
rm -f "$RECEIPTS_DIR/$name" 2>/dev/null || true
fi
touch "$marker"
fi
done 3< <(migration_entries)
Expand Down
1 change: 1 addition & 0 deletions default/i18n/en/cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@
"update.migrationsWaiting": "Waiting for pacman transaction to finish before running Omarchy migrations...",
"update.migrationsDeferred": "Pacman transaction is still running; Omarchy migrations will retry on next login.",
"update.migration": "Running migration (%1)",
"update.migrationAgain": "Running migration again after a snapshot restore (%1)",
"update.migrationDeferred": "Migration %1 is waiting for a package update; it and the migrations after it will run on the next update or login.",
"update.orphans": "Orphan system packages",
"update.orphansFound": {
Expand Down
4 changes: 4 additions & 0 deletions docs/update-process.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ The design goal is:
| `/tmp/omarchy-update.log` | user | Transcript of `omarchy update`, used by `omarchy-update-analyze-logs`. |
| `~/.local/state/omarchy/current/` | user | Generated active theme, selected theme name, and current background symlink. |
| `~/.local/state/omarchy/migrations/` | user | Per-user migration markers. |
| `~/.local/state/omarchy/migrations-receipts/<machine-id>/` | user | Per-user receipts: a root record was written for this migration on this machine. |
| `/var/lib/omarchy/migration-records/<uid>/` | user | Root records of completed migrations, on the root filesystem so a snapshot restore takes them back. Created with `sudo -n` by a migration run that inherited the update lock. |
| `~/.local/state/omarchy/reboot-required` | user | Optional reboot marker checked by `omarchy-update-restart`. |
| `~/.local/state/omarchy/restart-*-required` | user | Optional service/app restart markers checked by `omarchy-update-restart`. The shell needs no marker: it is restarted unconditionally after every update. |

Expand Down Expand Up @@ -57,6 +59,8 @@ privileged work should invoke the appropriate helper or privilege prompt.
Migrations must be idempotent; if one user already applied a machine-wide repair,
the migration should no-op for other users.

A snapshot restore of the root filesystem leaves those markers in place, so the runner also keeps a root record of each completed migration and a receipt for it in the home directory. A migration whose receipt is there while its root record is gone was rolled back by a restore and runs again; [`migrations.md`](../agents/skills/migrations.md) has the paths and the rules.

When invoked by the update, migrations share its single sudo authorization. The standalone migration runner has its own security changes in the migration-boundary PR; this update change does not establish that standalone boundary. Historical migrations remain strictly ordered.

For watchers and diagnostics, `omarchy-migrate --pending` prints pending
Expand Down
4 changes: 3 additions & 1 deletion manual/47-system-snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ A restore brings back your root filesystem, but not your `/home`. So it works fo

This also means that your `~/.config` directory is kept as-is. So if you're rolling back to an earlier version of a library or application that stores configuration files in a new format, you'll have to sort that out manually.

The same goes for Omarchy's own record of which migrations have already run: it lives in your home directory, so it survives the restore. Migrations that ran after the snapshot was taken stay marked as done, even though the changes they made to the root filesystem have been rolled back, and the next `omarchy update` won't run them again. So if you update again after a restore and something an earlier update had set up is still missing, that's the likely reason.
The same goes for Omarchy's own list of which migrations have already run: it lives in your home directory, so it survives the restore. To notice that, Omarchy also keeps a record on the root filesystem of the migrations run during `omarchy update`, and a note in your home directory that it did. When a restore takes the record away and the note is still there, that migration runs again at the next `omarchy update` or `omarchy-migrate`, and says it is running again after a snapshot restore.

This does not cover a migration for which Omarchy could not write both the record and the note: one run by an older version of Omarchy, one run by hand before the first update with this feature, or one where the write failed. Those stay marked as done, even though the changes they made to the root filesystem have been rolled back. So if you update again after a restore and something an earlier update had set up is still missing, that's the likely reason.
Loading