1{ lib, ... }:
2{
3 name = "earlyoom";
4 meta.maintainers = with lib.maintainers; [
5 ncfavier
6 oxalica
7 ];
8
9 nodes.machine =
10 { pkgs, ... }:
11 {
12 # Limit VM resource usage.
13 virtualisation.memorySize = 1024;
14
15 services.earlyoom = {
16 enable = true;
17 # Use SIGKILL, or `tail` will catch SIGTERM and exit successfully.
18 freeMemKillThreshold = 90;
19 };
20
21 systemd.services.testbloat = {
22 description = "Create a lot of memory pressure";
23 serviceConfig = {
24 ExecStart = "${pkgs.coreutils}/bin/tail /dev/zero";
25 };
26 };
27 };
28
29 testScript = ''
30 machine.wait_for_unit("earlyoom.service")
31
32 with subtest("earlyoom should kill the bad service"):
33 machine.fail("systemctl start --wait testbloat.service")
34 assert machine.get_unit_info("testbloat.service")["Result"] == "signal"
35 output = machine.succeed('journalctl -u earlyoom.service -b0')
36 assert 'low memory! at or below SIGKILL limits' in output
37 '';
38}