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.
This commit is contained in:
Gabriel Arazas 2022-07-20 16:55:23 +08:00
parent 01bf630a9d
commit dd9921fc7e
3 changed files with 57 additions and 10 deletions

View File

@ -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 { in {
@ -146,13 +159,19 @@ in {
Service.ExecStart = let Service.ExecStart = let
scriptName = "gallery-dl-service-${config.home.username}-${name}"; scriptName = "gallery-dl-service-${config.home.username}-${name}";
jobSpecificSettingsFile =
settingsFormat.generate "gallery-dl-service-job-${name}-settings"
value.settings;
archiveScript = pkgs.writeShellScriptBin scriptName '' archiveScript = pkgs.writeShellScriptBin scriptName ''
${cfg.package}/bin/gallery-dl ${ ${cfg.package}/bin/gallery-dl ${
lib.concatStringsSep " " cfg.extraArgs lib.escapeShellArgs cfg.extraArgs
} ${lib.concatStringsSep " " value.extraArgs} ${ } ${
lib.optionalString (cfg.settings != null) lib.optionalString (cfg.settings != null)
"--config ${settingsFormatFile}" "--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}"; in "${archiveScript}/bin/${scriptName}";
}) cfg.jobs; }) cfg.jobs;

View File

@ -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 { in {
@ -137,17 +150,32 @@ in {
description = "gallery-dl archive job for group '${name}'"; description = "gallery-dl archive job for group '${name}'";
documentation = [ "man:gallery-dl(1)" ]; documentation = [ "man:gallery-dl(1)" ];
enable = true; enable = true;
path = [ cfg.package ] ++ (with pkgs; [ coreutils ffmpeg ]); path = [ cfg.package ] ++ (with pkgs; [ brotli coreutils ffmpeg ]);
preStart = '' preStart = ''
mkdir -p ${lib.escapeShellArg cfg.archivePath} mkdir -p ${lib.escapeShellArg cfg.archivePath}
''; '';
script = ''
gallery-dl ${lib.concatStringsSep " " cfg.extraArgs} ${ # Order matters here. We're letting service-level arguments and
lib.concatStringsSep " " value.extraArgs # 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) lib.optionalString (cfg.settings != null)
"--config ${settingsFormatFile}" "--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 lib.escapeShellArgs value.urls
} }
''; '';

View File

@ -160,7 +160,7 @@ in
extraArgs = [ extraArgs = [
# Record all downloaded files in an archive file. # 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 to separate JSON files.
"--write-metadata" "--write-metadata"