From e0bba5655bae4ec84645ebea876d523e0666036b Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sun, 3 Apr 2022 10:10:29 +0800 Subject: [PATCH] services/gallery-dl: init service Both for home-manager and NixOS, this time. Because I find use for both of them, especially if you have a dedicated server for NixOS. --- modules/home-manager/services/gallery-dl.nix | 174 +++++++++++++++++++ modules/nixos/services/gallery-dl.nix | 153 ++++++++++++++++ 2 files changed, 327 insertions(+) create mode 100644 modules/home-manager/services/gallery-dl.nix create mode 100644 modules/nixos/services/gallery-dl.nix diff --git a/modules/home-manager/services/gallery-dl.nix b/modules/home-manager/services/gallery-dl.nix new file mode 100644 index 00000000..bdbcdda3 --- /dev/null +++ b/modules/home-manager/services/gallery-dl.nix @@ -0,0 +1,174 @@ +{ config, options, lib, pkgs, ... }: + +let + cfg = config.services.gallery-dl; + + settingsFormat = pkgs.formats.json { }; + settingsFormatFile = + settingsFormat.generate "gallery-dl-service-config-${config.home.username}" cfg.settings; + + jobType = { name, config, options, ... }: { + options = { + urls = lib.mkOption { + type = with lib.types; listOf str; + default = [ ]; + description = '' + A list of URLs to be downloaded to gallery-dl. Please + see the list of extractors with . + ''; + example = lib.literalExpression '' + [ + "https://www.deviantart.com/xezeno" + "https://www.pixiv.net/en/users/60562229" + ] + ''; + }; + + startAt = lib.mkOption { + type = with lib.types; str; + description = '' + Indicates how frequent the download will occur. The given schedule + should follow the format as described from + + systemd.time + 5 + . + ''; + default = "daily"; + example = "*-*-3/4"; + }; + + extraArgs = lib.mkOption { + type = with lib.types; listOf str; + description = '' + Job-specific extra arguments to be passed to the + gallery-dl. + ''; + default = [ ]; + example = lib.literalExpression '' + [ + "--date 'today-1week'" # get only videos from a week ago + "--output '%(uploader)s/%(title)s.%(ext)s'" # download them in the respective directory + ] + ''; + }; + }; + }; +in { + options.services.gallery-dl = { + enable = lib.mkEnableOption "archiving services with gallery-dl"; + + package = lib.mkOption { + type = lib.types.package; + description = + "Package containing the gallery-dl binary."; + default = pkgs.gallery-dl; + defaultText = lib.literalExpression "pkgs.gallery-dl"; + }; + + archivePath = lib.mkOption { + type = lib.types.str; + description = '' + The location of the archive to be downloaded. Take note it is assumed + to be created at the time of running the service. Must be an absolute + path. + ''; + default = "${config.xdg.userDirs.pictures}/gallery-dl-service"; + example = lib.literalExpression + "\${config.xdg.userDirs.download}/archiving-service/photos"; + }; + + settings = lib.mkOption { + type = settingsFormat.type; + description = '' + The configuration to be used for the service. If the value is empty, + the service will not pass any option relating to the custom + configuration. + ''; + default = null; + example = lib.literalExpression '' + { + cache.file = "~/.gallery-dl-cache.sqlite3"; + } + ''; + }; + + extraArgs = lib.mkOption { + type = with lib.types; listOf str; + description = + "List of arguments to be passed to gallery-dl."; + default = [ ]; + example = lib.literalExpression '' + [ + "--retries 20" + ] + ''; + }; + + jobs = lib.mkOption { + type = with lib.types; attrsOf (submodule jobType); + description = '' + A map of jobs for the archiving service. + ''; + default = { }; + example = lib.literalExpression '' + { + arts = { + urls = [ + "https://www.pixiv.net/en/users/60562229" + "https://www.deviantart.com/xezeno" + ]; + startAt = "weekly"; + }; + + mango = { + urls = [ + # TODO: Put some manga sites here + ]; + startAt = "weekly"; + }; + } + ''; + }; + }; + + config = lib.mkIf cfg.enable { + home.packages = [ cfg.package ]; + + systemd.user.services = lib.mapAttrs' (name: value: + lib.nameValuePair "gallery-dl-archive-service-${name}" { + Unit = { + Description = "gallery-dl archive job for group '${name}'"; + After = "network.target"; + Documentation = "man:gallery-dl(1)"; + }; + + Service.ExecStart = let + scriptName = "gallery-dl-service-${config.home.username}-${name}"; + archiveScript = pkgs.writeShellScriptBin scriptName '' + ${cfg.package}/bin/gallery-dl ${ + lib.concatStringsSep " " cfg.extraArgs + } ${lib.concatStringsSep " " value.extraArgs} ${ + lib.optionalString (cfg.settings != null) + "--config ${settingsFormatFile}" + } --directory ${cfg.archivePath} ${lib.escapeShellArgs value.urls} + ''; + in "${archiveScript}/bin/${scriptName}"; + }) cfg.jobs; + + systemd.user.timers = lib.mapAttrs' (name: value: + lib.nameValuePair "gallery-dl-archive-service-${name}" { + Unit = { + Description = "gallery-dl archive job for group '${name}'"; + Documentation = "man:gallery-dl(1)"; + }; + + Timer = { + OnCalendar = value.startAt; + Persistent = true; + }; + + Install.WantedBy = [ "timers.target" ]; + }) cfg.jobs; + }; +} diff --git a/modules/nixos/services/gallery-dl.nix b/modules/nixos/services/gallery-dl.nix new file mode 100644 index 00000000..87daea3c --- /dev/null +++ b/modules/nixos/services/gallery-dl.nix @@ -0,0 +1,153 @@ +{ config, options, lib, pkgs, ... }: + +let + cfg = config.services.gallery-dl; + + settingsFormat = pkgs.formats.json { }; + settingsFormatFile = + settingsFormat.generate "gallery-dl-service-config" cfg.settings; + + jobType = { name, config, options, ... }: { + options = { + urls = lib.mkOption { + type = with lib.types; listOf str; + default = [ ]; + description = '' + A list of URLs to be downloaded to gallery-dl. Please + see the list of extractors with . + ''; + example = lib.literalExpression '' + [ + "https://www.deviantart.com/xezeno" + "https://www.pixiv.net/en/users/60562229" + ] + ''; + }; + + startAt = lib.mkOption { + type = with lib.types; str; + description = '' + Indicates how frequent the download will occur. The given schedule + should follow the format as described from + + systemd.time + 5 + . + ''; + default = "daily"; + example = "*-*-3/4"; + }; + + extraArgs = lib.mkOption { + type = with lib.types; listOf str; + description = '' + Job-specific extra arguments to be passed to the + gallery-dl. + ''; + default = [ ]; + example = lib.literalExpression '' + [ + "--date 'today-1week'" # get only videos from a week ago + "--output '%(uploader)s/%(title)s.%(ext)s'" # download them in the respective directory + ] + ''; + }; + }; + }; +in { + options.services.gallery-dl = { + enable = lib.mkEnableOption "archiving services with gallery-dl"; + + package = lib.mkOption { + type = lib.types.package; + description = + "Package containing the gallery-dl binary."; + default = pkgs.gallery-dl; + defaultText = lib.literalExpression "pkgs.gallery-dl"; + }; + + archivePath = lib.mkOption { + type = lib.types.str; + description = '' + The location of the archive to be downloaded. Take note it is assumed + to be created at the time of running the service. + ''; + default = "/archives/gallery-dl-service"; + example = lib.literalExpression "/archiving-service/photos"; + }; + + settings = lib.mkOption { + type = settingsFormat.type; + description = '' + The configuration to be used for the service. If the value is empty, + the service will not pass any option relating to the custom + configuration. + ''; + default = null; + example = lib.literalExpression '' + { + cache.file = "~/.gallery-dl-cache.sqlite3"; + } + ''; + }; + + extraArgs = lib.mkOption { + type = with lib.types; listOf str; + description = + "List of arguments to be passed to gallery-dl."; + default = [ ]; + example = lib.literalExpression '' + [ + "--retries 20" + ] + ''; + }; + + jobs = lib.mkOption { + type = with lib.types; attrsOf (submodule jobType); + description = '' + A map of jobs for the archiving service. + ''; + default = { }; + example = lib.literalExpression '' + { + arts = { + urls = [ + "https://www.pixiv.net/en/users/60562229" + "https://www.deviantart.com/xezeno" + ]; + startAt = "weekly"; + }; + + mango = { + urls = [ + # TODO: Put some manga sites here + ]; + startAt = "weekly"; + }; + } + ''; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.services = lib.mapAttrs' (name: value: + lib.nameValuePair "gallery-dl-archive-service-${name}" { + after = [ "network.target" ]; + description = "gallery-dl archive job for group '${name}'"; + documentation = [ "man:gallery-dl(1)" ]; + enable = true; + path = [ cfg.package ] ++ (with pkgs; [ coreutils ffmpeg ]); + script = '' + mkdir -p ${lib.escapeShellArg cfg.archivePath} \ + && gallery-dl ${ + lib.concatStringsSep " " cfg.extraArgs + } ${lib.concatStringsSep " " value.extraArgs} ${ + lib.optionalString (cfg.settings != null) + "--config ${settingsFormatFile}" + } --directory ${lib.escapeShellArg cfg.archivePath} ${lib.escapeShellArgs value.urls} + ''; + startAt = value.startAt; + }) cfg.jobs; + }; +}