1{ lib, config, pkgs, user, helpers, ... }:
2
3with lib;
4let
5 cfgRoot = config.modules.server;
6 cfg = config.modules.server.jellyfin;
7
8 group = "share";
9in helpers.linuxAttrs {
10 options.modules.server.jellyfin = {
11 enable = mkOption {
12 default = false;
13 example = true;
14 description = "Whether to enable Jellyfin server.";
15 type = types.bool;
16 };
17
18 sync = mkOption {
19 default = cfg.enable;
20 description = "Whether to sync files from remotes";
21 type = types.bool;
22 };
23 };
24
25 config = mkIf (cfg.enable && cfgRoot.enable) {
26 hardware.graphics.enable = mkDefault true;
27
28 users.users."${user}".extraGroups = [ "${group}" ];
29
30 users.groups.share.gid = 1001;
31
32 services.jellyfin = {
33 enable = true;
34 openFirewall = false;
35 group = "${group}";
36 };
37
38 age.secrets."rclone.conf" = mkIf cfg.sync {
39 symlink = true;
40 path = "/run/secrets/rclone.conf";
41 file = ./encrypt/rclone.conf.age;
42 owner = "jellyfin";
43 group = "${group}";
44 };
45
46 systemd.services."rclone-sync@" = mkIf cfg.sync {
47 wants = [ "network-online.target" ];
48 description = "Sync files between different remotes via rclone";
49 stopIfChanged = false;
50 serviceConfig = {
51 Type = "simple";
52 User = "jellyfin";
53 Group = "${group}";
54 ExecStart = ''
55 ${pkgs.rclone}/bin/rclone \
56 --config /run/secrets/rclone.conf \
57 -P copy \
58 -u \
59 --max-age 24h \
60 --multi-thread-streams 0 \
61 putio:%I /share/media/%I
62 '';
63 };
64 };
65
66 systemd.timers."rclone-sync@" = mkIf cfg.sync {
67 description = "Sync files between different remotes via rclone periodically";
68 timerConfig = {
69 OnBootSec = "15min";
70 OnUnitActiveSec="8h";
71 Persistent = true;
72 };
73 };
74
75 systemd.targets.rclone-sync = mkIf cfg.sync {
76 wantedBy = [ "multi-user.target" ];
77 wants = [ "rclone-sync@movies.timer" "rclone-sync@series.timer" ];
78 };
79 };
80}