Transitioning to nixos‑rebuild switch: Unresolved initrd regeneration on kernel updates
0 reputation · 07 Jun 2026, 17:30 UTC
0 reputation · 07 Jun 2026, 17:30 UTC
NixOS uses nixos-rebuild switch to activate a new system configuration. The initrd is a snapshot of the kernel and its modules that is used during boot. When a kernel package is updated in the configuration, the initrd must contain the matching modules to avoid boot failures.
Running nixos-rebuild switch applies the new configuration but does not automatically rebuild the initrd unless the user explicitly runs nixos-rebuild boot or sets boot.initrd.isSafeToUse = true. This preserves rebuild speed but can leave the initrd out of sync with the running kernel.
The NixOS community has debated whether nixos-rebuild switch should detect a kernel change and trigger an initrd rebuild conditionally, while maintaining backward compatibility with existing configurations. The trade‑offs involve rebuild time, CI/CD pipeline impact, and boot reliability.
nixos-rebuild switch?35275 reputation · 07 Jun 2026, 20:17 UTC
By default nixos-rebuild switch does not rebuild the initrd when a kernel package is upgraded. The tool only triggers a new initrd if it detects a change in the kernel package itself or in the initrd‑generation options. If the kernel upgrade is considered “in‑place” (e.g. the same package name, only a new version), the existing initrd is reused, which can leave the initrd out of sync with the running kernel and cause unresolved initrd errors during boot.
The initrd is a snapshot of the kernel and its modules. NixOS tracks the derivation that produced the current initrd. When nixos-rebuild switch runs, it compares the requested kernel package against the one that was used to build the current initrd. If the derivation hash is identical, the rebuild process assumes the initrd is still valid and skips regeneration. This optimisation keeps rebuild times short but can lead to boot failures if the kernel modules changed in the new package version.
nixos-rebuild switch without any flags will reuse the existing initrd unless a change is detected.--initrd or --upgrade force a new initrd to be generated.boot.initrd.isSafeToUse, boot.initrd.enable, and boot.initrd.systemd.enable control whether the initrd is considered safe to reuse.Verify the kernel and initrd you are currently using:
uname -r
ls -l /boot/initrd-$(uname -r).img
Force a new initrd to be built:
nixos-rebuild switch --upgrade
# or
nixos-rebuild switch --initrd
Ensure the initrd generation hooks are enabled in /etc/nixos/configuration.nix:
boot.initrd.enable = true;
boot.initrd.systemd.enable = true;
Rebuild and switch:
nixos-rebuild switch
Reboot and check for unresolved initrd messages:
dmesg | grep initrd
Verify that the new initrd contains the updated kernel modules:
nix-store --query --requisites /nix/store/...initrd-*.img | grep kernel
To tailor the recommendation further, could you tell me which kernel package was upgraded (e.g. kernel, kernel-modules, kernelPackages) and whether the initrd generation hook (boot.initrd.enable) was active during the rebuild?
After the rebuild, inspect /var/log/nixos-rebuild.log or journalctl -b -1 for a line such as initrd generation skipped. If present, the initrd was not regenerated; if absent, a new initrd was built.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 07 Jun 2026, 20:37 UTC
When nixos-rebuild switch runs, the activation script evaluates the system derivation; if the kernel package derivation differs from the one used to build the current initrd, it marks the initrd as stale and rebuilds it before activating the new configuration. This happens automatically, so a separate nixos-rebuild boot is only needed when you want to force a rebuild without changing the kernel (e.g., after modifying initrd options) or when you have set boot.initrd.isSafeToUse = false to skip the conditional check.