home-manager/programs/borgmatic: init replacement module

This commit is contained in:
Gabriel Arazas 2024-09-07 21:59:26 +08:00
parent 86157bc1a2
commit d6a634f50a
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
5 changed files with 147 additions and 0 deletions

View File

@ -5,6 +5,7 @@
./programs/pipewire.nix ./programs/pipewire.nix
./programs/pop-launcher.nix ./programs/pop-launcher.nix
./programs/zed-editor.nix ./programs/zed-editor.nix
./programs/borgmatic.nix
./services/archivebox.nix ./services/archivebox.nix
./services/bleachbit.nix ./services/bleachbit.nix
./services/distant.nix ./services/distant.nix

View File

@ -0,0 +1,117 @@
# A replacement module for the Borgmatic home-manager module. It is quite
# limited and also feels janky to use.
{ config, lib, pkgs, ... }:
let
cfg = config.programs.borgmatic;
settingsFormat = pkgs.formats.yaml { };
borgmaticBackupsModule = { name, lib, ... }: {
options = {
settings = lib.mkOption {
type = settingsFormat.type;
default = { };
example = lib.literalExpression ''
{
source_directories = [
"''${config.xdg.configHome}"
"''${config.xdg.userDirs.extraConfig.XDG_PROJECTS_DIR}"
"''${config.home.homeDirectory}/.thunderbird"
"''${config.home.homeDirectory}/Zotero"
];
repositories = [
{
path = "ssh://k8pDxu32@k8pDxu32.repo.borgbase.com/./repo";
label = "borgbase";
}
{
path = "/var/lib/backups/local.borg";
label = "local";
}
];
keep_daily = 7;
keep_weekly = 4;
keep_monthly = 6;
checks = [
{ name = "repository"; }
{ name = "archives"; frequency = "2 weeks"; }
];
}
'';
};
validateConfig =
lib.mkEnableOption "validation step for the resulting configuration" // {
default = true;
};
};
};
mkBorgmaticConfig = n: v:
lib.nameValuePair "borgmatic.d/${n}.yaml" {
source = let
settingsFile = settingsFormat.generate "borgmatic-config-${n}" v.settings;
borgmaticValidateCmd =
if lib.versionOlder cfg.package.version "1.7.15" then
"borgmatic config validate --config ${settingsFile}"
else
"validate-borgmatic-config --config ${settingsFile}";
in
if v.validateConfig then
pkgs.runCommand "generate-borgmatic-config-with-validation" {
buildInputs = [ cfg.package ];
preferLocalBuild = true;
} ''
${borgmaticValidateCmd} && install ${settingsFile} $out
''
else
settingsFile;
};
in
{
disabledModules = [ "programs/borgmatic.nix" ];
options.programs.borgmatic = {
enable = lib.mkEnableOption "configuring Borg backups with Borgmatic";
package = lib.mkPackageOption pkgs "borgmatic" { };
backups = lib.mkOption {
type = with lib.types; attrsOf (submodule borgmaticBackupsModule);
default = { };
example = lib.literalExpression ''
{
personal = {
validateConfig = true;
settings = {
source_directories = [
config.xdg.configHome
config.xdg.userDirs.documents
config.xdg.userDirs.photos
];
repositories = lib.singleton {
path = "ssh://alskdjalskdjalsdkj";
label = "remote-hetzner-box";
};
keep_daily = 7;
keep_weekly = 6;
keep_monthly = 6;
}
};
}
'';
};
};
config = lib.mkIf cfg.enable {
home.packages = [ cfg.package ];
xdg.configFile = lib.mapAttrs' mkBorgmaticConfig cfg.backups;
};
}

View File

@ -51,7 +51,9 @@ in
import nmt { import nmt {
inherit pkgs lib modules; inherit pkgs lib modules;
testedAttrPath = [ "home" "activationPackage" ]; testedAttrPath = [ "home" "activationPackage" ];
# TODO: Fix nmt to accept specialArgs or something.
tests = builtins.foldl' (a: b: a // (import b)) { } ([ tests = builtins.foldl' (a: b: a // (import b)) { } ([
#./programs/borgmatic
./programs/neovide ./programs/neovide
./programs/pipewire ./programs/pipewire
./programs/pop-launcher ./programs/pop-launcher

View File

@ -0,0 +1,23 @@
{ config, lib, pkgs, ... }:
{
programs.borgmatic = {
enable = true;
backups = {
personal.settings = {
hello = "WORLD";
};
bizness.settings = {
hello = "MONEY";
};
};
};
test.stubs.borgmatic = { };
nmt.script = ''
assertFileExists home-files/.config/borgmatic.d/personal.yaml
assertFileExists home-files/.config/borgmatic.d/bizness.yaml
'';
}

View File

@ -0,0 +1,4 @@
{
borgmatic-basic = ./basic.nix;
borgmatic-multiple-configurations = ./multiple-configurations.nix;
}