summaryrefslogtreecommitdiff
path: root/nixos/modules/services/youtube-dl.nix
blob: 11fa091e174ab240ef3258b614d62a596f554ae1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{ config, lib, pkgs, ... }:
with lib;

let
  cfg = config.eth.services.youtube-dl;

  escapeName = name: "youtube-dl-${replaceStrings [ " " "'" ] [ "-" "" ] name}";

  mkService = name: opts: {
    name = escapeName name;
    value = {
      description = "Download ${name}";
      serviceConfig = {
	User = cfg.user;
	Group = cfg.group;
        ExecStart = ''
          ${pkgs.youtube-dl}/bin/youtube-dl \
            --output ${escapeShellArg opts.directory}/${escapeShellArg opts.pattern} \
            --write-description \
            --write-info-json \
            --write-sub \
            --write-thumbnail \
            ${escapeShellArg opts.url}
        '';
        NoNewPrivileges = true;
        ProtectHome = true;
        ProtectKernelTunables = true;
        ProtectControlGroups = true;
        ProtectKernelModules = true;
        RestrictAddressFamilies = "AF_INET AF_INET6";
      };
    };
  };

  mkTimer = name: opts: {
    name = escapeName name;
    value = {
      enable = opts.enable;
      description = "Periodically download ${name}";
      wants = [ "network.target" ];
      after = [ "network.target" ];
      wantedBy = [ "timers.target" ];
      timerConfig = {
        OnCalendar = "daily";
        Unit = "${escapeName name}.service";
        Persistent = true;
        RandomizedDelaySec = "8h";
      };
    };
  };

in {
  options.eth.services.youtube-dl = {
    enable = mkEnableOption "Download media with youtube-dl.";

    user = mkOption {
      type = types.nullOr types.str;
      default = null;
      example = "eth";
    };
    group = mkOption {
      type = types.nullOr types.str;
      default = null;
      example = "users";
    };

    download = mkOption {
      type = types.attrsOf (types.submodule {
        options = {
          enable = mkEnableOption "Enable downloader.";

          url = mkOption {
            type = types.str;
            description = "URL to download.";
            default = "";
            example = "http://web.server/rss.xml";
          };

          directory = mkOption {
            type = types.path;
            description = "Directory to download media to.";
            default = "";
            example = "/mnt/md0/media/podcasts";
          };

          pattern = mkOption {
            type = types.str;
            description = "Filename pattern";
            default = "";
            example = "%(title)s.%(ext)s";
          };
        };
      });
      default = {};
      example = {
        "Fun City" = {
          enable = true;
          url = "http://web.server/rss.xml";
          directory = "/mnt/md0/media/podcasts";
          pattern = "%(title)s.%(ext)s";
        };
      };
      description = "Things to download";
    };
  };

  config = mkIf cfg.enable {
    systemd = {
      services = mapAttrs' mkService cfg.download;
      timers = mapAttrs' mkTimer cfg.download;
    };
  };
}