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.
This commit is contained in:
YTG1234 2025-06-03 21:37:43 +03:00 committed by Austin Horstman
parent 7707ebb05c
commit 68cc9eeb38

View file

@ -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
''
);
};
}