1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 crashdump = config.boot.crashDump;
9
10 kernelParams = lib.concatStringsSep " " crashdump.kernelParams;
11
12in
13###### interface
14{
15 options = {
16 boot = {
17 crashDump = {
18 enable = lib.mkOption {
19 type = lib.types.bool;
20 default = false;
21 description = ''
22 If enabled, NixOS will set up a kernel that will
23 boot on crash, and leave the user in systemd rescue
24 to be able to save the crashed kernel dump at
25 /proc/vmcore.
26 It also activates the NMI watchdog.
27 '';
28 };
29 reservedMemory = lib.mkOption {
30 default = "128M";
31 type = lib.types.str;
32 description = ''
33 The amount of memory reserved for the crashdump kernel.
34 If you choose a too high value, dmesg will mention
35 "crashkernel reservation failed".
36 '';
37 };
38 kernelParams = lib.mkOption {
39 type = lib.types.listOf lib.types.str;
40 default = [
41 "1"
42 "boot.shell_on_fail"
43 ];
44 description = ''
45 Parameters that will be passed to the kernel kexec-ed on crash.
46 '';
47 };
48 };
49 };
50 };
51
52 ###### implementation
53
54 config = lib.mkIf crashdump.enable {
55 boot = {
56 postBootCommands = ''
57 echo "loading crashdump kernel...";
58 ${pkgs.kexec-tools}/sbin/kexec -p /run/current-system/kernel \
59 --initrd=/run/current-system/initrd \
60 --reset-vga --console-vga \
61 --command-line="init=$(readlink -f /run/current-system/init) irqpoll maxcpus=1 reset_devices ${kernelParams}"
62 '';
63 kernelParams = [
64 "crashkernel=${crashdump.reservedMemory}"
65 "nmi_watchdog=panic"
66 "softlockup_panic=1"
67 ];
68 };
69 };
70}