2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
2022-03-30 09:31:56 +00:00
|
|
|
cfg = config.services.earlyoom;
|
|
|
|
|
|
|
|
inherit (lib)
|
2024-04-21 15:54:59 +00:00
|
|
|
concatStringsSep
|
|
|
|
escapeShellArg
|
|
|
|
literalExpression
|
|
|
|
mkDefault
|
|
|
|
mkEnableOption
|
|
|
|
mkIf
|
|
|
|
mkOption
|
|
|
|
mkPackageOption
|
|
|
|
mkRemovedOptionModule
|
|
|
|
optionalString
|
|
|
|
optionals
|
|
|
|
types;
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
|
|
|
{
|
2024-04-21 15:54:59 +00:00
|
|
|
meta = {
|
|
|
|
maintainers = with lib.maintainers; [ AndersonTorres ];
|
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
options.services.earlyoom = {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "early out of memory killing";
|
|
|
|
|
|
|
|
package = mkPackageOption pkgs "earlyoom" { };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
freeMemThreshold = mkOption {
|
|
|
|
type = types.ints.between 1 100;
|
|
|
|
default = 10;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
Minimum available memory (in percent).
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
If the available memory falls below this threshold (and the analog is true for
|
2022-08-12 12:06:08 +00:00
|
|
|
{option}`freeSwapThreshold`) the killing begins.
|
2022-03-30 09:31:56 +00:00
|
|
|
SIGTERM is sent first to the process that uses the most memory; then, if the available
|
2022-08-12 12:06:08 +00:00
|
|
|
memory falls below {option}`freeMemKillThreshold` (and the analog is true for
|
|
|
|
{option}`freeSwapKillThreshold`), SIGKILL is sent.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
See [README](https://github.com/rfjakob/earlyoom#command-line-options) for details.
|
2022-03-30 09:31:56 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
freeMemKillThreshold = mkOption {
|
|
|
|
type = types.nullOr (types.ints.between 1 100);
|
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
Minimum available memory (in percent) before sending SIGKILL.
|
2022-08-12 12:06:08 +00:00
|
|
|
If unset, this defaults to half of {option}`freeMemThreshold`.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
See the description of [](#opt-services.earlyoom.freeMemThreshold).
|
2022-03-30 09:31:56 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-05-29 06:06:01 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
freeSwapThreshold = mkOption {
|
|
|
|
type = types.ints.between 1 100;
|
|
|
|
default = 10;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
Minimum free swap space (in percent) before sending SIGTERM.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
See the description of [](#opt-services.earlyoom.freeMemThreshold).
|
2022-03-30 09:31:56 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
freeSwapKillThreshold = mkOption {
|
|
|
|
type = types.nullOr (types.ints.between 1 100);
|
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
Minimum free swap space (in percent) before sending SIGKILL.
|
2022-08-12 12:06:08 +00:00
|
|
|
If unset, this defaults to half of {option}`freeSwapThreshold`.
|
2022-03-30 09:31:56 +00:00
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
See the description of [](#opt-services.earlyoom.freeMemThreshold).
|
2022-03-30 09:31:56 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableDebugInfo = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
Enable debugging messages.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableNotifications = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
Send notifications about killed processes via the system d-bus.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
To actually see the notifications in your GUI session, you need to have
|
2022-09-09 14:08:57 +00:00
|
|
|
`systembus-notify` running as your user, which this
|
|
|
|
option handles by enabling {option}`services.systembus-notify`.
|
2022-03-30 09:31:56 +00:00
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
See [README](https://github.com/rfjakob/earlyoom#notifications) for details.
|
2022-03-30 09:31:56 +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
|
|
|
|
'''
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
An absolute path to an executable to be run for each process killed.
|
|
|
|
Some environment variables are available, see
|
2022-08-12 12:06:08 +00: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-30 09:31:56 +00:00
|
|
|
for details.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
reportInterval = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 3600;
|
|
|
|
example = 0;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Interval (in seconds) at which a memory report is printed (set to 0 to disable).";
|
2022-03-30 09:31:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extraArgs = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "-g" "--prefer '(^|/)(java|chromium)$'" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Extra command-line arguments to be passed to earlyoom.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-03-05 16:20:37 +00:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "earlyoom" "useKernelOOMKiller" ] ''
|
|
|
|
This option is deprecated and ignored by earlyoom since 1.2.
|
|
|
|
'')
|
2022-03-30 09:31:56 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "earlyoom" "notificationsCommand" ] ''
|
|
|
|
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.
|
|
|
|
'')
|
2022-03-05 16:20:37 +00:00
|
|
|
];
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
services.systembus-notify.enable = mkDefault cfg.enableNotifications;
|
2020-05-29 06:06:01 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
systemd.services.earlyoom = {
|
|
|
|
description = "Early OOM Daemon for Linux";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
path = optionals cfg.enableNotifications [ pkgs.dbus ];
|
2020-04-24 23:36:52 +00:00
|
|
|
serviceConfig = {
|
2021-10-06 13:57:05 +00:00
|
|
|
StandardError = "journal";
|
2022-03-05 16:20:37 +00:00
|
|
|
ExecStart = concatStringsSep " " ([
|
2024-04-21 15:54:59 +00:00
|
|
|
"${lib.getExe cfg.package}"
|
2022-03-30 09:31:56 +00:00
|
|
|
("-m ${toString cfg.freeMemThreshold}"
|
2024-04-21 15:54:59 +00:00
|
|
|
+ optionalString (cfg.freeMemKillThreshold != null) ",${toString cfg.freeMemKillThreshold}")
|
2022-03-30 09:31:56 +00:00
|
|
|
("-s ${toString cfg.freeSwapThreshold}"
|
2024-04-21 15:54:59 +00:00
|
|
|
+ optionalString (cfg.freeSwapKillThreshold != null) ",${toString cfg.freeSwapKillThreshold}")
|
2022-03-30 09:31:56 +00:00
|
|
|
"-r ${toString cfg.reportInterval}"
|
|
|
|
]
|
2024-04-21 15:54:59 +00:00
|
|
|
++ optionals cfg.enableDebugInfo [ "-d" ]
|
|
|
|
++ optionals cfg.enableNotifications [ "-n" ]
|
|
|
|
++ optionals (cfg.killHook != null) [ "-N ${escapeShellArg cfg.killHook}" ]
|
|
|
|
++ cfg.extraArgs);
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|