2016-05-04 08:58:01 +00:00
{ config , pkgs , lib , . . . }:
with lib ;
let
cfg = config . services . nzbget ;
2019-02-13 16:38:33 +00:00
dataDir = builtins . dirOf cfg . configFile ;
2018-07-20 17:56:59 +00:00
in {
2016-05-04 08:58:01 +00:00
options = {
services . nzbget = {
enable = mkEnableOption " N Z B G e t " ;
package = mkOption {
type = types . package ;
default = pkgs . nzbget ;
defaultText = " p k g s . n z b g e t " ;
description = " T h e N Z B G e t p a c k a g e t o u s e " ;
} ;
2019-01-13 12:38:20 +00:00
dataDir = mkOption {
type = types . str ;
default = " / v a r / l i b / n z b g e t " ;
description = " T h e d i r e c t o r y w h e r e N Z B G e t s t o r e s i t s c o n f i g u r a t i o n f i l e s . " ;
} ;
openFirewall = mkOption {
type = types . bool ;
default = false ;
description = ''
Open ports in the firewall for the NZBGet web interface
'' ;
} ;
2016-05-04 08:58:01 +00:00
user = mkOption {
type = types . str ;
default = " n z b g e t " ;
description = " U s e r a c c o u n t u n d e r w h i c h N Z B G e t r u n s " ;
} ;
group = mkOption {
type = types . str ;
default = " n z b g e t " ;
description = " G r o u p u n d e r w h i c h N Z B G e t r u n s " ;
} ;
2019-02-13 16:38:33 +00:00
configFile = mkOption {
type = types . str ;
default = " / v a r / l i b / n z b g e t / n z b g e t . c o n f " ;
description = " P a t h f o r N Z B G e t ' s c o n f i g f i l e . ( I f t h i s d o e s n ' t e x i s t , t h e d e f a u l t c o n f i g t e m p l a t e i s c o p i e d h e r e . ) " ;
} ;
2016-05-04 08:58:01 +00:00
} ;
} ;
config = mkIf cfg . enable {
systemd . services . nzbget = {
description = " N Z B G e t D a e m o n " ;
after = [ " n e t w o r k . t a r g e t " ] ;
wantedBy = [ " m u l t i - u s e r . t a r g e t " ] ;
path = with pkgs ; [
unrar
p7zip
] ;
preStart = ''
2017-10-30 00:37:56 +00:00
cfgtemplate = $ { cfg . package } /share/nzbget/nzbget.conf
2019-02-13 16:38:33 +00:00
if [ ! - f $ { cfg . configFile } ] ; then
echo " ${ cfg . configFile } n o t f o u n d . C o p y i n g d e f a u l t c o n f i g $ c f g t e m p l a t e t o ${ cfg . configFile } "
install - m 0700 $ cfgtemplate $ { cfg . configFile }
2016-05-04 08:58:01 +00:00
echo " S e t t i n g t e m p o r a r y \$ M A I N D I R v a r i a b l e i n d e f a u l t c o n f i g r e q u i r e d i n o r d e r t o a l l o w n z b g e t t o c o m p l e t e i n i t i a l s t a r t "
echo " R e m e m b e r t o c h a n g e t h i s t o a p r o p e r v a l u e o n c e N Z B G e t s t a r t u p h a s b e e n c o m p l e t e d "
2019-02-13 16:38:33 +00:00
sed - i - e ' s/MainDir = . * /MainDir = \ /tmp/g ' $ { cfg . configFile }
fi
2017-10-30 00:37:56 +00:00
'' ;
script = ''
2019-02-13 16:38:33 +00:00
args = " - - d a e m o n - - c o n f i g f i l e ${ 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
2017-10-30 00:37:56 +00:00
# the currently installed nzbget derivation.
cfgfallback ( ) {
2019-02-13 16:38:33 +00:00
local hit = ` grep - Po " ( ? < = ^ $ 1 = ) . * + " " ${ cfg . configFile } " | sed ' s / [ \ t ] * $ // ' ` # Strip trailing whitespace
2017-10-30 00:37:56 +00:00
( test $ hit && test - e $ hit ) || {
2019-02-13 16:38:33 +00:00
echo " I n ${ cfg . configFile } , v a l i d $ 1 n o t f o u n d ; f a l l i n g b a c k t o $ 1 = $ 2 "
2017-10-30 00:37:56 +00:00
args + = " - o $ 1 = $ 2 "
}
}
cfgfallback ConfigTemplate $ { cfg . package } /share/nzbget/nzbget.conf
cfgfallback WebDir $ { cfg . package } /share/nzbget/webui
$ { cfg . package } /bin/nzbget $ args
2016-05-04 08:58:01 +00:00
'' ;
serviceConfig = {
2019-02-13 16:38:33 +00:00
StateDirectory = dataDir ;
StateDirectoryMode = " 0 7 0 0 " ;
2016-05-04 08:58:01 +00:00
Type = " f o r k i n g " ;
User = cfg . user ;
Group = cfg . group ;
PermissionsStartOnly = " t r u e " ;
Restart = " o n - f a i l u r e " ;
} ;
} ;
2019-01-13 12:38:20 +00:00
networking . firewall = mkIf cfg . openFirewall {
allowedTCPPorts = [ 8989 ] ;
} ;
2018-06-29 23:58:35 +00:00
users . users = mkIf ( cfg . user == " n z b g e t " ) {
2016-05-04 08:58:01 +00:00
nzbget = {
group = cfg . group ;
uid = config . ids . uids . nzbget ;
} ;
} ;
2018-06-29 23:58:35 +00:00
users . groups = mkIf ( cfg . group == " n z b g e t " ) {
2016-05-04 08:58:01 +00:00
nzbget = {
gid = config . ids . gids . nzbget ;
} ;
} ;
} ;
}