9 cfg = config.services.earlyoom;
26 maintainers = with lib.maintainers; [ AndersonTorres ];
29 options.services.earlyoom = {
30 enable = mkEnableOption "early out of memory killing";
32 package = mkPackageOption pkgs "earlyoom" { };
34 freeMemThreshold = mkOption {
35 type = types.ints.between 1 100;
38 Minimum available memory (in percent).
40 If the available memory falls below this threshold (and the analog is true for
41 {option}`freeSwapThreshold`) the killing begins.
42 SIGTERM is sent first to the process that uses the most memory; then, if the available
43 memory falls below {option}`freeMemKillThreshold` (and the analog is true for
44 {option}`freeSwapKillThreshold`), SIGKILL is sent.
46 See [README](https://github.com/rfjakob/earlyoom#command-line-options) for details.
50 freeMemKillThreshold = mkOption {
51 type = types.nullOr (types.ints.between 1 100);
54 Minimum available memory (in percent) before sending SIGKILL.
55 If unset, this defaults to half of {option}`freeMemThreshold`.
57 See the description of [](#opt-services.earlyoom.freeMemThreshold).
61 freeSwapThreshold = mkOption {
62 type = types.ints.between 1 100;
65 Minimum free swap space (in percent) before sending SIGTERM.
67 See the description of [](#opt-services.earlyoom.freeMemThreshold).
71 freeSwapKillThreshold = mkOption {
72 type = types.nullOr (types.ints.between 1 100);
75 Minimum free swap space (in percent) before sending SIGKILL.
76 If unset, this defaults to half of {option}`freeSwapThreshold`.
78 See the description of [](#opt-services.earlyoom.freeMemThreshold).
82 enableDebugInfo = mkOption {
86 Enable debugging messages.
90 enableNotifications = mkOption {
94 Send notifications about killed processes via the system d-bus.
96 WARNING: enabling this option (while convenient) should *not* be done on a
97 machine where you do not trust the other users as it allows any other
98 local user to DoS your session by spamming notifications.
100 To actually see the notifications in your GUI session, you need to have
101 `systembus-notify` running as your user, which this
102 option handles by enabling {option}`services.systembus-notify`.
104 See [README](https://github.com/rfjakob/earlyoom#notifications) for details.
108 killHook = mkOption {
109 type = types.nullOr types.path;
111 example = literalExpression ''
112 pkgs.writeShellScript "earlyoom-kill-hook" '''
113 echo "Process $EARLYOOM_NAME ($EARLYOOM_PID) was killed" >> /path/to/log
117 An absolute path to an executable to be run for each process killed.
118 Some environment variables are available, see
119 [README](https://github.com/rfjakob/earlyoom#notifications) and
120 [the man page](https://github.com/rfjakob/earlyoom/blob/master/MANPAGE.md#-n-pathtoscript)
123 WARNING: earlyoom is running in a sandbox with ProtectSystem="strict"
124 by default, so filesystem write is also prohibited for the hook.
125 If you want to change these protection rules, override the systemd
126 service via `systemd.services.earlyoom.serviceConfig.ProtectSystem`.
130 reportInterval = mkOption {
134 description = "Interval (in seconds) at which a memory report is printed (set to 0 to disable).";
137 extraArgs = mkOption {
138 type = types.listOf types.str;
143 "(^|/)(java|chromium)$"
146 Extra command-line arguments to be passed to earlyoom. Each element in
147 the value list will be escaped as an argument without further
154 (mkRemovedOptionModule [ "services" "earlyoom" "useKernelOOMKiller" ] ''
155 This option is deprecated and ignored by earlyoom since 1.2.
157 (mkRemovedOptionModule [ "services" "earlyoom" "notificationsCommand" ] ''
158 This option was removed in earlyoom 1.6, but was reimplemented in 1.7
159 and is available as the new option `services.earlyoom.killHook`.
161 (mkRemovedOptionModule [ "services" "earlyoom" "ignoreOOMScoreAdjust" ] ''
162 This option is deprecated and ignored by earlyoom since 1.7.
166 config = mkIf cfg.enable {
167 services.systembus-notify.enable = mkDefault cfg.enableNotifications;
169 systemd.packages = [ cfg.package ];
171 systemd.services.earlyoom = {
172 overrideStrategy = "asDropin";
174 wantedBy = [ "multi-user.target" ];
175 path = optionals cfg.enableNotifications [ pkgs.dbus ];
177 # We setup `EARLYOOM_ARGS` via drop-ins, so disable the default import
178 # from /etc/default/earlyoom.
179 serviceConfig.EnvironmentFile = "";
181 environment.EARLYOOM_ARGS =
182 lib.cli.toGNUCommandLineShell { } {
184 "${toString cfg.freeMemThreshold}"
185 + optionalString (cfg.freeMemKillThreshold != null) ",${toString cfg.freeMemKillThreshold}";
187 "${toString cfg.freeSwapThreshold}"
188 + optionalString (cfg.freeSwapKillThreshold != null) ",${toString cfg.freeSwapKillThreshold}";
189 r = "${toString cfg.reportInterval}";
190 d = cfg.enableDebugInfo;
191 n = cfg.enableNotifications;
192 N = if cfg.killHook != null then cfg.killHook else null;
195 + lib.escapeShellArgs cfg.extraArgs;