use configobj for post-install config changes

The Python configobj library is what SABnzbd uses to create and modify
their ini config file. Figured it'd be easiest to do the same, at least
for when we need to make changes after the initial config file has been
copied.
This commit is contained in:
Richard Carter
2024-06-04 18:51:36 -04:00
parent f20993ed57
commit 2af829d35e
+26 -15
View File
@@ -59,22 +59,33 @@ let
''; '';
}; };
apply-user-configs-script = pkgs.writeShellApplication { user-configs-to-python = with lib;
name = "sabnzbd-set-user-values"; attrsets.collect (f: !builtins.isAttrs f) (
runtimeInputs = with pkgs; [gnused util-linux]; attrsets.mapAttrsRecursive (
text = with lib; '' path: value:
if [ ! -f ${ini-file-target} ]; then "sab_config_map['"
echo "FAILURE: Cannot write changes to ${ini-file-target}, file does not exist" + (lib.strings.concatStringsSep "']['" path)
exit 1 + "'] = '"
fi + (builtins.toString value)
+ "'"
)
user-configs
);
apply-user-configs-script = with lib; (pkgs.writers.writePython3Bin
"sabnzbd-set-user-values" {libraries = [pkgs.python3Packages.configobj];} ''
from pathlib import Path
from configobj import ConfigObj
cp --preserve ${ini-file-target}{,.tmp} sab_config_path = Path("${ini-file-target}")
< ${ini-file-target} \ if not sab_config_path.is_file() or sab_config_path.suffix != ".ini":
'' + (strings.concatStringsSep "|" (lists.flatten user-config-set-cmds)) raise Exception(f"{sab_config_path} is not a valid config file path.")
+ ''
> ${ini-file-target}.tmp && mv -f ${ini-file-target}{.tmp,} sab_config_map = ConfigObj(str(sab_config_path))
'';
}; ${lib.strings.concatStringsSep "\n" user-configs-to-python}
sab_config_map.write()
'');
bashCheckIfEmptyStr = v: "[[ -z \$${v} || \$${v} == '\"\"' ]]"; bashCheckIfEmptyStr = v: "[[ -z \$${v} || \$${v} == '\"\"' ]]";
gen-uuids-script = pkgs.writeShellApplication { gen-uuids-script = pkgs.writeShellApplication {