Merge pull request #60019 from aanderse/nzbget

nzbget: fix broken service, as well as some improvements
This commit is contained in:
Florian Klink 2019-04-27 18:26:50 +02:00 committed by GitHub
commit 033882e0b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 61 deletions

View File

@ -83,6 +83,14 @@
The same applies to ModemManager where modem-manager.service is now called ModemManager.service again. The same applies to ModemManager where modem-manager.service is now called ModemManager.service again.
</para> </para>
</listitem> </listitem>
<listitem>
<para>
The <option>services.nzbget.configFile</option> and <option>services.nzbget.openFirewall</option>
options were removed as they are managed internally by the nzbget. The
<option>services.nzbget.dataDir</option> option hadn't actually been used by
the module for some time and so was removed as cleanup.
</para>
</listitem>
</itemizedlist> </itemizedlist>
</section> </section>

View File

@ -45,6 +45,9 @@ with lib;
(mkRemovedOptionModule [ "services" "neo4j" "port" ] "Use services.neo4j.http.listenAddress instead.") (mkRemovedOptionModule [ "services" "neo4j" "port" ] "Use services.neo4j.http.listenAddress instead.")
(mkRemovedOptionModule [ "services" "neo4j" "boltPort" ] "Use services.neo4j.bolt.listenAddress instead.") (mkRemovedOptionModule [ "services" "neo4j" "boltPort" ] "Use services.neo4j.bolt.listenAddress instead.")
(mkRemovedOptionModule [ "services" "neo4j" "httpsPort" ] "Use services.neo4j.https.listenAddress instead.") (mkRemovedOptionModule [ "services" "neo4j" "httpsPort" ] "Use services.neo4j.https.listenAddress instead.")
(mkRemovedOptionModule [ "services" "misc" "nzbget" "configFile" ] "The configuration of nzbget is now managed by users through the web interface.")
(mkRemovedOptionModule [ "services" "misc" "nzbget" "dataDir" ] "The data directory for nzbget is now /var/lib/nzbget.")
(mkRemovedOptionModule [ "services" "misc" "nzbget" "openFirewall" ] "The port used by nzbget is managed through the web interface so you should adjust your firewall rules accordingly.")
(mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "user" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a user setting.") (mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "user" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a user setting.")
(mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "group" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a group setting.") (mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "group" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a group setting.")
(mkRenamedOptionModule [ "services" "tor" "relay" "portSpec" ] [ "services" "tor" "relay" "port" ]) (mkRenamedOptionModule [ "services" "tor" "relay" "portSpec" ] [ "services" "tor" "relay" "port" ])

View File

@ -4,33 +4,35 @@ with lib;
let let
cfg = config.services.nzbget; cfg = config.services.nzbget;
dataDir = builtins.dirOf cfg.configFile; pkg = pkgs.nzbget;
in { stateDir = "/var/lib/nzbget";
configFile = "${stateDir}/nzbget.conf";
configOpts = concatStringsSep " " (mapAttrsToList (name: value: "-o ${name}=${value}") nixosOpts);
nixosOpts = {
# allows nzbget to run as a "simple" service
OutputMode = "loggable";
# use journald for logging
WriteLog = "none";
ErrorTarget = "screen";
WarningTarget = "screen";
InfoTarget = "screen";
DetailTarget = "screen";
# required paths
ConfigTemplate = "${pkg}/share/nzbget/nzbget.conf";
WebDir = "${pkg}/share/nzbget/webui";
# nixos handles package updates
UpdateCheck = "none";
};
in
{
# interface
options = { options = {
services.nzbget = { services.nzbget = {
enable = mkEnableOption "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 { user = mkOption {
type = types.str; type = types.str;
default = "nzbget"; default = "nzbget";
@ -42,14 +44,10 @@ in {
default = "nzbget"; default = "nzbget";
description = "Group under which NZBGet runs"; description = "Group under which NZBGet runs";
}; };
};
};
configFile = mkOption { # implementation
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 { config = mkIf cfg.enable {
systemd.services.nzbget = { systemd.services.nzbget = {
@ -61,50 +59,26 @@ in {
p7zip p7zip
]; ];
preStart = '' preStart = ''
cfgtemplate=${cfg.package}/share/nzbget/nzbget.conf if [ ! -f ${configFile} ]; then
if [ ! -f ${cfg.configFile} ]; then ${pkgs.coreutils}/bin/install -m 0700 ${pkg}/share/nzbget/nzbget.conf ${configFile}
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 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 = { serviceConfig = {
StateDirectory = dataDir; StateDirectory = "nzbget";
StateDirectoryMode = "0700"; StateDirectoryMode = "0750";
Type = "forking";
User = cfg.user; User = cfg.user;
Group = cfg.group; Group = cfg.group;
PermissionsStartOnly = "true"; UMask = "0002";
Restart = "on-failure"; Restart = "on-failure";
ExecStart = "${pkg}/bin/nzbget --server --configfile ${stateDir}/nzbget.conf ${configOpts}";
ExecStop = "${pkg}/bin/nzbget --quit";
}; };
}; };
networking.firewall = mkIf cfg.openFirewall {
allowedTCPPorts = [ 8989 ];
};
users.users = mkIf (cfg.user == "nzbget") { users.users = mkIf (cfg.user == "nzbget") {
nzbget = { nzbget = {
home = stateDir;
group = cfg.group; group = cfg.group;
uid = config.ids.uids.nzbget; uid = config.ids.uids.nzbget;
}; };

View File

@ -172,6 +172,7 @@ in
nix-ssh-serve = handleTest ./nix-ssh-serve.nix {}; nix-ssh-serve = handleTest ./nix-ssh-serve.nix {};
novacomd = handleTestOn ["x86_64-linux"] ./novacomd.nix {}; novacomd = handleTestOn ["x86_64-linux"] ./novacomd.nix {};
nsd = handleTest ./nsd.nix {}; nsd = handleTest ./nsd.nix {};
nzbget = handleTest ./nzbget.nix {};
openldap = handleTest ./openldap.nix {}; openldap = handleTest ./openldap.nix {};
opensmtpd = handleTest ./opensmtpd.nix {}; opensmtpd = handleTest ./opensmtpd.nix {};
openssh = handleTest ./openssh.nix {}; openssh = handleTest ./openssh.nix {};

26
nixos/tests/nzbget.nix Normal file
View File

@ -0,0 +1,26 @@
import ./make-test.nix ({ pkgs, ...} : {
name = "nzbget";
meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ aanderse flokli ];
};
nodes = {
server = { ... }: {
services.nzbget.enable = true;
# hack, don't add (unfree) unrar to nzbget's path,
# so we can run this test in CI
systemd.services.nzbget.path = pkgs.stdenv.lib.mkForce [ pkgs.p7zip ];
};
};
testScript = ''
startAll;
$server->waitForUnit("nzbget.service");
$server->waitForUnit("network.target");
$server->waitForOpenPort(6789);
$server->succeed("curl -s -u nzbget:tegbzn6789 http://127.0.0.1:6789 | grep -q 'This file is part of nzbget'");
$server->succeed("${pkgs.nzbget}/bin/nzbget -n -o ControlIP=127.0.0.1 -o ControlPort=6789 -o ControlPassword=tegbzn6789 -V");
'';
})