From 68cc9eeb3875ae9682c04629f20738e1e79d72aa Mon Sep 17 00:00:00 2001 From: YTG1234 Date: Tue, 3 Jun 2025 21:37:43 +0300 Subject: [PATCH] jellyfin-mpv-shim: merge settings with existing config file The Nix-defined settings take priority over the existing settings, but any new keys added by the user are preserved. --- modules/services/jellyfin-mpv-shim.nix | 46 +++++++++++++++++--------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/modules/services/jellyfin-mpv-shim.nix b/modules/services/jellyfin-mpv-shim.nix index 037750d9..849247b2 100644 --- a/modules/services/jellyfin-mpv-shim.nix +++ b/modules/services/jellyfin-mpv-shim.nix @@ -107,22 +107,6 @@ in ]; xdg.configFile = { - # jellyfin-mpv-shim errors if the config file isn't writeable - # so, as a temporary workaround, we generate a hidden file (.conf.json) - # and make a writeable copy. - "jellyfin-mpv-shim/conf.json" = lib.mkIf (cfg.settings != { }) { - source = jsonFormat.generate "jellyfin-mpv-shim-conf" cfg.settings; - target = "jellyfin-mpv-shim/.conf.json"; - onChange = - let - dir = "${config.xdg.configHome}/jellyfin-mpv-shim"; - in - '' - cp --dereference ${dir}/.conf.json ${dir}/conf.json - chmod u+w ${dir}/conf.json - ''; - }; - "jellyfin-mpv-shim/mpv.conf" = lib.mkIf (cfg.mpvConfig != null) { text = renderOptions cfg.mpvConfig; }; @@ -148,5 +132,35 @@ in WantedBy = [ "graphical-session.target" ]; }; }; + + # Yoinked from programs/zed-editor.nix + # jellyfin-mpv-shim can't load the configuration file if it's not + # writeable. So we merge the settings defined here in Nix with the existing + # configuration, if any. + home.activation.jellyfinMpvShimSettingsActivation = + let + path = lib.escapeShellArg "${config.xdg.configHome}/jellyfin-mpv-shim/conf.json"; + staticSettings = lib.escapeShellArg (jsonFormat.generate "jellyfin-mpv-shim-conf" cfg.settings); + cmd = "${lib.getExe pkgs.jq} -s '.[0] * .[1]' ${path} ${staticSettings}"; + in + lib.mkIf (cfg.settings != { }) ( + lib.hm.dag.entryAfter [ "linkGeneration" ] '' + run mkdir -p "$(dirname ${path})" + if [ ! -e ${path} ]; then + # Create the file + if [[ -v DRY_RUN ]]; then + run echo '{}' '>' ${path} + else + echo '{}' > ${path} + fi + fi + if [[ -v DRY_RUN ]]; then + run ${cmd} '>' ${path} + else + config="$(${cmd})" + printf '%s\n' "$config" > ${path} + fi + '' + ); }; }