nixos删除旧的generations

臃肿的/nix/store

在使用了一段时间的nixos之后,我注意到我的/nix/store越来越大,在使用ncdu扫描了一次磁盘以后我发现nix store里面存储了一个软件的很多个版本

(libreoffice 举例子)

1
2
3
4
1.4 GiB S   1.4 GiB [                         ] /8cysrx7fi336w8a4j6hhk8ddgkdd4nhd-libreoffice-24.8.7.2
1.4 GiB S 1.4 GiB [ ] /kfln3cfd26rgszjycrzs3nddxdlins3z-libreoffice-24.8.7.2
1.4 GiB S 1.4 GiB [ ] /glavi3cl3lh8blm74y2l8m14cwvvjh8l-libreoffice-24.8.5.2
1.4 GiB S 1.4 GiB [ ] /zziy4c3k8vmzmwjgzq28bgsvmhmk6ri6-libreoffice-24.8.5.2

但是sudo nix-collect-garbage -d却无法删除他们

查找原因

我使用sudo nix-store -q --roots $PATH来查找这个包的gc root,在经过多次查找后,我发现在/nix/var/nix/profiles/目录下有多个generations

1
2
3
4
5
6
7
8
9
total 140K
lrwxrwxrwx 1 root root 43 Oct 7 2024 default -> /nix/var/nix/profiles/per-user/root/profile
drwxr-xr-x 3 root root 4.0K Sep 8 2024 per-user/
lrwxrwxrwx 1 root root 15 May 24 19:58 system -> system-676-link/
lrwxrwxrwx 1 root root 88 May 12 08:07 system-643-link -> /nix/store/v7z9ivdpvma69kdwywfch4w6w676zk5i-nixos-system-miLaptop-25.05.20250505.3730d8a/
lrwxrwxrwx 1 root root 88 May 12 15:19 system-644-link -> /nix/store/qlhkq5ijh56i1dsy9r609izyyqpmw122-nixos-system-miLaptop-25.05.20250510.d89fc19/
lrwxrwxrwx 1 root root 88 May 12 15:24 system-645-link -> /nix/store/v7z9ivdpvma69kdwywfch4w6w676zk5i-nixos-system-miLaptop-25.05.20250505.3730d8a/
lrwxrwxrwx 1 root root 88 May 12 16:14 system-646-link -> /nix/store/kzln35sy6ckiy4xjy9vsppz1bm09qfy7-nixos-system-miLaptop-25.05.20250505.3730d8a/
...

因为默认情况下,NixOS不会自动删除旧的generations.

只要一个/nix/store目录下的包被某个generations引用,直接或间接引用,nix-collect-garbage 命令就不会删除它.

列出所有generations

1
sudo nix-env -p /nix/var/nix/profiles/system --list-generations

删除旧的generations

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

有关”gc roots”

/nix/var/nix/gcroots里面存放了很多store path的符号链接,垃圾回收的时候会递归扫描,不会删除这些内容

使用sudo nix-store -q --roots /nix/store/kfln3cfd26rgszjycrzs3nddxdlins3z-libreoffice-24.8.7.2可以查询哪些 GC roots (垃圾回收根) 导致了 /nix/store/kfln3cfd26rgszjycrzs3nddxdlins3z-libreoffice-24.8.7.2 这个文件没有被垃圾回收

1
2
3
4
5
6
sudo nix-store -q --roots /nix/store/kfln3cfd26rgszjycrzs3nddxdlins3z-libreoffice-24.8.7.2
/home/seeker/.local/state/nix/profiles/home-manager-480-link -> /nix/store/jcs304j51w4kvcrpznyxcyws9lkmjsg0-home-manager-generation
/run/booted-system -> /nix/store/czwvr337cshb0033siab1ylpbad0xywr-nixos-system-miLaptop-25.11.20250520.2795c50
/nix/var/nix/profiles/system-678-link -> /nix/store/czwvr337cshb0033siab1ylpbad0xywr-nixos-system-miLaptop-25.11.20250520.2795c50
/run/current-system -> /nix/store/czwvr337cshb0033siab1ylpbad0xywr-nixos-system-miLaptop-25.11.20250520.2795c50
...

可以看到有产物引用了这个drv

1
2
3
4
tree -l /nix/store/jcs304j51w4kvcrpznyxcyws9lkmjsg0-home-manager-generation/ | grep "kfln3cfd26rgszjycrzs3nddxdlins3z-libreoffice-24.8.7.2"
│   │   │   ├── access2base.py -> /nix/store/kfln3cfd26rgszjycrzs3nddxdlins3z-libreoffice-24.8.7.2/lib/libreoffice/program/access2base.py
│   │   │   ├── bootstraprc -> /nix/store/kfln3cfd26rgszjycrzs3nddxdlins3z-libreoffice-24.8.7.2/lib/libreoffice/program/bootstraprc
...

nixos删除旧的generations
https://20040702.xyz/2025/05/24/nixos-delete-generations/
作者
Seeker
发布于
2025年5月24日
许可协议