Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 55 additions & 1 deletion nixos-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ in
};
};
};

storage-clear = {
enable = lib.mkEnableOption "Enable clearing NEAR validator storage automatically when free storage falls bellow a certain threshold.";

min-free = lib.mkOption {
type = lib.types.str;
default = "15G";
example = "500M";
description = ''
Free storage threshold to trigger the storage clear when bellow.
'';
};
};
};
};

Expand Down Expand Up @@ -134,7 +147,6 @@ in
timerConfig = {
OnUnitInactiveSec = cfg.pinger.schedule.minimum-wait;
RandomizedDelaySec = cfg.pinger.schedule.random-delay;
Unit = "near-validator-pinger.service";
Persistent = true;
};
};
Expand Down Expand Up @@ -173,5 +185,47 @@ in
'';
}
);

systemd.timers.near-validator-storage-clear = lib.mkIf cfg.storage-clear.enable {
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = "daily";
RandomizedOffsetSec = "24h";
Persistent = true;
};
};

systemd.services.near-validator-storage-clear =
let
stateDir = "/var/lib/near-validator";
in
{
description = "Remove NEAR validator storage if free space falls under threshold";
after = [ "near-validator.target" ];
wants = [ "near-validator.target" ];
serviceConfig = {
Type = "oneshot";
};
path = [
config.systemd.package
pkgs.gnused
];
script =
let
dataDir = "${stateDir}/.near/data";
in
''
free=$(df -B1 --output=avail "${dataDir}" | tail -1)
threshold=$(( $(echo "${cfg.storage-clear.min-free}" | sed 's/G$/000000000/;s/M$/000000/;s/K$/000/') ))

if [ "$free" -ge "$threshold" ]; then
exit 0
fi

systemctl stop near-validator
rm -rf "${dataDir}"
systemctl start near-validator
'';
};
};
}
Loading