simplify sabnzbd config file generation

Let api keys be generated by SABnzbd on first run; there's no point in
trying to do this ourselves. That effort was originally made when we had
confidence in generating the whole config file from scratch.
This commit is contained in:
Richard Carter
2024-07-14 16:10:57 -04:00
parent d524d09d0d
commit f9d626bb6e
+3 -51
View File
@@ -30,33 +30,11 @@
}; };
}; };
api-key-configs = {
misc = {
api_key = "";
nzb_key = "";
};
};
compiled-configs = {misc = user-configs.misc // api-key-configs.misc;};
ini-base-config-file = pkgs.writeTextFile { ini-base-config-file = pkgs.writeTextFile {
name = "base-config.ini"; name = "base-config.ini";
text = lib.generators.toINI {} compiled-configs; text = lib.generators.toINI {} user-configs;
}; };
mkSedEditValue = name: value: ''sed -E 's%(\b${name} ?= ?).*%\1${builtins.toString value}%g' '';
user-config-set-cmds = with lib.attrsets;
mapAttrsToList (
group-n: group-v: (
mapAttrsToList (
n: v: "${mkSedEditValue n v} \\\n"
)
group-v
)
)
user-configs;
fix-config-permissions-script = pkgs.writeShellApplication { fix-config-permissions-script = pkgs.writeShellApplication {
name = "sabnzbd-fix-config-permissions"; name = "sabnzbd-fix-config-permissions";
runtimeInputs = with pkgs; [util-linux]; runtimeInputs = with pkgs; [util-linux];
@@ -71,7 +49,7 @@
''; '';
}; };
user-configs-to-python = with lib; user-configs-to-python-list = with lib;
attrsets.collect (f: !builtins.isAttrs f) ( attrsets.collect (f: !builtins.isAttrs f) (
attrsets.mapAttrsRecursive ( attrsets.mapAttrsRecursive (
path: value: path: value:
@@ -94,40 +72,14 @@
sab_config_map = ConfigObj(str(sab_config_path)) sab_config_map = ConfigObj(str(sab_config_path))
${lib.strings.concatStringsSep "\n" user-configs-to-python} ${lib.strings.concatStringsSep "\n" user-configs-to-python-list}
sab_config_map.write() sab_config_map.write()
''); '');
bashCheckIfEmptyStr = v: "[[ -z \$${v} || \$${v} == '\"\"' ]]";
gen-uuids-script = pkgs.writeShellApplication {
name = "sabnzbd-set-random-api-uuids";
runtimeInputs = with pkgs; [initool gnused util-linux];
text = ''
if [ ! -f ${ini-file-target} ]; then
echo "FAILURE: ${ini-file-target} does not exist. Cannot generate crypto strings."
exit 1
fi
api_key_value=$(initool get ${ini-file-target} misc api_key -v)
nzb_key_value=$(initool get ${ini-file-target} misc nzb_key -v)
if ${bashCheckIfEmptyStr "api_key_value"} || ${bashCheckIfEmptyStr "nzb_key_value"}; then
cp --preserve ${ini-file-target}{,.tmp}
api_uuid=$(uuidgen --random | tr -d '-')
nzb_uuid=$(uuidgen --random | tr -d '-')
< ${ini-file-target} \
${mkSedEditValue "api_key" "'\"$api_uuid\"'"} \
| ${mkSedEditValue "nzb_key" "'\"$nzb_uuid\"'"} \
> ${ini-file-target}.tmp && mv -f ${ini-file-target}{.tmp,}
fi
'';
};
in { in {
systemd.tmpfiles.rules = ["C ${cfg.stateDir}/sabnzbd.ini - - - - ${ini-base-config-file}"]; systemd.tmpfiles.rules = ["C ${cfg.stateDir}/sabnzbd.ini - - - - ${ini-base-config-file}"];
systemd.services.sabnzbd.serviceConfig.ExecStartPre = lib.mkBefore [ systemd.services.sabnzbd.serviceConfig.ExecStartPre = lib.mkBefore [
("+" + fix-config-permissions-script + "/bin/sabnzbd-fix-config-permissions") ("+" + fix-config-permissions-script + "/bin/sabnzbd-fix-config-permissions")
(gen-uuids-script + "/bin/sabnzbd-set-random-api-uuids")
(apply-user-configs-script + "/bin/sabnzbd-set-user-values") (apply-user-configs-script + "/bin/sabnzbd-set-user-values")
]; ];
} }