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