如何让 NixOS 显示一次更新的具体内容

当 NixOS 配置好了 flake、home-manager 等等,后续的更新我们只需要运行:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
❯ nix flake update

❯ sudo nixos-rebuild switch
building the system configuration...
activating the configuration...
setting up /etc...
reloading user units for davinci42...
restarting sysinit-reactivation.target
the following new units were started: NetworkManager-dispatcher.service
Done. The new configuration is /nix/store/mp17nhbadmffs9ah961ldj0y1l38a73m-nixos-system-azure-25.05.20250424.f771eb4

除了 kernel 的更新需要重启,某些环境变量需要新开 session 之外,其它 pkg 和对应的 systemd-service 应该都是最新的状态了。

但是上面的 log 中,我们是不知道这一次的 switch 具体更新了什么的。
如果 nixpkgs 和 home-manager 的 repo 有新的 commit 我们是可以知道 hash 更新,但是 2 次 commit 之间更新的 pkg 我们是不知道的。
有什么方式,能让我们看到更新前后 2 个 generation 的 diff 吗?

可以使用 nvd1 这个 pkg,它支持对 2 个 generation 做 diff,比如:

1
2
3
4
5
❯ nvd diff /run/booted-system /run/current-system
<<< /run/booted-system
>>> /run/current-system
No version or selection state changes.
Closure size: 1144 -> 1144 (10 paths added, 10 paths removed, delta +0, disk usage +1.3KiB).

同样的方式,我们就可以对任意 generation 做 diff 了:

1
2
3
4
5
6
7
8
9
❯ ls -al /nix/var/nix/profiles
total 52
drwxr-xr-x 3 root root 4096 Apr 27 20:54 .
drwxr-xr-x 9 root root 4096 Apr 27 20:15 ..
lrwxrwxrwx 1 root root   43 Apr  4 00:00 default -> /nix/var/nix/profiles/per-user/root/profile
drwxr-xr-x 3 root root 4096 Apr  3 17:25 per-user
lrwxrwxrwx 1 root root   15 Apr 27 20:54 system -> system-166-link
lrwxrwxrwx 1 root root   85 Apr 27 20:53 system-165-link -> /nix/store/b66ijf8hscs19bz2d8ph6baxgn6p6ff7-nixos-system-azure-25.05.20250424.f771eb4
lrwxrwxrwx 1 root root   85 Apr 27 20:54 system-166-link -> /nix/store/k57fs0isb02siaaj1vyvjfgsxz882jp1-nixos-system-azure-25.05.20250424.f771eb4

但是这样其实很不方便,我们并不想每次来到这个目录下,找到最后的 2 个 generation 来手动 diff。

正好每次 NixOS 更新也是 2 个单独的命令,写个脚本一起封装了吧:

1
2
3
4
5
6
alias nfu="treefmt -q . && nix flake update"
alias nrs="sudo nixos-rebuild switch"
function ns() {
    local curVer=$(realpath /run/current-system)
    nfu && nrs && nvd diff $curVer $(realpath /run/current-system)
}
  1. 记录 switch 之前的 system 对应的 generation
  2. 对 nixos config 目录下所有 .nix 文件做格式化
  3. 更新 flake
  4. rebuild switch
  5. nvd 做 diff

为什么要做临时变量记录 switch 之前的 path 呢?
因为我们永远可以通过 realpath /run/current-system 找到当前的 path,但是找不到 switch 之前的上一个 path。


我们给 environment.systemPackages 添加 neovim 试试看:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
❯ ns
building the system configuration...
activating the configuration...
setting up /etc...
reloading user units for davinci42...
restarting sysinit-reactivation.target
reloading the following units: dbus.service
the following new units were started: NetworkManager-dispatcher.service
Done. The new configuration is /nix/store/dc1j0mljl3dvlcn1qlk0qfjfs46kzs12-nixos-system-azure-25.05.20250424.f771eb4
<<< /nix/store/abccik16826yrr4jgys1mhmkx3qbwp6a-nixos-system-azure-25.05.20250424.f771eb4
>>> /nix/store/dc1j0mljl3dvlcn1qlk0qfjfs46kzs12-nixos-system-azure-25.05.20250424.f771eb4
Version changes:
[C.]  #1  python3  3.12.9 x2 -> 3.12.9 x2, 3.12.9-env
Added packages:
[A.]  #01  c-grammar-neovim                0.11.0
[A.]  #02  libluv                          1.48.0-2
[A.]  #03  lua-grammar-neovim              0.11.0
[A.]  #04  luajit                          2.1.1741730670, 2.1.1741730670-env x2
[A.]  #05  luajit2.1-lpeg                  1.1.0-2
[A.]  #06  luajit2.1-luabitop              1.0.2-3
[A.]  #07  luajit2.1-mpack                 1.0.12-0
[A.]  #08  markdown-grammar-neovim         0.11.0
[A.]  #09  markdown_inline-grammar-neovim  0.11.0
[A+]  #10  neovim                          0.11.0
[A.]  #11  neovim-unwrapped                0.11.0
[A.]  #12  python3.12-greenlet             3.1.1
[A.]  #13  python3.12-msgpack              1.1.0
[A.]  #14  python3.12-pynvim               0.5.2
[A.]  #15  query-grammar-neovim            0.11.0
[A.]  #16  tree-sitter                     0.25.3
[A.]  #17  unibilium                       2.1.2
[A.]  #18  utf8proc                        2.10.0
[A.]  #19  vim-grammar-neovim              0.11.0
[A.]  #20  vimdoc-grammar-neovim           0.11.0
Closure size: 1145 -> 1168 (34 paths added, 11 paths removed, delta +23, disk usage +49.0MiB).

很完美!但是这么做具体有什么意义呢?

也没什么,看到 kernel 更新会记得重启一下吧。