blob: 6ab98751c57b49eced1d0119cf19c3ed250d1a43 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
{ config, pkgs, lib, ... }:
with lib;
let
cfg = config.services.nzbget;
dataDir = builtins.dirOf cfg.configFile;
in {
options = {
services.nzbget = {
enable = mkEnableOption "NZBGet";
package = mkOption {
type = types.package;
default = pkgs.nzbget;
defaultText = "pkgs.nzbget";
description = "The NZBGet package to use";
};
dataDir = mkOption {
type = types.str;
default = "/var/lib/nzbget";
description = "The directory where NZBGet stores its configuration files.";
};
openFirewall = mkOption {
type = types.bool;
default = false;
description = ''
Open ports in the firewall for the NZBGet web interface
'';
};
user = mkOption {
type = types.str;
default = "nzbget";
description = "User account under which NZBGet runs";
};
group = mkOption {
type = types.str;
default = "nzbget";
description = "Group under which NZBGet runs";
};
configFile = mkOption {
type = types.str;
default = "/var/lib/nzbget/nzbget.conf";
description = "Path for NZBGet's config file. (If this doesn't exist, the default config template is copied here.)";
};
};
};
config = mkIf cfg.enable {
systemd.services.nzbget = {
description = "NZBGet Daemon";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
path = with pkgs; [
unrar
p7zip
];
preStart = ''
cfgtemplate=${cfg.package}/share/nzbget/nzbget.conf
if [ ! -f ${cfg.configFile} ]; then
echo "${cfg.configFile} not found. Copying default config $cfgtemplate to ${cfg.configFile}"
install -m 0700 $cfgtemplate ${cfg.configFile}
echo "Setting temporary \$MAINDIR variable in default config required in order to allow nzbget to complete initial start"
echo "Remember to change this to a proper value once NZBGet startup has been completed"
sed -i -e 's/MainDir=.*/MainDir=\/tmp/g' ${cfg.configFile}
fi
'';
script = ''
args="--daemon --configfile ${cfg.configFile}"
# 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.
# 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
# the currently installed nzbget derivation.
cfgfallback () {
local hit=`grep -Po "(?<=^$1=).*+" "${cfg.configFile}" | sed 's/[ \t]*$//'` # Strip trailing whitespace
( test $hit && test -e $hit ) || {
echo "In ${cfg.configFile}, valid $1 not found; falling back to $1=$2"
args+=" -o $1=$2"
}
}
cfgfallback ConfigTemplate ${cfg.package}/share/nzbget/nzbget.conf
cfgfallback WebDir ${cfg.package}/share/nzbget/webui
${cfg.package}/bin/nzbget $args
'';
serviceConfig = {
StateDirectory = dataDir;
StateDirectoryMode = "0700";
Type = "forking";
User = cfg.user;
Group = cfg.group;
PermissionsStartOnly = "true";
Restart = "on-failure";
};
};
networking.firewall = mkIf cfg.openFirewall {
allowedTCPPorts = [ 8989 ];
};
users.users = mkIf (cfg.user == "nzbget") {
nzbget = {
group = cfg.group;
uid = config.ids.uids.nzbget;
};
};
users.groups = mkIf (cfg.group == "nzbget") {
nzbget = {
gid = config.ids.gids.nzbget;
};
};
};
}
|