From 2af829d35eb13433fbac26b89485d8a783b88d3a Mon Sep 17 00:00:00 2001 From: Richard Carter Date: Tue, 4 Jun 2024 18:51:36 -0400 Subject: [PATCH] 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. --- nixarr/sabnzbd/config.nix | 41 +++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/nixarr/sabnzbd/config.nix b/nixarr/sabnzbd/config.nix index b45b92a..6a16a08 100644 --- a/nixarr/sabnzbd/config.nix +++ b/nixarr/sabnzbd/config.nix @@ -59,22 +59,33 @@ let ''; }; - apply-user-configs-script = pkgs.writeShellApplication { - name = "sabnzbd-set-user-values"; - runtimeInputs = with pkgs; [gnused util-linux]; - text = with lib; '' - if [ ! -f ${ini-file-target} ]; then - echo "FAILURE: Cannot write changes to ${ini-file-target}, file does not exist" - exit 1 - fi + user-configs-to-python = with lib; + attrsets.collect (f: !builtins.isAttrs f) ( + attrsets.mapAttrsRecursive ( + path: value: + "sab_config_map['" + + (lib.strings.concatStringsSep "']['" path) + + "'] = '" + + (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} - < ${ini-file-target} \ - '' + (strings.concatStringsSep "|" (lists.flatten user-config-set-cmds)) - + '' - > ${ini-file-target}.tmp && mv -f ${ini-file-target}{.tmp,} - ''; - }; + sab_config_path = Path("${ini-file-target}") + if not sab_config_path.is_file() or sab_config_path.suffix != ".ini": + raise Exception(f"{sab_config_path} is not a valid config file path.") + + 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} == '\"\"' ]]"; gen-uuids-script = pkgs.writeShellApplication {