summaryrefslogtreecommitdiff
path: root/hosts/ahmed/torrenting/save-path.nix
blob: 065c72faf05da078ff76ec8ad464fc016ed903c5 (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
{
  pkgs,
  lib,
  config,
  ...
}: let
  downloadPath = "/srv/media/";

  categories = ["Movies" "Anime Movies" "Anime Series" "Series" "Stand-up" "Miscellaneous"];
in {
  # Create the directory to which media will be downloaded. This will be used
  # by qBittorent to hold files and Jellyfin will serve from it.
  systemd.tmpfiles.rules = let
    user = config.services.qbittorrent.user;
    group = config.services.qbittorrent.group;
  in
    map (category: "d ${lib.strings.escapeC [" "] "${downloadPath}/${category}"} 0755 ${user} ${group}") categories;

  # Configure qBittorrent to use the correct save path.
  services.qbittorrent.settings = {
    BitTorrent = {
      "Session\\DefaultSavePath" = assert builtins.elem "Miscellaneous" categories; "${downloadPath}/Miscellaneous";
      "Session\\TempPath" = "${config.services.qbittorrent.profile}/qBittorrent/temp";
      "Session\\TempPathEnabled" = true;
    };
    Preferences = {
      # Again??
      "Downloads\\SavePath" = downloadPath;
    };
  };

  # Create categories for qBittorrent with correct save paths.
  # WARNING: qBittorrent does NOT like it when you change the categories used by active torrents.
  systemd.services.qbittorrent.unitConfig = {
    Requires = ["qbittorrent-create-categories.service"];
    After = ["qbittorrent-create-categories.service"];
  };
  systemd.services.qbittorrent-create-categories = {
    enable = true;
    serviceConfig = {
      Type = "oneshot";
      User = config.services.qbittorrent.user;
      Group = config.services.qbittorrent.group;
      ExecStart = let
        categoriesJson = lib.genAttrs categories (c: {"save_path" = "${downloadPath}/${c}";});
        categoriesFile = (pkgs.formats.json {}).generate "categories.json" categoriesJson;
        categoriesPath = "${config.services.qbittorrent.profile}/qBittorrent/config/categories.json";

        script = pkgs.writeShellScript "qbittorrent-create-categories.sh" ''
          # FIXME: Creation and chowning are duplicated between this and qBittorrent service definition.
          mkdir -p /var/lib/qBittorrent/qBittorrent/config
          ln -s -f ${categoriesFile} ${categoriesPath}
          chown --recursive qbittorrent:qbittorrent -- ${config.services.qbittorrent.profile}/qBittorrent/config/
        '';
      in "!${script}";
    };
  };

  # WARNING: Jellyfin has been manually configured to serve from the correct download paths.
}