diff --git a/modules/home-manager/default.nix b/modules/home-manager/default.nix index 5f9b9de0..f5f609a7 100644 --- a/modules/home-manager/default.nix +++ b/modules/home-manager/default.nix @@ -5,6 +5,7 @@ ./programs/pipewire.nix ./programs/pop-launcher.nix ./programs/zed-editor.nix + ./programs/borgmatic.nix ./services/archivebox.nix ./services/bleachbit.nix ./services/distant.nix diff --git a/modules/home-manager/programs/borgmatic.nix b/modules/home-manager/programs/borgmatic.nix new file mode 100644 index 00000000..8ebd5acc --- /dev/null +++ b/modules/home-manager/programs/borgmatic.nix @@ -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; + }; +} diff --git a/tests/modules/home-manager/default.nix b/tests/modules/home-manager/default.nix index 5d1e64ab..371a1808 100644 --- a/tests/modules/home-manager/default.nix +++ b/tests/modules/home-manager/default.nix @@ -51,7 +51,9 @@ in import nmt { inherit pkgs lib modules; testedAttrPath = [ "home" "activationPackage" ]; + # TODO: Fix nmt to accept specialArgs or something. tests = builtins.foldl' (a: b: a // (import b)) { } ([ + #./programs/borgmatic ./programs/neovide ./programs/pipewire ./programs/pop-launcher diff --git a/tests/modules/home-manager/programs/borgmatic/basic.nix b/tests/modules/home-manager/programs/borgmatic/basic.nix new file mode 100644 index 00000000..27fd3dff --- /dev/null +++ b/tests/modules/home-manager/programs/borgmatic/basic.nix @@ -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 + ''; +} diff --git a/tests/modules/home-manager/programs/borgmatic/default.nix b/tests/modules/home-manager/programs/borgmatic/default.nix new file mode 100644 index 00000000..853cc69c --- /dev/null +++ b/tests/modules/home-manager/programs/borgmatic/default.nix @@ -0,0 +1,4 @@ +{ + borgmatic-basic = ./basic.nix; + borgmatic-multiple-configurations = ./multiple-configurations.nix; +}