An attempt at cleaning my /nix/store
pls help my /nix/store
Published: Apr 17, 2025

/nix/store were known to become extremely bloated after large amounts of system changes. Now, it has finally bothered me enough to force myself into freeing my disk space.

Identifying

First of all, I wanted to know what package is taking so much space in the system. We can quickly do that by using fd and some basic UNIX utilities.

$ fd . "/nix/store" -H -d 1 -t d -x du -sh | tee store.txt

Here, fd walks through all first-level directories in our /nix/store and execute du -sh for each found. Then, it is logged into store.txt to be used for other purposes. tee is used here to get live outputs of fd while also saving the output to store.txt.

Once we have got our data, we can sort them out using sort:

$ cat store.txt | sort -h

...
786M	/nix/store/0rn3apgrjdqrq6zpqsrh5b8dmlxbci42-rustc-1.85.0
817M	/nix/store/65rh16mh99fln30ba6bhg37d0gygc3ch-clang-19.1.7-lib
817M	/nix/store/z5995dzahxwj4k9ffixasw6mwjk592sd-clang-19.1.7-lib
1.2G	/nix/store/4c8wnf73sn9k4rlzzb9mhjwzkd3v8ril-source
1.5G	/nix/store/2pi0733gx1l6mfy3shcq3y9hcbb3hxyb-libreoffice-24.8.5.2
1.5G	/nix/store/azgd3yxx65qzdx8ck5jnb4b321yy42di-libreoffice-24.8.4.2
1.7G	/nix/store/32c93kbfsp3x91bhr8370hzx1ynqg5nd-ghc-9.6.6

This will sort all directories by their size in ascending order, so that you’d see the largest package right above your shell prompt. If you wanted to browse the file interactively, sort it in descending order, and open it using a terminal pager, like less for example:

$ cat store.txt | sort -hr | less

Cleaning

Now, you’ve known what was taking so much space in your system. You may choose to purge them or not.

One thing you’d likely notice is that there are some duplicated and/or old stores for some packages. If you didn’t enable any kind of garbage collection on NixOS, most likely those directories won’t be going anywhere in any time soon.

Luckily, you can clear out most of them using Nix commands.

Remove old generations

This will remove all system generations except for the current one.

nix-env --profile /nix/var/nix/profiles/system --delete-generations old

Remove unused/old stores

This will also remove all old generations and then walk through all stores to detect and remove old stores.

nix-collect-garbage -d

Optimise store

This command tries to remove all duplicated files in your store.

nix store optimise

What?

And then, maybe you realized it didn’t work so well at all. I did all of this, removed the libreoffice, retrocore and some other packages, and when I ran all of the commands again, some stores were still there when they shouldn’t be.

...
283M	/nix/store/csvkhgdbkwas5mx8kjzk3y2pyv5apmqr-floorp-unwrapped-11.23.0
283M	/nix/store/yhlpbsqicphvv4kkmjvw01xbxqy188jv-floorp-unwrapped-11.25.0
293M	/nix/store/2nf40m689jgz1jyqjfmaggj3sa77049i-librewolf-unwrapped-136.0.1-1
335M	/nix/store/dw6xj3ig9ws7dvkqa4k05nrshqlmfb8p-emacs-pgtk-30.0.93
336M	/nix/store/1z0lbiv6w4kz0vspk3ivanaiwphyjgnn-emacs-pgtk-30.1
348M	/nix/store/641ikx3dvladq8mphfvi03j62c9cyym6-wine-10.0
348M	/nix/store/xscdgv18nz3lj5ya7bnqp1bg34xfmbfd-wine-10.0
356M	/nix/store/nyis1gj6479d3r6fvn0vw7s83g731h7h-google-cloud-sdk-515.0.0
361M	/nix/store/1l5gb4773rbqjzv49wb4h9xlmaz2zs5h-zig-0.13.0
371M	/nix/store/dzdlr4lms4wgjvi02r1pcqh54iiq9pn5-zig-0.14.0
373M	/nix/store/frqqgm3qcyy2nq7apjklpq9zxkfapwp6-electron-unwrapped-33.4.8
373M	/nix/store/zrq2y1f50k7wsblrbv3720wbxp99j4xs-electron-unwrapped-33.4.1
374M	/nix/store/xhgf5p3gr78c7xzyrmy9kx477x9lj6si-electron-unwrapped-34.5.0
395M	/nix/store/92xrmisaxzcdifwbmddvd829bbh6q27c-qtwebengine-6.8.2
395M	/nix/store/ys6c9lb02pswhirlbwffki7gv4xjbvhj-qtwebengine-6.8.2
416M	/nix/store/5ds20jm3x2s4z7wn3581r6lc9ybmh45b-source
419M	/nix/store/kdynjy1mbgkdg4p196v9gx6ljpf7q4nk-source
428M	/nix/store/fmkk29ny3hlg31g8nzwzjwrv8d741w6x-nixos
432M	/nix/store/ry527crpglp1clbigs4z6z02127wpav5-lsp-types-2.3.0.0
473M	/nix/store/a2cqrwqjifyfq8vz1sw2ki3vb5nc5w8b-anki-bin-25.02
473M	/nix/store/bg73dr00b2ndgb6lzlhki196s9il43cb-anki-bin-25.02
475M	/nix/store/ncgghw604sy4wygr6wk0gwfb2znvvkzh-llvm-18.1.8-lib
475M	/nix/store/ssn6m16ki16l2pf1x2m5appf332jpqn7-llvm-18.1.8-lib
477M	/nix/store/ww5327c6m9zjzwbii1ii2981shw87wz2-retroarch-assets-1.20.0-unstable-2025-01-30
481M	/nix/store/gkb76wz2dmx7x8d8xlf78gg1bxnf6l6m-llvm-18.1.8-lib
486M	/nix/store/42j011aadbpp3qp4h0y33zm7j403v3zb-llvm-19.1.7-lib
486M	/nix/store/g4kaihh5vb1fnzazm52dnn05y0m0z8gp-llvm-19.1.7-lib
504M	/nix/store/gdfl8p6d252whpdl03mxivsw2xh9snib-llvm-19.1.7-lib
504M	/nix/store/jc956plgrvplf3mi6dyn5ds0frr6h692-llvm-19.1.7-lib
548M	/nix/store/i60i19f4d42npxfzpjam6g3r4ljxzwfm-linux-firmware-20250211-zstd
566M	/nix/store/55qm2mvhmv7n2n6yzym1idrvnlwia73z-openjdk-21.0.5+11
566M	/nix/store/jqc9khwn11pmd9b8b2gxb8fcfknd4r67-openjdk-21.0.5+11
582M	/nix/store/iypxfpwhzr3m93pp5ji66wjwr02mdnac-linux-firmware-20250410-zstd
647M	/nix/store/y0n2rsgi3lfdsgvh2rdkglnb2c07a4gs-source
652M	/nix/store/8093vxnrv6399br66ljkgg38sm1f6bl5-ghc-lib-parser-9.6.6.20240701
652M	/nix/store/dc7xmddfynfy9vqfshry8mickzb481k7-ghc-9.6.6-doc
670M	/nix/store/4i3a50ysjh079pxbpbnx9cg766sw1fr6-rustc-1.84.0
734M	/nix/store/zkzk2yhszwxwnncamc3r1dijpj5z5ng7-clang-19.1.7-lib
780M	/nix/store/ag2fgzv36y5z4kfnwclj5x7l53mrabk4-clang-18.1.8-lib
786M	/nix/store/0rn3apgrjdqrq6zpqsrh5b8dmlxbci42-rustc-1.85.0
817M	/nix/store/65rh16mh99fln30ba6bhg37d0gygc3ch-clang-19.1.7-lib
817M	/nix/store/z5995dzahxwj4k9ffixasw6mwjk592sd-clang-19.1.7-lib
1.5G	/nix/store/2pi0733gx1l6mfy3shcq3y9hcbb3hxyb-libreoffice-24.8.5.2
1.5G	/nix/store/azgd3yxx65qzdx8ck5jnb4b321yy42di-libreoffice-24.8.4.2
1.7G	/nix/store/32c93kbfsp3x91bhr8370hzx1ynqg5nd-ghc-9.6.6

At this point, I’m out of ideas. I can’t find much help with my trusted search engines either, so now, I can only ask for help.

To all NixOS users reading this post, I’m begging you to tell me how can I clean up my /nix/store properly, or tell me why some packages seemed to be duplicated (I suspect my knowledge about how Nix stores work was lacking?). Any help would be appreciated!


← Go to parent