1{
2 config,
3 lib,
4 pkgs,
5 utils,
6 ...
7}:
8
9with lib;
10
11let
12 cfg = config.systemd.coredump;
13 systemd = config.systemd.package;
14in
15{
16 options = {
17 systemd.coredump.enable = mkOption {
18 default = true;
19 type = types.bool;
20 description = ''
21 Whether core dumps should be processed by
22 {command}`systemd-coredump`. If disabled, core dumps
23 appear in the current directory of the crashing process.
24 '';
25 };
26
27 systemd.coredump.extraConfig = mkOption {
28 default = "";
29 type = types.lines;
30 example = "Storage=journal";
31 description = ''
32 Extra config options for systemd-coredump. See {manpage}`coredump.conf(5)` man page
33 for available options.
34 '';
35 };
36 };
37
38 config = mkMerge [
39
40 (mkIf cfg.enable {
41 systemd.additionalUpstreamSystemUnits = [
42 "systemd-coredump.socket"
43 "systemd-coredump@.service"
44 ];
45
46 environment.etc = {
47 "systemd/coredump.conf".text = ''
48 [Coredump]
49 ${cfg.extraConfig}
50 '';
51
52 # install provided sysctl snippets
53 "sysctl.d/50-coredump.conf".source =
54 # Fix systemd-coredump error caused by truncation of `kernel.core_pattern`
55 # when the `systemd` derivation name is too long. This works by substituting
56 # the path to `systemd` with a symlink that has a constant-length path.
57 #
58 # See: https://github.com/NixOS/nixpkgs/issues/213408
59 pkgs.substitute {
60 src = "${systemd}/example/sysctl.d/50-coredump.conf";
61 substitutions = [
62 "--replace-fail"
63 "${systemd}"
64 "${pkgs.symlinkJoin {
65 name = "systemd";
66 paths = [ systemd ];
67 }}"
68 ];
69 };
70
71 "sysctl.d/50-default.conf".source = "${systemd}/example/sysctl.d/50-default.conf";
72 };
73
74 users.users.systemd-coredump = {
75 uid = config.ids.uids.systemd-coredump;
76 group = "systemd-coredump";
77 };
78 users.groups.systemd-coredump = { };
79 })
80
81 (mkIf (!cfg.enable) {
82 boot.kernel.sysctl."kernel.core_pattern" = mkDefault "core";
83 })
84
85 ];
86
87}