I use NixOS. My nix DB gets corrupted every few months. I want to find a way to repair this sort of problem without having to completely rebuild my system.
Note: I don’t think it’s a disk issue. I only ever have problems with the Nix store; no other files get corrupted. I’ve swapped disks several times, but the problem doesn’t go away. Might be because I’m on “unstable”.
I saw this, which looked promising…
I recently switched to btrfs, but I’m not very familiar with it yet. I guess I would use snapshots to do that?
So here’s what I did try…
❯ nix-collect-garbage
finding garbage collector roots...
deleting garbage...
0 store paths deleted, 0.00 MiB freed
error: executing SQLite statement 'delete from ValidPaths where path = '/nix/store/xfl304fnamjx1l2rj5wc0wbhkccj5lmh-context-typescripts.r60422.tar.xz.drv';': database disk image is malformed, database disk image is malformed (in '/nix/var/nix/db/db.sqlite')
❯ sudo nix-store --verify --check-contents --repair
[sudo] password for amy:
reading the Nix store...
checking path existence...
error: store path '2xw0vq0cf42p20i6h0pair49afbm6nz-text-short-0.1.5-r3.cabal.drv' contains illegal base-32 character ''
❯ sudo nix store delete /nix/store/2xw0vq0cf42p20i6h0pair49afbm6nz-text-short-0.1.5-r3.cabal.drv
path '/nix/store/2xw0vq0cf42p20i6h0pair49afbm6nz-text-short-0.1.5-r3.cabal.drv' does not contain a 'flake.nix', searching up
error: getting status of '/nix/store/2xw0vq0cf42p20i6h0pair49afbm6nz-text-short-0.1.5-r3.cabal.drv': No such file or directory
❯ ls /nix/store/2xw0vq0cf42p20i6h0pair49afbm6nz-text-short-0.1.5-r3.cabal.drv
ls: cannot access '/nix/store/2xw0vq0cf42p20i6h0pair49afbm6nz-text-short-0.1.5-r3.cabal.drv': No such file or directory
I followed the steps described in nix-collect-garbage: error getting outputs of ... database disk image is malformed · Issue #1353 · NixOS/nix · GitHub and Nix store sqlite db corruption
❯ sudo sqlite3 /nix/var/nix/db/db.sqlite 'pragma integrity_check'
wrong # of entries in index sqlite_autoindex_Refs_1
row 47413 missing from index sqlite_autoindex_ValidPaths_1
row 62261 missing from index sqlite_autoindex_ValidPaths_1
row 62262 missing from index sqlite_autoindex_ValidPaths_1
wrong # of entries in index sqlite_autoindex_ValidPaths_1
# cd /nix/var/nix/db
# nix-shell -p sqlite
# sqlite3 db.sqlite ".backup 'db.bak.sqlite' "
# sqlite3 db.sqlite
SQLite version 3.43.2 2023-10-10 12:14:04
Enter ".help" for usage hints.
sqlite> .output db.sql
sqlite> .dump
sqlite>
# sqlite3 db.new.sqlite
Parse error near line 3: table ValidPaths already exists
CREATE TABLE ValidPaths ( id integer primary key autoincreme
^--- error here
Runtime error near line 14: UNIQUE constraint failed: ValidPaths.id (19)
Runtime error near line 15: UNIQUE constraint failed: ValidPaths.id (19)
Runtime error near line 16: UNIQUE constraint failed: ValidPaths.id (19)
Runtime error near line 17: UNIQUE constraint failed: ValidPaths.id (19)
Runtime error near line 18: UNIQUE constraint failed: ValidPaths.id (19)
Runtime error near line 19: UNIQUE constraint failed: ValidPaths.id (19)
Runtime error near line 20: UNIQUE constraint failed: ValidPaths.id (19)
. . .
Runtime error near line 677463: UNIQUE constraint failed: DerivationOutputs.drv, DerivationOutputs.id (19)
Runtime error near line 677464: UNIQUE constraint failed: DerivationOutputs.drv, DerivationOutputs.id (19)
Runtime error near line 677465: UNIQUE constraint failed: DerivationOutputs.drv, DerivationOutputs.id (19)
Parse error near line 677468: index IndexReferrer already exists
Parse error near line 677469: index IndexReference already exists
Parse error near line 677470: trigger DeleteSelfRefs already exists
CREATE TRIGGER DeleteSelfRefs before delete on ValidPaths begin delete f
^--- error here
Parse error near line 677474: index IndexDerivationOutputs already exists
I don’t know what else to do at this point