2017-03-24 22:16:16 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
2021-07-12 08:36:22 +01:00
|
|
|
cfg = config.services.earlyoom;
|
|
|
|
|
|
|
|
inherit (lib)
|
|
|
|
mkDefault mkEnableOption mkIf mkOption types
|
2022-03-24 07:34:09 +00:00
|
|
|
mkRemovedOptionModule literalExpression
|
|
|
|
escapeShellArg concatStringsSep optional optionalString;
|
2021-07-12 08:36:22 +01:00
|
|
|
|
2017-03-24 22:16:16 +00:00
|
|
|
in
|
|
|
|
{
|
2021-07-12 08:36:22 +01:00
|
|
|
options.services.earlyoom = {
|
2022-08-28 20:18:44 +01:00
|
|
|
enable = mkEnableOption (lib.mdDoc "Early out of memory killing");
|
2017-03-24 22:16:16 +00:00
|
|
|
|
2021-07-12 08:36:22 +01:00
|
|
|
freeMemThreshold = mkOption {
|
|
|
|
type = types.ints.between 1 100;
|
|
|
|
default = 10;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2022-03-24 07:34:09 +00:00
|
|
|
Minimum available memory (in percent).
|
|
|
|
|
|
|
|
If the available memory falls below this threshold (and the analog is true for
|
2022-07-28 22:19:15 +01:00
|
|
|
{option}`freeSwapThreshold`) the killing begins.
|
2022-03-24 07:34:09 +00:00
|
|
|
SIGTERM is sent first to the process that uses the most memory; then, if the available
|
2022-07-28 22:19:15 +01:00
|
|
|
memory falls below {option}`freeMemKillThreshold` (and the analog is true for
|
|
|
|
{option}`freeSwapKillThreshold`), SIGKILL is sent.
|
2022-03-24 07:34:09 +00:00
|
|
|
|
2022-07-28 22:19:15 +01:00
|
|
|
See [README](https://github.com/rfjakob/earlyoom#command-line-options) for details.
|
2022-03-24 07:34:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
freeMemKillThreshold = mkOption {
|
|
|
|
type = types.nullOr (types.ints.between 1 100);
|
|
|
|
default = null;
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''
|
2022-03-24 07:34:09 +00:00
|
|
|
Minimum available memory (in percent) before sending SIGKILL.
|
2022-08-03 21:46:41 +01:00
|
|
|
If unset, this defaults to half of {option}`freeMemThreshold`.
|
2022-03-24 07:34:09 +00:00
|
|
|
|
2022-08-03 21:46:41 +01:00
|
|
|
See the description of [](#opt-services.earlyoom.freeMemThreshold).
|
2021-07-12 08:36:22 +01:00
|
|
|
'';
|
|
|
|
};
|
2017-03-24 22:16:16 +00:00
|
|
|
|
2021-07-12 08:36:22 +01:00
|
|
|
freeSwapThreshold = mkOption {
|
|
|
|
type = types.ints.between 1 100;
|
|
|
|
default = 10;
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''
|
2022-03-24 07:34:09 +00:00
|
|
|
Minimum free swap space (in percent) before sending SIGTERM.
|
|
|
|
|
2022-08-03 21:46:41 +01:00
|
|
|
See the description of [](#opt-services.earlyoom.freeMemThreshold).
|
2021-07-12 08:36:22 +01:00
|
|
|
'';
|
|
|
|
};
|
2017-03-24 22:16:16 +00:00
|
|
|
|
2022-03-24 07:34:09 +00:00
|
|
|
freeSwapKillThreshold = mkOption {
|
|
|
|
type = types.nullOr (types.ints.between 1 100);
|
|
|
|
default = null;
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''
|
2022-03-24 07:34:09 +00:00
|
|
|
Minimum free swap space (in percent) before sending SIGKILL.
|
2022-08-03 21:46:41 +01:00
|
|
|
If unset, this defaults to half of {option}`freeSwapThreshold`.
|
2022-03-24 07:34:09 +00:00
|
|
|
|
2022-08-03 21:46:41 +01:00
|
|
|
See the description of [](#opt-services.earlyoom.freeMemThreshold).
|
2021-07-12 08:36:22 +01:00
|
|
|
'';
|
|
|
|
};
|
2017-03-24 22:16:16 +00:00
|
|
|
|
2021-07-12 08:36:22 +01:00
|
|
|
enableDebugInfo = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-07-12 08:36:22 +01:00
|
|
|
Enable debugging messages.
|
|
|
|
'';
|
|
|
|
};
|
2017-03-24 22:16:16 +00:00
|
|
|
|
2021-07-12 08:36:22 +01:00
|
|
|
enableNotifications = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-29 20:48:56 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-07-12 08:36:22 +01:00
|
|
|
Send notifications about killed processes via the system d-bus.
|
2019-01-05 12:26:12 +00:00
|
|
|
|
2021-07-12 08:36:22 +01:00
|
|
|
WARNING: enabling this option (while convenient) should *not* be done on a
|
|
|
|
machine where you do not trust the other users as it allows any other
|
|
|
|
local user to DoS your session by spamming notifications.
|
2020-05-25 16:13:55 +01:00
|
|
|
|
2021-07-12 08:36:22 +01:00
|
|
|
To actually see the notifications in your GUI session, you need to have
|
2022-08-29 20:48:56 +01:00
|
|
|
`systembus-notify` running as your user, which this
|
|
|
|
option handles by enabling {option}`services.systembus-notify`.
|
2019-01-05 12:26:12 +00:00
|
|
|
|
2022-08-29 20:48:56 +01:00
|
|
|
See [README](https://github.com/rfjakob/earlyoom#notifications) for details.
|
2021-07-12 08:36:22 +01:00
|
|
|
'';
|
2017-03-24 22:16:16 +00:00
|
|
|
};
|
2022-03-24 07:34:09 +00:00
|
|
|
|
|
|
|
killHook = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = literalExpression ''
|
|
|
|
pkgs.writeShellScript "earlyoom-kill-hook" '''
|
|
|
|
echo "Process $EARLYOOM_NAME ($EARLYOOM_PID) was killed" >> /path/to/log
|
|
|
|
'''
|
|
|
|
'';
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2022-03-24 07:34:09 +00:00
|
|
|
An absolute path to an executable to be run for each process killed.
|
|
|
|
Some environment variables are available, see
|
2022-07-28 22:19:15 +01:00
|
|
|
[README](https://github.com/rfjakob/earlyoom#notifications) and
|
|
|
|
[the man page](https://github.com/rfjakob/earlyoom/blob/master/MANPAGE.md#-n-pathtoscript)
|
2022-03-24 07:34:09 +00:00
|
|
|
for details.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
reportInterval = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 3600;
|
|
|
|
example = 0;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Interval (in seconds) at which a memory report is printed (set to 0 to disable).";
|
2022-03-24 07:34:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extraArgs = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "-g" "--prefer '(^|/)(java|chromium)$'" ];
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Extra command-line arguments to be passed to earlyoom.";
|
2022-03-24 07:34:09 +00:00
|
|
|
};
|
2017-03-24 22:16:16 +00:00
|
|
|
};
|
|
|
|
|
2022-02-13 15:25:24 +00:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "earlyoom" "useKernelOOMKiller" ] ''
|
|
|
|
This option is deprecated and ignored by earlyoom since 1.2.
|
|
|
|
'')
|
2021-07-12 08:36:22 +01:00
|
|
|
(mkRemovedOptionModule [ "services" "earlyoom" "notificationsCommand" ] ''
|
2022-03-24 07:34:09 +00:00
|
|
|
This option was removed in earlyoom 1.6, but was reimplemented in 1.7
|
|
|
|
and is available as the new option `services.earlyoom.killHook`.
|
|
|
|
'')
|
|
|
|
(mkRemovedOptionModule [ "services" "earlyoom" "ignoreOOMScoreAdjust" ] ''
|
|
|
|
This option is deprecated and ignored by earlyoom since 1.7.
|
2021-07-12 08:36:22 +01:00
|
|
|
'')
|
2022-02-13 15:25:24 +00:00
|
|
|
];
|
|
|
|
|
2021-07-12 08:36:22 +01:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
services.systembus-notify.enable = mkDefault cfg.enableNotifications;
|
2020-05-25 16:13:55 +01:00
|
|
|
|
2017-03-24 22:16:16 +00:00
|
|
|
systemd.services.earlyoom = {
|
|
|
|
description = "Early OOM Daemon for Linux";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-07-12 08:36:22 +01:00
|
|
|
path = optional cfg.enableNotifications pkgs.dbus;
|
2017-03-24 22:16:16 +00:00
|
|
|
serviceConfig = {
|
2021-10-05 10:17:09 +01:00
|
|
|
StandardError = "journal";
|
2022-02-13 15:25:24 +00:00
|
|
|
ExecStart = concatStringsSep " " ([
|
|
|
|
"${pkgs.earlyoom}/bin/earlyoom"
|
2022-03-24 07:34:09 +00:00
|
|
|
("-m ${toString cfg.freeMemThreshold}"
|
|
|
|
+ optionalString (cfg.freeMemKillThreshold != null) ",${toString cfg.freeMemKillThreshold}")
|
|
|
|
("-s ${toString cfg.freeSwapThreshold}"
|
|
|
|
+ optionalString (cfg.freeSwapKillThreshold != null) ",${toString cfg.freeSwapKillThreshold}")
|
|
|
|
"-r ${toString cfg.reportInterval}"
|
2021-07-12 08:36:22 +01:00
|
|
|
]
|
|
|
|
++ optional cfg.enableDebugInfo "-d"
|
|
|
|
++ optional cfg.enableNotifications "-n"
|
2022-03-24 07:34:09 +00:00
|
|
|
++ optional (cfg.killHook != null) "-N ${escapeShellArg cfg.killHook}"
|
|
|
|
++ cfg.extraArgs
|
2021-07-12 08:36:22 +01:00
|
|
|
);
|
2017-03-24 22:16:16 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|