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