nixos/services/gallery-dl: refactor module for its settings merging

Instead of duplicating the options where the application selects those
options, we let the nixpkgs module system do that instead.
This commit is contained in:
Gabriel Arazas 2024-09-11 13:05:21 +08:00
parent 316df74307
commit 83bb89b33a
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360

View File

@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, options, ... }:
let let
cfg = config.services.gallery-dl; cfg = config.services.gallery-dl;
@ -9,7 +9,7 @@ let
settingsFormatFile = settingsFormatFile =
settingsFormat.generate "gallery-dl-service-config" cfg.settings; settingsFormat.generate "gallery-dl-service-config" cfg.settings;
jobType = { name, config, options, ... }: { jobType = { name, config, ... }: {
options = { options = {
urls = lib.mkOption { urls = lib.mkOption {
type = with lib.types; listOf str; type = with lib.types; listOf str;
@ -37,6 +37,10 @@ let
example = "*-*-3/4"; example = "*-*-3/4";
}; };
downloadPath = options.services.gallery-dl.downloadPath // {
default = cfg.downloadPath;
};
extraArgs = lib.mkOption { extraArgs = lib.mkOption {
type = with lib.types; listOf str; type = with lib.types; listOf str;
description = '' description = ''
@ -52,19 +56,19 @@ let
''; '';
}; };
settings = lib.mkOption { settings = options.services.gallery-dl.settings // {
type = settingsFormat.type;
description = '' description = ''
Job-specific settings to be overridden to the service-wide settings. Job-specific settings to be overridden to the service-wide settings
(if there's any).
''; '';
default = { }; default = { };
example = lib.literalExpression ''
{
extractor.directory = [ "{category}" "{user|artist|uploader}" ];
}
'';
}; };
}; };
config = {
extraArgs = cfg.extraArgs;
settings = cfg.settings;
};
}; };
in in
{ {
@ -79,13 +83,13 @@ in
defaultText = lib.literalExpression "pkgs.gallery-dl"; defaultText = lib.literalExpression "pkgs.gallery-dl";
}; };
archivePath = lib.mkOption { downloadPath = lib.mkOption {
type = lib.types.str; type = lib.types.str;
description = '' description = ''
The location of the archive to be downloaded. Take note it is assumed The default download path of the entire jobset (which can easily be
to be created at the time of running the service. Should be an absolute overriden).
path.
''; '';
default = "/var/gallery-dl";
example = "/var/archives/gallery-dl-service"; example = "/var/archives/gallery-dl-service";
}; };
@ -100,14 +104,16 @@ in
example = lib.literalExpression '' example = lib.literalExpression ''
{ {
cache.file = "~/.gallery-dl-cache.sqlite3"; cache.file = "~/.gallery-dl-cache.sqlite3";
extractor.directory = [ "{category}" "{user|artist|uploader}" ];
} }
''; '';
}; };
extraArgs = lib.mkOption { extraArgs = lib.mkOption {
type = with lib.types; listOf str; type = with lib.types; listOf str;
description = description = ''
"List of arguments to be passed to {command}`gallery-dl`."; Global list of arguments to be passed to each gallery-dl download jobs.
'';
default = [ ]; default = [ ];
example = lib.literalExpression '' example = lib.literalExpression ''
[ [
@ -153,7 +159,7 @@ in
enable = true; enable = true;
path = with pkgs; [ brotli ffmpeg cfg.package ]; path = with pkgs; [ brotli ffmpeg cfg.package ];
preStart = '' preStart = ''
mkdir -p ${lib.escapeShellArg cfg.archivePath} mkdir -p ${lib.escapeShellArg value.downloadPath}
''; '';
# Order matters here. We're letting service-level arguments and # Order matters here. We're letting service-level arguments and
@ -172,13 +178,10 @@ in
value.settings; value.settings;
in in
'' ''
gallery-dl ${lib.escapeShellArgs cfg.extraArgs} ${ gallery-dl ${lib.escapeShellArgs value.extraArgs} ${
lib.optionalString (cfg.settings != null)
"--config ${settingsFormatFile}"
} ${lib.escapeShellArgs value.extraArgs} ${
lib.optionalString (value.settings != null) lib.optionalString (value.settings != null)
"--config ${jobLevelSettingsFile}" "--config ${jobLevelSettingsFile}"
} --destination ${lib.escapeShellArg cfg.archivePath} ${ } --destination ${lib.escapeShellArg value.downloadPath} ${
lib.escapeShellArgs value.urls lib.escapeShellArgs value.urls
} }
''; '';