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