1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.nzbget;
7 nzbget = pkgs.nzbget; in {
8 options = {
9 services.nzbget = {
10 enable = mkEnableOption "NZBGet";
11
12 package = mkOption {
13 type = types.package;
14 default = pkgs.nzbget;
15 defaultText = "pkgs.nzbget";
16 description = "The NZBGet package to use";
17 };
18
19 user = mkOption {
20 type = types.str;
21 default = "nzbget";
22 description = "User account under which NZBGet runs";
23 };
24
25 group = mkOption {
26 type = types.str;
27 default = "nzbget";
28 description = "Group under which NZBGet runs";
29 };
30 };
31 };
32
33 config = mkIf cfg.enable {
34 systemd.services.nzbget = {
35 description = "NZBGet Daemon";
36 after = [ "network.target" ];
37 wantedBy = [ "multi-user.target" ];
38 path = with pkgs; [
39 unrar
40 p7zip
41 ];
42 preStart = ''
43 datadir=/var/lib/nzbget
44 cfgtemplate=${cfg.package}/share/nzbget/nzbget.conf
45 test -d $datadir || {
46 echo "Creating nzbget data directory in $datadir"
47 mkdir -p $datadir
48 }
49 test -f $configfile || {
50 echo "nzbget.conf not found. Copying default config $cfgtemplate to $configfile"
51 cp $cfgtemplate $configfile
52 echo "Setting $configfile permissions to 0700 (needs to be written and contains plaintext credentials)"
53 chmod 0700 $configfile
54 echo "Setting temporary \$MAINDIR variable in default config required in order to allow nzbget to complete initial start"
55 echo "Remember to change this to a proper value once NZBGet startup has been completed"
56 sed -i -e 's/MainDir=.*/MainDir=\/tmp/g' $configfile
57 }
58 echo "Ensuring proper ownership of $datadir (${cfg.user}:${cfg.group})."
59 chown -R ${cfg.user}:${cfg.group} $datadir
60 '';
61
62 script = ''
63 configfile=/var/lib/nzbget/nzbget.conf
64 args="--daemon --configfile $configfile"
65 # The script in preStart (above) copies nzbget's config template to datadir on first run, containing paths that point to the nzbget derivation installed at the time.
66 # These paths break when nzbget is upgraded & the original derivation is garbage collected. If such broken paths are found in the config file, override them to point to
67 # the currently installed nzbget derivation.
68 cfgfallback () {
69 local hit=`grep -Po "(?<=^$1=).*+" "$configfile" | sed 's/[ \t]*$//'` # Strip trailing whitespace
70 ( test $hit && test -e $hit ) || {
71 echo "In $configfile, valid $1 not found; falling back to $1=$2"
72 args+=" -o $1=$2"
73 }
74 }
75 cfgfallback ConfigTemplate ${cfg.package}/share/nzbget/nzbget.conf
76 cfgfallback WebDir ${cfg.package}/share/nzbget/webui
77 ${cfg.package}/bin/nzbget $args
78 '';
79
80 serviceConfig = {
81 Type = "forking";
82 User = cfg.user;
83 Group = cfg.group;
84 PermissionsStartOnly = "true";
85 Restart = "on-failure";
86 };
87 };
88
89 users.extraUsers = mkIf (cfg.user == "nzbget") {
90 nzbget = {
91 group = cfg.group;
92 uid = config.ids.uids.nzbget;
93 };
94 };
95
96 users.extraGroups = mkIf (cfg.group == "nzbget") {
97 nzbget = {
98 gid = config.ids.gids.nzbget;
99 };
100 };
101 };
102}