|
1 | | -{ config, lib, ... }: |
| 1 | +{ config, lib, pkgs, ... }: |
2 | 2 |
|
3 | 3 | with lib; |
4 | 4 |
|
5 | | -let cfg = config.services.declutarr; |
| 5 | +let |
| 6 | + cfg = config.services.declutarr; |
| 7 | + format = pkgs.formats.yaml { }; |
| 8 | + configFile = if cfg.config != null then |
| 9 | + format.generate "configuration.yaml" cfg.config |
| 10 | + else |
| 11 | + cfg.configFile; |
6 | 12 | in { |
7 | 13 | options.services.declutarr = { |
8 | 14 | enable = mkEnableOption "Enable Declutarr"; |
9 | 15 |
|
10 | | - configFile = mkOption { type = types.path; }; |
| 16 | + package = mkOption { |
| 17 | + type = types.package; |
| 18 | + default = pkgs.declutarr; |
| 19 | + }; |
| 20 | + |
| 21 | + dataDir = mkOption { |
| 22 | + type = types.path; |
| 23 | + default = "/var/lib/declutarr"; |
| 24 | + }; |
| 25 | + |
| 26 | + user = mkOption { |
| 27 | + type = types.str; |
| 28 | + default = "declutarr"; |
| 29 | + }; |
11 | 30 |
|
12 | | - extraVolumes = mkOption { |
13 | | - type = types.listOf types.path; |
14 | | - default = [ ]; |
| 31 | + group = mkOption { |
| 32 | + type = types.str; |
| 33 | + default = "declutarr"; |
| 34 | + }; |
| 35 | + |
| 36 | + config = mkOption { |
| 37 | + type = types.nullOr format.type; |
| 38 | + default = null; |
| 39 | + }; |
| 40 | + |
| 41 | + configFile = mkOption { |
| 42 | + type = types.nullOr types.path; |
| 43 | + default = null; |
| 44 | + }; |
| 45 | + |
| 46 | + environmentFile = mkOption { |
| 47 | + type = types.nullOr types.path; |
| 48 | + default = null; |
15 | 49 | }; |
16 | 50 | }; |
17 | 51 |
|
18 | 52 | config = mkIf cfg.enable { |
19 | | - virtualisation.oci-containers.containers.declutarr = { |
20 | | - image = "ghcr.io/manimatter/decluttarr:dev"; |
21 | | - environment = { TZ = config.time.timeZone; }; |
22 | | - volumes = [ "${cfg.configFile}:/app/config/config.yaml" ] |
23 | | - ++ (map (vol: "${vol}:${vol}") cfg.extraVolumes); |
| 53 | + assertions = [{ |
| 54 | + assertion = !(cfg.config != null && cfg.configFile != null); |
| 55 | + message = |
| 56 | + "Only one of `config` or `configFile` can be configured at the same time"; |
| 57 | + }]; |
| 58 | + |
| 59 | + systemd.tmpfiles.settings."10-declutarr" = { |
| 60 | + ${cfg.dataDir} = { |
| 61 | + "d" = { |
| 62 | + inherit (cfg) user group; |
| 63 | + mode = "0777"; |
| 64 | + }; |
| 65 | + }; |
| 66 | + |
| 67 | + "${cfg.dataDir}/config" = { |
| 68 | + "d" = { |
| 69 | + inherit (cfg) user group; |
| 70 | + mode = "0777"; |
| 71 | + }; |
| 72 | + }; |
| 73 | + |
| 74 | + "${cfg.dataDir}/logs" = { |
| 75 | + "d" = { |
| 76 | + inherit (cfg) user group; |
| 77 | + mode = "0777"; |
| 78 | + }; |
| 79 | + }; |
| 80 | + }; |
| 81 | + |
| 82 | + users = { |
| 83 | + users.declutarr = mkIf (cfg.user == "declutarr") { |
| 84 | + inherit (cfg) group; |
| 85 | + isSystemUser = true; |
| 86 | + }; |
| 87 | + |
| 88 | + groups = mkIf (cfg.group == "declutarr") { declutarr = { }; }; |
| 89 | + }; |
| 90 | + |
| 91 | + systemd.services.declutarr = { |
| 92 | + enable = true; |
| 93 | + wants = [ "network.online.target" ]; |
| 94 | + after = [ "network.online.target" ]; |
| 95 | + wantedBy = [ "multi-user.target" ]; |
| 96 | + preStart = '' |
| 97 | + ln -sf ${configFile} ${cfg.dataDir}/config/config.yaml |
| 98 | + ''; |
| 99 | + serviceConfig = { |
| 100 | + WorkingDirectory = cfg.dataDir; |
| 101 | + EnvironmentFile = [ cfg.environmentFile ]; |
| 102 | + ExecStart = lib.getExe cfg.package; |
| 103 | + ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; |
| 104 | + User = cfg.user; |
| 105 | + Group = cfg.group; |
| 106 | + Restart = "on-failure"; |
| 107 | + ReadWritePaths = [ cfg.dataDir ]; |
| 108 | + }; |
24 | 109 | }; |
25 | 110 | }; |
26 | 111 | } |
0 commit comments