2016-05-04 10:58:01 +02:00
{ config , pkgs , lib , . . . }:
with lib ;
let
cfg = config . services . nzbget ;
2018-07-20 19:56:59 +02:00
in {
2016-05-04 10:58:01 +02: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 13:38:20 +01: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 10:58:01 +02: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 " ;
} ;
} ;
} ;
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 = ''
2019-01-13 13:38:20 +01:00
datadir = $ { cfg . dataDir }
configfile = $ { cfg . dataDir } /nzbget.conf
2017-10-30 01:37:56 +01:00
cfgtemplate = $ { cfg . package } /share/nzbget/nzbget.conf
test - d $ datadir || {
echo " C r e a t i n g n z b g e t d a t a d i r e c t o r y i n $ d a t a d i r "
mkdir - p $ datadir
2016-05-04 10:58:01 +02:00
}
2017-10-30 01:37:56 +01:00
test - f $ configfile || {
echo " n z b g e t . c o n f 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 $ c o n f i g f i l e "
cp $ cfgtemplate $ configfile
echo " S e t t i n g $ c o n f i g f i l e p e r m i s s i o n s t o 0 7 0 0 ( n e e d s t o b e w r i t t e n a n d c o n t a i n s p l a i n t e x t c r e d e n t i a l s ) "
chmod 0700 $ configfile
2016-05-04 10:58:01 +02: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 "
2017-10-30 01:37:56 +01:00
sed - i - e ' s/MainDir = . * /MainDir = \ /tmp/g ' $ configfile
2016-05-04 10:58:01 +02:00
}
2017-10-30 01:37:56 +01:00
echo " E n s u r i n g p r o p e r o w n e r s h i p o f $ d a t a d i r ( ${ cfg . user } : ${ cfg . group } ) . "
chown - R $ { cfg . user }: $ { cfg . group } $ datadir
'' ;
script = ''
2019-01-13 13:38:20 +01:00
configfile = $ { cfg . dataDir } /nzbget.conf
2017-10-30 01:37:56 +01:00
args = " - - d a e m o n - - c o n f i g f i l e $ c o n f i g f i l e "
# 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 = ) . * + " " $ c o n f i g f i l e " | sed ' s / [ \ t ] * $ // ' ` # Strip trailing whitespace
( test $ hit && test - e $ hit ) || {
echo " I n $ c o n f i g f i l e , 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 "
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 10:58:01 +02:00
'' ;
serviceConfig = {
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 13:38:20 +01:00
networking . firewall = mkIf cfg . openFirewall {
allowedTCPPorts = [ 8989 ] ;
} ;
2018-06-30 01:58:35 +02:00
users . users = mkIf ( cfg . user == " n z b g e t " ) {
2016-05-04 10:58:01 +02:00
nzbget = {
group = cfg . group ;
uid = config . ids . uids . nzbget ;
} ;
} ;
2018-06-30 01:58:35 +02:00
users . groups = mkIf ( cfg . group == " n z b g e t " ) {
2016-05-04 10:58:01 +02:00
nzbget = {
gid = config . ids . gids . nzbget ;
} ;
} ;
} ;
}