|
| 1 | +{ |
| 2 | + config, |
| 3 | + lib, |
| 4 | + pkgs, |
| 5 | + utils, |
| 6 | + ... |
| 7 | +}: |
| 8 | + |
| 9 | +let |
| 10 | + cfg = config.services.recyclarr; |
| 11 | + format = pkgs.formats.yaml { }; |
| 12 | + stateDir = "/var/lib/recyclarr"; |
| 13 | + configPath = "${stateDir}/config.json"; |
| 14 | +in |
| 15 | +{ |
| 16 | + options.services.recyclarr = { |
| 17 | + enable = lib.mkEnableOption "recyclarr service"; |
| 18 | + |
| 19 | + package = lib.mkPackageOption pkgs "recyclarr" { }; |
| 20 | + |
| 21 | + configuration = lib.mkOption { |
| 22 | + type = format.type; |
| 23 | + default = { }; |
| 24 | + example = { |
| 25 | + sonarr = [ |
| 26 | + { |
| 27 | + instance_name = "main"; |
| 28 | + base_url = "http://localhost:8989"; |
| 29 | + api_key = { |
| 30 | + _secret = "/run/credentials/recyclarr.service/sonarr-api_key"; |
| 31 | + }; |
| 32 | + } |
| 33 | + ]; |
| 34 | + radarr = [ |
| 35 | + { |
| 36 | + instance_name = "main"; |
| 37 | + base_url = "http://localhost:7878"; |
| 38 | + api_key = { |
| 39 | + _secret = "/run/credentials/recyclarr.service/radarr-api_key"; |
| 40 | + }; |
| 41 | + } |
| 42 | + ]; |
| 43 | + }; |
| 44 | + description = lib.mdDoc '' |
| 45 | + Recyclarr YAML configuration as a Nix attribute set. |
| 46 | +
|
| 47 | + For detailed configuration options and examples, see the |
| 48 | + [official configuration reference](https://recyclarr.dev/wiki/yaml/config-reference/). |
| 49 | +
|
| 50 | + The configuration is processed using [utils.genJqSecretsReplacementSnippet](https://github.com/NixOS/nixpkgs/blob/master/nixos/lib/utils.nix#L232-L331) to handle secret substitution. |
| 51 | +
|
| 52 | + To avoid permission issues, secrets should be provided via systemd's credential mechanism: |
| 53 | +
|
| 54 | + ```nix |
| 55 | + systemd.services.recyclarr.serviceConfig.LoadCredential = [ |
| 56 | + "radarr-api_key:''${config.sops.secrets.radarr-api_key.path}" |
| 57 | + ]; |
| 58 | + ''; |
| 59 | + }; |
| 60 | + |
| 61 | + schedule = lib.mkOption { |
| 62 | + type = lib.types.str; |
| 63 | + default = "daily"; |
| 64 | + description = "When to run recyclarr in systemd calendar format."; |
| 65 | + }; |
| 66 | + |
| 67 | + command = lib.mkOption { |
| 68 | + type = lib.types.str; |
| 69 | + default = "sync"; |
| 70 | + description = "The recyclarr command to run (e.g., sync)."; |
| 71 | + }; |
| 72 | + |
| 73 | + user = lib.mkOption { |
| 74 | + type = lib.types.str; |
| 75 | + default = "recyclarr"; |
| 76 | + description = "User account under which recyclarr runs."; |
| 77 | + }; |
| 78 | + |
| 79 | + group = lib.mkOption { |
| 80 | + type = lib.types.str; |
| 81 | + default = "recyclarr"; |
| 82 | + description = "Group under which recyclarr runs."; |
| 83 | + }; |
| 84 | + }; |
| 85 | + |
| 86 | + config = lib.mkIf cfg.enable { |
| 87 | + |
| 88 | + users.users = lib.mkIf (cfg.user == "recyclarr") { |
| 89 | + recyclarr = { |
| 90 | + isSystemUser = true; |
| 91 | + description = "recyclarr user"; |
| 92 | + home = stateDir; |
| 93 | + group = cfg.group; |
| 94 | + }; |
| 95 | + }; |
| 96 | + |
| 97 | + users.groups = lib.mkIf (cfg.group == "recyclarr") { |
| 98 | + ${cfg.group} = { }; |
| 99 | + }; |
| 100 | + |
| 101 | + systemd.services.recyclarr = { |
| 102 | + description = "Recyclarr Service"; |
| 103 | + |
| 104 | + # YAML is a JSON super-set |
| 105 | + preStart = utils.genJqSecretsReplacementSnippet cfg.configuration configPath; |
| 106 | + |
| 107 | + serviceConfig = { |
| 108 | + Type = "oneshot"; |
| 109 | + User = cfg.user; |
| 110 | + Group = cfg.group; |
| 111 | + StateDirectory = "recyclarr"; |
| 112 | + ExecStart = "${lib.getExe cfg.package} ${cfg.command} --app-data ${stateDir} --config ${configPath}"; |
| 113 | + |
| 114 | + ProtectSystem = "strict"; |
| 115 | + ProtectHome = true; |
| 116 | + PrivateTmp = true; |
| 117 | + PrivateDevices = true; |
| 118 | + ProtectHostname = true; |
| 119 | + ProtectClock = true; |
| 120 | + ProtectKernelTunables = true; |
| 121 | + ProtectKernelModules = true; |
| 122 | + ProtectKernelLogs = true; |
| 123 | + ProtectControlGroups = true; |
| 124 | + |
| 125 | + PrivateNetwork = false; |
| 126 | + RestrictAddressFamilies = [ |
| 127 | + "AF_INET" |
| 128 | + "AF_INET6" |
| 129 | + ]; |
| 130 | + |
| 131 | + NoNewPrivileges = true; |
| 132 | + RestrictSUIDSGID = true; |
| 133 | + RemoveIPC = true; |
| 134 | + |
| 135 | + ReadWritePaths = [ stateDir ]; |
| 136 | + |
| 137 | + CapabilityBoundingSet = ""; |
| 138 | + |
| 139 | + LockPersonality = true; |
| 140 | + RestrictRealtime = true; |
| 141 | + }; |
| 142 | + }; |
| 143 | + |
| 144 | + systemd.timers.recyclarr = { |
| 145 | + description = "Recyclarr Timer"; |
| 146 | + wantedBy = [ "timers.target" ]; |
| 147 | + partOf = [ "recyclarr.service" ]; |
| 148 | + |
| 149 | + timerConfig = { |
| 150 | + OnCalendar = cfg.schedule; |
| 151 | + Persistent = true; |
| 152 | + RandomizedDelaySec = "5m"; |
| 153 | + }; |
| 154 | + }; |
| 155 | + }; |
| 156 | +} |
0 commit comments