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.

1$ 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:

 1$ cat store.txt | sort -h
 2
 3...
 4786M	/nix/store/0rn3apgrjdqrq6zpqsrh5b8dmlxbci42-rustc-1.85.0
 5817M	/nix/store/65rh16mh99fln30ba6bhg37d0gygc3ch-clang-19.1.7-lib
 6817M	/nix/store/z5995dzahxwj4k9ffixasw6mwjk592sd-clang-19.1.7-lib
 71.2G	/nix/store/4c8wnf73sn9k4rlzzb9mhjwzkd3v8ril-source
 81.5G	/nix/store/2pi0733gx1l6mfy3shcq3y9hcbb3hxyb-libreoffice-24.8.5.2
 91.5G	/nix/store/azgd3yxx65qzdx8ck5jnb4b321yy42di-libreoffice-24.8.4.2
101.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:

1$ 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.

1nix-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.

1nix-collect-garbage -d

Optimise store

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

1nix 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.

 1...
 2283M	/nix/store/csvkhgdbkwas5mx8kjzk3y2pyv5apmqr-floorp-unwrapped-11.23.0
 3283M	/nix/store/yhlpbsqicphvv4kkmjvw01xbxqy188jv-floorp-unwrapped-11.25.0
 4293M	/nix/store/2nf40m689jgz1jyqjfmaggj3sa77049i-librewolf-unwrapped-136.0.1-1
 5335M	/nix/store/dw6xj3ig9ws7dvkqa4k05nrshqlmfb8p-emacs-pgtk-30.0.93
 6336M	/nix/store/1z0lbiv6w4kz0vspk3ivanaiwphyjgnn-emacs-pgtk-30.1
 7348M	/nix/store/641ikx3dvladq8mphfvi03j62c9cyym6-wine-10.0
 8348M	/nix/store/xscdgv18nz3lj5ya7bnqp1bg34xfmbfd-wine-10.0
 9356M	/nix/store/nyis1gj6479d3r6fvn0vw7s83g731h7h-google-cloud-sdk-515.0.0
10361M	/nix/store/1l5gb4773rbqjzv49wb4h9xlmaz2zs5h-zig-0.13.0
11371M	/nix/store/dzdlr4lms4wgjvi02r1pcqh54iiq9pn5-zig-0.14.0
12373M	/nix/store/frqqgm3qcyy2nq7apjklpq9zxkfapwp6-electron-unwrapped-33.4.8
13373M	/nix/store/zrq2y1f50k7wsblrbv3720wbxp99j4xs-electron-unwrapped-33.4.1
14374M	/nix/store/xhgf5p3gr78c7xzyrmy9kx477x9lj6si-electron-unwrapped-34.5.0
15395M	/nix/store/92xrmisaxzcdifwbmddvd829bbh6q27c-qtwebengine-6.8.2
16395M	/nix/store/ys6c9lb02pswhirlbwffki7gv4xjbvhj-qtwebengine-6.8.2
17416M	/nix/store/5ds20jm3x2s4z7wn3581r6lc9ybmh45b-source
18419M	/nix/store/kdynjy1mbgkdg4p196v9gx6ljpf7q4nk-source
19428M	/nix/store/fmkk29ny3hlg31g8nzwzjwrv8d741w6x-nixos
20432M	/nix/store/ry527crpglp1clbigs4z6z02127wpav5-lsp-types-2.3.0.0
21473M	/nix/store/a2cqrwqjifyfq8vz1sw2ki3vb5nc5w8b-anki-bin-25.02
22473M	/nix/store/bg73dr00b2ndgb6lzlhki196s9il43cb-anki-bin-25.02
23475M	/nix/store/ncgghw604sy4wygr6wk0gwfb2znvvkzh-llvm-18.1.8-lib
24475M	/nix/store/ssn6m16ki16l2pf1x2m5appf332jpqn7-llvm-18.1.8-lib
25477M	/nix/store/ww5327c6m9zjzwbii1ii2981shw87wz2-retroarch-assets-1.20.0-unstable-2025-01-30
26481M	/nix/store/gkb76wz2dmx7x8d8xlf78gg1bxnf6l6m-llvm-18.1.8-lib
27486M	/nix/store/42j011aadbpp3qp4h0y33zm7j403v3zb-llvm-19.1.7-lib
28486M	/nix/store/g4kaihh5vb1fnzazm52dnn05y0m0z8gp-llvm-19.1.7-lib
29504M	/nix/store/gdfl8p6d252whpdl03mxivsw2xh9snib-llvm-19.1.7-lib
30504M	/nix/store/jc956plgrvplf3mi6dyn5ds0frr6h692-llvm-19.1.7-lib
31548M	/nix/store/i60i19f4d42npxfzpjam6g3r4ljxzwfm-linux-firmware-20250211-zstd
32566M	/nix/store/55qm2mvhmv7n2n6yzym1idrvnlwia73z-openjdk-21.0.5+11
33566M	/nix/store/jqc9khwn11pmd9b8b2gxb8fcfknd4r67-openjdk-21.0.5+11
34582M	/nix/store/iypxfpwhzr3m93pp5ji66wjwr02mdnac-linux-firmware-20250410-zstd
35647M	/nix/store/y0n2rsgi3lfdsgvh2rdkglnb2c07a4gs-source
36652M	/nix/store/8093vxnrv6399br66ljkgg38sm1f6bl5-ghc-lib-parser-9.6.6.20240701
37652M	/nix/store/dc7xmddfynfy9vqfshry8mickzb481k7-ghc-9.6.6-doc
38670M	/nix/store/4i3a50ysjh079pxbpbnx9cg766sw1fr6-rustc-1.84.0
39734M	/nix/store/zkzk2yhszwxwnncamc3r1dijpj5z5ng7-clang-19.1.7-lib
40780M	/nix/store/ag2fgzv36y5z4kfnwclj5x7l53mrabk4-clang-18.1.8-lib
41786M	/nix/store/0rn3apgrjdqrq6zpqsrh5b8dmlxbci42-rustc-1.85.0
42817M	/nix/store/65rh16mh99fln30ba6bhg37d0gygc3ch-clang-19.1.7-lib
43817M	/nix/store/z5995dzahxwj4k9ffixasw6mwjk592sd-clang-19.1.7-lib
441.5G	/nix/store/2pi0733gx1l6mfy3shcq3y9hcbb3hxyb-libreoffice-24.8.5.2
451.5G	/nix/store/azgd3yxx65qzdx8ck5jnb4b321yy42di-libreoffice-24.8.4.2
461.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