trying out some new tool commands for easier immpermanence with bcache and my setup

This commit is contained in:
RingOfStorms (Joshua Bell) 2025-12-17 13:39:39 -06:00
parent 03487772ac
commit 1d0cb5e08f
6 changed files with 487 additions and 54 deletions

View file

@ -0,0 +1,67 @@
{ config, lib, pkgs, ... }:
let
cfg = config.impermanence.tools;
bcacheImpermanenceBin = pkgs.writeShellScriptBin "bcache-impermanence" (
builtins.readFile ./impermanence-tools.sh
);
in
{
options.impermanence.tools = {
enable = lib.mkEnableOption "bcachefs impermanence tools (GC + CLI)";
snapshotRoot = lib.mkOption {
type = lib.types.str;
default = "/.snapshots/old_roots";
description = "Root directory containing old root snapshots.";
};
gc = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Enable garbage collection of old root snapshots.";
};
keepPerMonth = lib.mkOption {
type = lib.types.int;
default = 1;
description = "Keep at least this many snapshots per calendar month (latest ones).";
};
keepRecentWeeks = lib.mkOption {
type = lib.types.int;
default = 4;
description = "Keep at least one snapshot per ISO week within this many recent weeks.";
};
keepRecentCount = lib.mkOption {
type = lib.types.int;
default = 5;
description = "Always keep at least this many most recent snapshots overall.";
};
};
};
config = lib.mkIf cfg.enable {
environment.systemPackages = [ bcacheImpermanenceBin ];
systemd.services."bcache-impermanence-gc" = lib.mkIf cfg.gc.enable {
description = "Garbage collect bcachefs impermanence snapshots";
wantedBy = [ "multi-user.target" ];
after = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
Environment = "PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.findutils pkgs.diffutils pkgs.bcachefs-tools ]}:/run/current-system/sw/bin";
};
script = ''
exec ${bcacheImpermanenceBin}/bin/bcache-impermanence gc \
--snapshot-root ${cfg.snapshotRoot} \
--keep-per-month ${toString cfg.gc.keepPerMonth} \
--keep-recent-weeks ${toString cfg.gc.keepRecentWeeks} \
--keep-recent-count ${toString cfg.gc.keepRecentCount}
'';
};
};
}