nixos/services/yt-dlp: create job-specific archivePath

This commit is contained in:
Gabriel Arazas 2024-08-21 18:12:50 +08:00
parent e4b63c084a
commit 23b2be907f
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360

View File

@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, options, pkgs, ... }:
let let
cfg = config.services.yt-dlp; cfg = config.services.yt-dlp;
@ -7,7 +7,7 @@ let
jobUnitName = name: "yt-dlp-archive-service-${name}"; jobUnitName = name: "yt-dlp-archive-service-${name}";
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;
@ -35,16 +35,12 @@ let
example = "*-*-3/4"; example = "*-*-3/4";
}; };
extraArgs = lib.mkOption { extraArgs = options.services.yt-dlp.extraArgs // {
type = with lib.types; listOf str; default = cfg.extraArgs;
description = };
"Job-specific extra arguments to be passed to the {command}`yt-dlp`.";
default = [ ]; archivePath = options.services.yt-dlp.archivePath // {
example = lib.literalExpression '' default = cfg.archivePath;
[
"--date" "today"
]
'';
}; };
}; };
}; };
@ -64,17 +60,18 @@ in
}; };
archivePath = lib.mkOption { archivePath = lib.mkOption {
type = lib.types.str; type = lib.types.path;
description = '' description = ''
The location of the archive to be downloaded. Must be an absolute path. The location of the archive to be downloaded. Must be an absolute path.
''; '';
default = "/var/yt-dlp";
example = "/var/archives/yt-dlp-service"; example = "/var/archives/yt-dlp-service";
}; };
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}`yt-dlp`."; "Global list of arguments to be passed to each yt-dlp job.";
default = [ "--download-archive videos" ]; default = [ "--download-archive videos" ];
example = lib.literalExpression '' example = lib.literalExpression ''
[ [
@ -118,11 +115,12 @@ in
# `--paths` flag. # `--paths` flag.
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
systemd.services = lib.mapAttrs' systemd.services = lib.mapAttrs'
(name: value: (name: job:
let let
jobLevelArgs = lib.escapeShellArgs value.extraArgs; jobLevelArgs = lib.escapeShellArgs job.extraArgs;
in in
lib.nameValuePair (jobUnitName name) { lib.nameValuePair (jobUnitName name) {
inherit (job) startAt;
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ]; wants = [ "network-online.target" ];
after = [ "network-online.target" ]; after = [ "network-online.target" ];
@ -131,13 +129,13 @@ in
enable = true; enable = true;
path = [ cfg.package pkgs.coreutils ]; path = [ cfg.package pkgs.coreutils ];
preStart = '' preStart = ''
mkdir -p ${lib.escapeShellArg cfg.archivePath} mkdir -p ${lib.escapeShellArg job.archivePath}
''; '';
script = '' script = ''
yt-dlp ${serviceLevelArgs} ${jobLevelArgs} \ yt-dlp ${serviceLevelArgs} ${jobLevelArgs} \
${lib.escapeShellArgs value.urls} --paths ${lib.escapeShellArg cfg.archivePath} ${lib.escapeShellArgs job.urls} \
--paths ${lib.escapeShellArg job.archivePath}
''; '';
startAt = value.startAt;
serviceConfig = { serviceConfig = {
LockPersonality = true; LockPersonality = true;
NoNewPrivileges = true; NoNewPrivileges = true;