1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.flexget;
7 pkg = cfg.package;
8 ymlFile = pkgs.writeText "flexget.yml" ''
9 ${cfg.config}
10
11 ${optionalString cfg.systemScheduler "schedules: no"}
12'';
13 configFile = "${toString cfg.homeDir}/flexget.yml";
14in {
15 options = {
16 services.flexget = {
17 enable = mkEnableOption (lib.mdDoc "FlexGet daemon");
18
19 package = mkPackageOptionMD pkgs "flexget" {};
20
21 user = mkOption {
22 default = "deluge";
23 example = "some_user";
24 type = types.str;
25 description = lib.mdDoc "The user under which to run flexget.";
26 };
27
28 homeDir = mkOption {
29 default = "/var/lib/deluge";
30 example = "/home/flexget";
31 type = types.path;
32 description = lib.mdDoc "Where files live.";
33 };
34
35 interval = mkOption {
36 default = "10m";
37 example = "1h";
38 type = types.str;
39 description = lib.mdDoc "When to perform a {command}`flexget` run. See {command}`man 7 systemd.time` for the format.";
40 };
41
42 systemScheduler = mkOption {
43 default = true;
44 example = false;
45 type = types.bool;
46 description = lib.mdDoc "When true, execute the runs via the flexget-runner.timer. If false, you have to specify the settings yourself in the YML file.";
47 };
48
49 config = mkOption {
50 default = "";
51 type = types.lines;
52 description = lib.mdDoc "The YAML configuration for FlexGet.";
53 };
54 };
55 };
56
57 config = mkIf cfg.enable {
58
59 environment.systemPackages = [ pkg ];
60
61 systemd.services = {
62 flexget = {
63 description = "FlexGet Daemon";
64 path = [ pkg ];
65 serviceConfig = {
66 User = cfg.user;
67 ExecStartPre = "${pkgs.coreutils}/bin/install -m644 ${ymlFile} ${configFile}";
68 ExecStart = "${pkg}/bin/flexget -c ${configFile} daemon start";
69 ExecStop = "${pkg}/bin/flexget -c ${configFile} daemon stop";
70 ExecReload = "${pkg}/bin/flexget -c ${configFile} daemon reload";
71 Restart = "on-failure";
72 PrivateTmp = true;
73 WorkingDirectory = toString cfg.homeDir;
74 };
75 wantedBy = [ "multi-user.target" ];
76 };
77
78 flexget-runner = mkIf cfg.systemScheduler {
79 description = "FlexGet Runner";
80 after = [ "flexget.service" ];
81 wants = [ "flexget.service" ];
82 serviceConfig = {
83 User = cfg.user;
84 ExecStart = "${pkg}/bin/flexget -c ${configFile} execute";
85 PrivateTmp = true;
86 WorkingDirectory = toString cfg.homeDir;
87 };
88 };
89 };
90
91 systemd.timers.flexget-runner = mkIf cfg.systemScheduler {
92 description = "Run FlexGet every ${cfg.interval}";
93 wantedBy = [ "timers.target" ];
94 timerConfig = {
95 OnBootSec = "5m";
96 OnUnitInactiveSec = cfg.interval;
97 Unit = "flexget-runner.service";
98 };
99 };
100 };
101}