From dd9921fc7ec0b936c0e38b45c230100da87a5f6f Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Wed, 20 Jul 2022 16:55:23 +0800 Subject: [PATCH] services/gallery-dl: add job-specific settings The arguments are also arranged to let the resulting settings cascade from service-wide to job-specific settings. --- modules/home-manager/services/gallery-dl.nix | 25 ++++++++++-- modules/nixos/services/gallery-dl.nix | 40 ++++++++++++++++--- .../tasks/multimedia-archive/default.nix | 2 +- 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/modules/home-manager/services/gallery-dl.nix b/modules/home-manager/services/gallery-dl.nix index 6c408462..21cf454e 100644 --- a/modules/home-manager/services/gallery-dl.nix +++ b/modules/home-manager/services/gallery-dl.nix @@ -53,6 +53,19 @@ let ] ''; }; + + settings = lib.mkOption { + type = settingsFormat.type; + description = '' + Job-specific settings to be overridden to the service-wide settings. + ''; + default = { }; + example = lib.literalExpression '' + { + extractor.directory = [ "{category}" "{user|artist|uploader}" ]; + } + ''; + }; }; }; in { @@ -146,13 +159,19 @@ in { Service.ExecStart = let scriptName = "gallery-dl-service-${config.home.username}-${name}"; + jobSpecificSettingsFile = + settingsFormat.generate "gallery-dl-service-job-${name}-settings" + value.settings; archiveScript = pkgs.writeShellScriptBin scriptName '' ${cfg.package}/bin/gallery-dl ${ - lib.concatStringsSep " " cfg.extraArgs - } ${lib.concatStringsSep " " value.extraArgs} ${ + lib.escapeShellArgs cfg.extraArgs + } ${ lib.optionalString (cfg.settings != null) "--config ${settingsFormatFile}" - } --directory ${cfg.archivePath} ${lib.escapeShellArgs value.urls} + } ${lib.escapeShellArgs value.extraArgs} ${ + lib.optionalString (value.settings != null) + "--config ${jobSpecificSettingsFile}" + } --destination ${cfg.archivePath} ${lib.escapeShellArgs value.urls} ''; in "${archiveScript}/bin/${scriptName}"; }) cfg.jobs; diff --git a/modules/nixos/services/gallery-dl.nix b/modules/nixos/services/gallery-dl.nix index c236d7d3..9671ac26 100644 --- a/modules/nixos/services/gallery-dl.nix +++ b/modules/nixos/services/gallery-dl.nix @@ -52,6 +52,19 @@ let ] ''; }; + + settings = lib.mkOption { + type = settingsFormat.type; + description = '' + Job-specific settings to be overridden to the service-wide settings. + ''; + default = { }; + example = lib.literalExpression '' + { + extractor.directory = [ "{category}" "{user|artist|uploader}" ]; + } + ''; + }; }; }; in { @@ -137,17 +150,32 @@ in { description = "gallery-dl archive job for group '${name}'"; documentation = [ "man:gallery-dl(1)" ]; enable = true; - path = [ cfg.package ] ++ (with pkgs; [ coreutils ffmpeg ]); + path = [ cfg.package ] ++ (with pkgs; [ brotli coreutils ffmpeg ]); preStart = '' mkdir -p ${lib.escapeShellArg cfg.archivePath} ''; - script = '' - gallery-dl ${lib.concatStringsSep " " cfg.extraArgs} ${ - lib.concatStringsSep " " value.extraArgs - } ${ + + # Order matters here. We're letting service-level arguments and + # settings to be overridden with job-specific things as much as + # possible especially with the settings. + # + # Regarding to settings (`settings`) and extra arguments + # (`extraArgs`), the settings is the last applied argument with + # `--config` option. This means that it will cascade resultings + # settings from `extraArgs` if there's any related option that is + # given like another `--config` for example. + script = let + jobLevelSettingsFile = + settingsFormat.generate "gallery-dl-job-${name}-settings" + value.settings; + in '' + gallery-dl ${lib.escapeShellArgs cfg.extraArgs} ${ lib.optionalString (cfg.settings != null) "--config ${settingsFormatFile}" - } --directory ${lib.escapeShellArg cfg.archivePath} ${ + } ${lib.escapeShellArgs value.extraArgs} ${ + lib.optionalString (value.settings != null) + "--config ${jobLevelSettingsFile}" + } --destination ${lib.escapeShellArg cfg.archivePath} ${ lib.escapeShellArgs value.urls } ''; diff --git a/modules/nixos/tasks/multimedia-archive/default.nix b/modules/nixos/tasks/multimedia-archive/default.nix index 626678ba..b0f0b541 100644 --- a/modules/nixos/tasks/multimedia-archive/default.nix +++ b/modules/nixos/tasks/multimedia-archive/default.nix @@ -160,7 +160,7 @@ in extraArgs = [ # Record all downloaded files in an archive file. - "--download-archive ${config.services.gallery-dl.archivePath}/photos" + "--download-archive" "${config.services.gallery-dl.archivePath}/photos" # Write metadata to separate JSON files. "--write-metadata"