2016-05-04 09:58:01 +01:00
{ config , pkgs , lib , . . . }:
with lib ;
let
cfg = config . services . nzbget ;
2017-10-30 00:37:56 +00:00
nzbget = pkgs . nzbget ; in {
2016-05-04 09:58:01 +01: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 " ;
} ;
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 = ''
2017-10-30 00:37:56 +00:00
datadir = /var/lib/nzbget
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 09:58:01 +01:00
}
2017-10-30 00:37:56 +00: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 09:58:01 +01: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 00:37:56 +00:00
sed - i - e ' s/MainDir = . * /MainDir = \ /tmp/g ' $ configfile
2016-05-04 09:58:01 +01:00
}
2017-10-30 00:37:56 +00: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 = ''
configfile = /var/lib/nzbget/nzbget.conf
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 09:58:01 +01: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 " ;
} ;
} ;
users . extraUsers = mkIf ( cfg . user == " n z b g e t " ) {
nzbget = {
group = cfg . group ;
uid = config . ids . uids . nzbget ;
} ;
} ;
users . extraGroups = mkIf ( cfg . group == " n z b g e t " ) {
nzbget = {
gid = config . ids . gids . nzbget ;
} ;
} ;
} ;
}