1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.sonarr;
7in
8{
9 options = {
10 services.sonarr = {
11 enable = mkEnableOption (lib.mdDoc "Sonarr");
12
13 dataDir = mkOption {
14 type = types.str;
15 default = "/var/lib/sonarr/.config/NzbDrone";
16 description = lib.mdDoc "The directory where Sonarr stores its data files.";
17 };
18
19 openFirewall = mkOption {
20 type = types.bool;
21 default = false;
22 description = lib.mdDoc ''
23 Open ports in the firewall for the Sonarr web interface
24 '';
25 };
26
27 user = mkOption {
28 type = types.str;
29 default = "sonarr";
30 description = lib.mdDoc "User account under which Sonaar runs.";
31 };
32
33 group = mkOption {
34 type = types.str;
35 default = "sonarr";
36 description = lib.mdDoc "Group under which Sonaar runs.";
37 };
38
39 package = mkOption {
40 type = types.package;
41 default = pkgs.sonarr;
42 defaultText = literalExpression "pkgs.sonarr";
43 description = lib.mdDoc ''
44 Sonarr package to use.
45 '';
46 };
47 };
48 };
49
50 config = mkIf cfg.enable {
51 systemd.tmpfiles.rules = [
52 "d '${cfg.dataDir}' 0700 ${cfg.user} ${cfg.group} - -"
53 ];
54
55 systemd.services.sonarr = {
56 description = "Sonarr";
57 after = [ "network.target" ];
58 wantedBy = [ "multi-user.target" ];
59
60 serviceConfig = {
61 Type = "simple";
62 User = cfg.user;
63 Group = cfg.group;
64 ExecStart = "${cfg.package}/bin/NzbDrone -nobrowser -data='${cfg.dataDir}'";
65 Restart = "on-failure";
66 };
67 };
68
69 networking.firewall = mkIf cfg.openFirewall {
70 allowedTCPPorts = [ 8989 ];
71 };
72
73 users.users = mkIf (cfg.user == "sonarr") {
74 sonarr = {
75 group = cfg.group;
76 home = cfg.dataDir;
77 uid = config.ids.uids.sonarr;
78 };
79 };
80
81 users.groups = mkIf (cfg.group == "sonarr") {
82 sonarr.gid = config.ids.gids.sonarr;
83 };
84 };
85}