Skip to content

Commit

Permalink
simplify sabnzbd config file generation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rlad78 committed Jul 14, 2024
1 parent d524d09 commit f9d626b
Showing 1 changed file with 3 additions and 51 deletions.
54 changes: 3 additions & 51 deletions nixarr/sabnzbd/config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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 {
name = "sabnzbd-fix-config-permissions";
runtimeInputs = with pkgs; [util-linux];
Expand All @@ -71,7 +49,7 @@
'';
};

user-configs-to-python = with lib;
user-configs-to-python-list = with lib;
attrsets.collect (f: !builtins.isAttrs f) (
attrsets.mapAttrsRecursive (
path: value:
Expand All @@ -94,40 +72,14 @@
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()
'');

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 {
systemd.tmpfiles.rules = ["C ${cfg.stateDir}/sabnzbd.ini - - - - ${ini-base-config-file}"];
systemd.services.sabnzbd.serviceConfig.ExecStartPre = lib.mkBefore [
("+" + 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")
];
}

0 comments on commit f9d626b

Please sign in to comment.