Database disk image is malformed


I am running NixOs now for a month and happy with it the only issue is the error below.

When I try to run nixos-rebuild switch I get the error message database disk image is malformed (in '/nix/var/nix/db/db.sqlite')

I tried nix-store --verify --repair --check-contents and nix-collect-garbage but is doesn’t seem to help.

Any one ideas what I could do to fix my system? Thanks!


Hah. So, I saw someone (probably even @raoul) asking about this in Matrix a few days ago. I thought “oh, I had that issue, I wonder how I solved it?” but I was a little distracted and couldn’t find it in my other notes.

1 Like

Thanks, I have a ext4 filesystem with a boot partition, swap and other files with LUKS encryption. Does your explanation count also for my setup?.. I pretty new with Nix, it looks quite complex…

Alas, not directly. I was able to make throwaway root and replacement /nix filesystems without repartitioning and from the running system. For your case, the theory is the same, but the less-flexible partitioning and filesystem setup will require additional shenanigans.

Is your /nix (and /nix/store) part of the / filesystem, or a dedicated partition?

You’ll need to do something like:

  • make a backup (at least of your data and anything not reproducible by nix)
  • boot from the install media
  • open LUKS, mount filesystems under /mnt as per the manual install, without remaking the filesystems
  • clean out or move aside the old /nix (now /mnt/nix), depending on whether it’s a separate fs or just a directory under root
  • make sure your channels in the running system are set the same as the system being rebuilt
  • re-run the install as I did

With some extra external media, and extra steps, you can avoid re-downloading the same way I did, but it may just be easier to let it fetch from cache again.

Thanks @uep ! I think it would be easier for me to make a backup from /home/user and my configuration.nix + hardware-configuration.nix files. Make a reinstall and place these files back or am I missing something?

It’s almost exactly the same thing, as far as the OS install is concerned. You’re rebuilding / redownloading the system closure to a fresh store, based on the same declarative configuration to get the same result. This is what happens every time you run nixos-rebuild, except the store is empty so there’s nothing to reuse.

The main difference is that you don’t remake the filesystems, so:

  • you don’t need to restore the user data (unless something goes horribly wrong)
  • you keep other bits of state that may have accumulated on your system, stuff like systemd-journal logs, maybe some wifi or bluetooth settings you imperatively paired using gnome (or whatever) gui desktop tools, etc.

That said, if you used the GUI calamares installer last time and are more comfortable doing that again, go for it. One small thing to watch out for there is that the partition labels will likely change if they get remade, so you’ll need to merge that new information into your prior config (i.e., the filesystems.* attrset in hardware-configuration.nix)

It’s a worthwhile exercise (either way) and will help reinforce for you how reproducible your system is, and highlight some small parts you haven’t encompassed in your declarative config, like the examples above.

1 Like

One more question, every time when I preform a sudo nix-store --verify --repair --check-contents I get

error: cannot repair path '/nix/store/hif59l7wmsadxzcli88bci496f2rq365-nixos'
warning: not all store errors were fixed

How could I find what kind of “application” that is and maybe remove it? Thanks

That’s an instance of the nixos sources (at a particular revision).

Pretty readily disposable once you’ve upgraded past it and removed old generations.