Fixing Disk Space Issues on NixOS

The Problem

I recently encountered an unexpected disk space issue on my NixOS system. After running without a restart for 5 days, relying only on suspend/sleep and performing several system updates, I discovered my /nix/store directory had bloated to 100GB.

Root Cause

The issue stemmed from:

  1. Not restarting the system for an extended period (affected some GC roots)
  2. Multiple system updates accumulating without a full restart
  3. Components like the window manager waiting for a restart to complete updates
  4. Old software versions and generations remaining in the store

Simple Solution

Two steps resolved the issue:

  1. Restart your system
  2. Run sudo nix-collect-garbage --delete-older-than 7d

After these steps, my /nix/store size decreased to 44GB.

Additional Cleanup Commands for Developers

Development Tools Cache Cleanup

# Package Manager Caches
pnpm store prune --force            # PNPM global store (Node.js)
rm -rf .gradle/caches               # Gradle (Java/Android)
cargo clean -Z gc                   # Rust (nightly only)

# Go Caches
go clean -modcache
go clean -cache

# Docker
docker system prune -a -f           # Removes all unused containers, networks, images (both dangling and unreferenced)

Node.js Projects Cleanup

# Find node_modules directories (creates a review file)
find . -type d -name "node_modules" -not -path "*/node_modules/**/node_modules" | tee reviewed_node_modules.txt

# After reviewing, delete the selected directories
cat reviewed_node_modules.txt | xargs -I {} rm -rf {}
rm reviewed_node_modules.txt

System Analysis and Additional Cleanup

# Analyze disk usage
nix-shell -p ncdu --command 'ncdu /'

# Devenv.sh cleanup
devenv gc

# Application cache cleanup
rm -rf .cache/spotify/Data

Core Dumps Review (Optional)

# Locate core dumps (you need to manually delete them)
find / -type f -name 'core' 2>/dev/null

:warning: Warning: Exercise caution with these commands. They can cause data loss if used improperly. Always review what you’re deleting and ensure you have backups of important data.

Tips

  • Regularly restart your system after updates
  • Use ncdu to identify space-consuming directories
  • Review cleanup commands and their implications before execution
  • Keep track of development tool caches, especially in large projects

I hope this helps others who might encounter similar issues!

1 Like

“restarting” or “rebooting” doesn’t affect this.

What affects the store size is garbage collection, and blindly suggesting --delete-old is harmfull in my opinion, --delete-older-than is much more sensible for most users.

Also please keep in mind, that you have to use it with sudo (or as root) to get rid of old system profiles, while you have to run it as a regular user to clean up that user profiles generations.

It affects some GC roots, I believe, e.g. /run/booted-system (mainly for kernel but it’s kept whole surely), and other running SW like the display manager that people usually don’t restart except on reboots.

3 Likes

If you do a lot of development, especially when using nix-direnv, it can be a good idea to check under /nix/var/nix/gcroots/*. I noticed that even though I regularly run GC deleting old generations as both, root and regular user, my Nix store would keep growing. I do a lot of the aforementioned development and noticed that my Nix store kept getting bigger and bigger over time.

To get rid of them, just delete the .direnv folder in the project and things should be garbage collected. Though I’d prefer if nix-direnv had some form of tooling for that.

1 Like

Yeah, I checked nix-store roots before and after, they were different.
I also had devenv installed for various projects which also caused extra space to be taken because of different versions of the same programming language being installed.

Thank you. I updated my message.

Exactly, I researched for this over 2 hours yesterday. I ended up with a simple find and rm command afterwards.

Indeed it affects running processes as roots, though the reboot is usually not the relevant part in getting rid of cruft, but much more the fact that GCs are done at all.

1 Like