Home Explore Blog Models CI



nixpkgs

nixos/doc/manual/administration/store-corruption.section.md
27ac542cd5e0e472dafc670b2178ebda0bddc6eb3ae854910000000300000409
# Nix Store Corruption {#sec-nix-store-corruption}

After a system crash, it's possible for files in the Nix store to become
corrupted. (For instance, the Ext4 file system has the tendency to
replace un-synced files with zero bytes.) NixOS tries hard to prevent
this from happening: it performs a `sync` before switching to a new
configuration, and Nix's database is fully transactional. If corruption
still occurs, you may be able to fix it automatically.

If the corruption is in a path in the closure of the NixOS system
configuration, you can fix it by doing

```ShellSession
# nixos-rebuild switch --repair
```

This will cause Nix to check every path in the closure, and if its
cryptographic hash differs from the hash recorded in Nix's database, the
path is rebuilt or redownloaded.

You can also scan the entire Nix store for corrupt paths:

```ShellSession
# nix-store --verify --check-contents --repair
```

Any corrupt paths will be redownloaded if they're available in a binary
cache; otherwise, they cannot be repaired.

Chunks
adefa9dc (1st chunk of `nixos/doc/manual/administration/store-corruption.section.md`)
Title: Nix Store Corruption and Repair
Summary
This section discusses the potential for Nix store file corruption after a system crash, often due to filesystem issues like Ext4 replacing unsynced files. While NixOS attempts to prevent this with `sync` operations and transactional databases, corruption can still occur. The text provides two methods for automatic repair: `nixos-rebuild switch --repair` to fix paths within the system configuration's closure by re-hashing and rebuilding/redownloading, and `nix-store --verify --check-contents --repair` to scan and repair the entire Nix store by redownloading corrupt paths from binary caches if available.