From ea035976ccbdf64454049b95a1bdac3bfe103244 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Mon, 12 Aug 2024 17:55:50 +0800 Subject: [PATCH] home-manager/services/ludusavi: init --- modules/home-manager/default.nix | 1 + modules/home-manager/services/ludusavi.nix | 102 ++++++++++++++++++ tests/modules/home-manager/default.nix | 1 + .../home-manager/services/ludusavi/basic.nix | 24 +++++ .../services/ludusavi/default.nix | 3 + 5 files changed, 131 insertions(+) create mode 100644 modules/home-manager/services/ludusavi.nix create mode 100644 tests/modules/home-manager/services/ludusavi/basic.nix create mode 100644 tests/modules/home-manager/services/ludusavi/default.nix diff --git a/modules/home-manager/default.nix b/modules/home-manager/default.nix index 97dffc50..5f9b9de0 100644 --- a/modules/home-manager/default.nix +++ b/modules/home-manager/default.nix @@ -10,6 +10,7 @@ ./services/distant.nix ./services/gallery-dl.nix ./services/gonic.nix + ./services/ludusavi.nix ./services/matcha.nix ./services/plover.nix ./services/yt-dlp.nix diff --git a/modules/home-manager/services/ludusavi.nix b/modules/home-manager/services/ludusavi.nix new file mode 100644 index 00000000..ebe08d4f --- /dev/null +++ b/modules/home-manager/services/ludusavi.nix @@ -0,0 +1,102 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.services.ludusavi; + + settingsFormat = pkgs.formats.yaml { }; + + configFile = + if cfg.configFile == null then + settingsFormat.generate "ludusavi-service-config" cfg.settings + else + cfg.configFile; +in +{ + options.services.ludusavi = { + enable = lib.mkEnableOption "Ludusavi game backup"; + + package = lib.mkPackageOption pkgs "ludusavi" { }; + + settings = lib.mkOption { + type = settingsFormat.type; + description = '' + The configuration for the backup service. If + {option}`services.ludusavi.configFile` contains a non-null value, this + option is effectively ignored. + ''; + default = { }; + example = lib.literalExpression '' + { + manifest.url = "https://raw.githubusercontent.com/mtkennerly/ludusavi-manifest/master/data/manifest.yaml"; + backup.path = "''${config.xdg.cacheHome}/ludusavi/backups"; + restore.path = "''${config.xdg.cacheHome}/ludusavi/backups"; + } + ''; + }; + + extraArgs = lib.mkOption { + type = with lib.types; listOf str; + description = '' + Extra arguments to be passed to the game backup service. + ''; + default = [ "--force" ]; + example = [ + "--force" + "--compression" "zstd" + "--compression-level" "13" + ]; + }; + + startAt = lib.mkOption { + type = lib.types.str; + description = '' + How often the backup occurs. + + The value is used to `Calendar.OnCalendar` systemd timer option. For + more details about the value format, see {manpage}`systemd.time(7)`. + ''; + default = "daily"; + example = "weekly"; + }; + + configFile = lib.mkOption { + type = with lib.types; nullOr path; + description = '' + The path of the configuration file to be used for the game backup + service. If this is set to `null`, it will generate one from + {option}`services.ludusavi.settings`. + ''; + default = null; + example = lib.literalExpression "./config/ludusavi/config.yaml"; + }; + }; + + config = lib.mkIf cfg.enable { + assertions = [ + (lib.hm.assertions.assertPlatform "services.ludusavi" pkgs lib.platforms.linux) + ]; + + systemd.user.services.ludusavi = { + Unit = { + Description = "Periodic game backup"; + Documentation = [ "https://github.com/mtkennerly/ludusavi" ]; + }; + + Service = { + ExecStart = "${lib.getExe' cfg.package "ludusavi"} --config ${configFile} backup ${lib.concatStringsSep " " cfg.extraArgs}"; + Restart = "on-failure"; + }; + + Install.WantedBy = [ "default.target" ]; + }; + + systemd.user.timers.ludusavi = { + Unit.Description = "Periodic game backup"; + Timer = { + Persistent = true; + OnCalendar = cfg.startAt; + }; + Install.WantedBy = [ "timers.target" ]; + }; + }; +} diff --git a/tests/modules/home-manager/default.nix b/tests/modules/home-manager/default.nix index 3609fa5f..cde21315 100644 --- a/tests/modules/home-manager/default.nix +++ b/tests/modules/home-manager/default.nix @@ -61,6 +61,7 @@ import nmt { ./services/bleachbit ./services/gallery-dl ./services/gonic + ./services/ludusavi ./services/matcha ./services/plover ./services/yt-dlp diff --git a/tests/modules/home-manager/services/ludusavi/basic.nix b/tests/modules/home-manager/services/ludusavi/basic.nix new file mode 100644 index 00000000..0179e568 --- /dev/null +++ b/tests/modules/home-manager/services/ludusavi/basic.nix @@ -0,0 +1,24 @@ +{ config, ... }: + +{ + services.ludusavi = { + enable = true; + extraArgs = [ + "--force" + "--compression zstd" + "--compression-level 15" + ]; + settings = { + manifest.url = "https://raw.githubusercontent.com/mtkennerly/ludusavi-manifest/master/data/manifest.yaml"; + backup.path = "${config.xdg.cacheHome}/ludusavi/backups"; + restore.path = "${config.xdg.cacheHome}/ludusavi/backups"; + }; + }; + + test.stubs.ludusavi = { }; + + nmt.script = '' + assertFileExists home-files/.config/systemd/user/ludusavi.service + assertFileExists home-files/.config/systemd/user/ludusavi.timer + ''; +} diff --git a/tests/modules/home-manager/services/ludusavi/default.nix b/tests/modules/home-manager/services/ludusavi/default.nix new file mode 100644 index 00000000..15183c4b --- /dev/null +++ b/tests/modules/home-manager/services/ludusavi/default.nix @@ -0,0 +1,3 @@ +{ + ludusavi-basic = ./basic.nix; +}