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