@@ -0,0 +1,96 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ ecfg = config.services.earlyoom;
7+in
8+{
9+ options = {
10+ services.earlyoom = {
11+
12+ enable = mkOption {
13+ type = types.bool;
14+ default = false;
15+ description = ''
16+ Enable early out of memory killing.
17+ '';
18+ };
19+
20+ freeMemThreshold = mkOption {
21+ type = types.int;
22+ default = 10;
23+ description = ''
24+ Minimum of availabe memory (in percent).
25+ If the free memory falls below this threshold and the analog is true for
26+ <option>services.earlyoom.freeSwapThreshold</option>
27+ the killing begins.
28+ '';
29+ };
30+
31+ freeSwapThreshold = mkOption {
32+ type = types.int;
33+ default = 10;
34+ description = ''
35+ Minimum of availabe swap space (in percent).
36+ If the available swap space falls below this threshold and the analog
37+ is true for <option>services.earlyoom.freeMemThreshold</option>
38+ the killing begins.
39+ '';
40+ };
41+
42+ useKernelOOMKiller= mkOption {
43+ type = types.bool;
44+ default = false;
45+ description = ''
46+ Use kernel OOM killer instead of own user-space implementation.
47+ '';
48+ };
49+
50+ ignoreOOMScoreAdjust = mkOption {
51+ type = types.bool;
52+ default = false;
53+ description = ''
54+ Ignore oom_score_adjust values of processes.
55+ User-space implementation only.
56+ '';
57+ };
58+
59+ enableDebugInfo = mkOption {
60+ type = types.bool;
61+ default = false;
62+ description = ''
63+ Enable debugging messages.
64+ '';
65+ };
66+ };
67+ };
68+
69+ config = mkIf ecfg.enable {
70+ assertions = [
71+ { assertion = ecfg.freeMemThreshold > 0 && ecfg.freeMemThreshold <= 100;
72+ message = "Needs to be a positive percentage"; }
73+ { assertion = ecfg.freeSwapThreshold > 0 && ecfg.freeSwapThreshold <= 100;
74+ message = "Needs to be a positive percentage"; }
75+ { assertion = !ecfg.useKernelOOMKiller || !ecfg.ignoreOOMScoreAdjust;
76+ message = "Both options in conjunction do not make sense"; }
77+ ];
78+
79+ systemd.services.earlyoom = {
80+ description = "Early OOM Daemon for Linux";
81+ wantedBy = [ "multi-user.target" ];
82+ serviceConfig = {
83+ StandardOutput = "null";
84+ StandardError = "syslog";
85+ ExecStart = ''
86+ ${pkgs.earlyoom}/bin/earlyoom \
87+ -m ${toString ecfg.freeMemThreshold} \
88+ -s ${toString ecfg.freeSwapThreshold} \
89+ ${optionalString ecfg.useKernelOOMKiller "-k"} \
90+ ${optionalString ecfg.ignoreOOMScoreAdjust "-i"} \
91+ ${optionalString ecfg.enableDebugInfo "-d"}
92+ '';
93+ };
94+ };
95+ };
96+}