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