From ff31a4677c1a8ae506aa7e003a3dba08cb203f82 Mon Sep 17 00:00:00 2001 From: Jairo Llopis <973709+yajo@users.noreply.github.com> Date: Tue, 24 Jun 2025 21:48:03 +0100 Subject: [PATCH] fix(zed): support preexisting JSON5 settings (#7317) Zed uses JSON5 for settings files. JQ doesn't understand that format and fails if found, when merging with preexisting settings. Here I add a conversion step that converts JSON5 to JSON before handling the contents to JQ. Besides, I changed the arguments in the jq function, so instead of using `[0]` and `[1]`, we now use `$dynamic` and `$static` respectively. This should make scripts more readable. Fixes https://github.com/nix-community/home-manager/issues/7247 Fixes https://github.com/nix-community/home-manager/issues/7226 --- modules/programs/zed-editor.nix | 9 ++++++--- tests/modules/programs/zed-editor/keymap.nix | 7 +++++-- tests/modules/programs/zed-editor/settings.nix | 3 +++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/programs/zed-editor.nix b/modules/programs/zed-editor.nix index 16640308..82f9ccf6 100644 --- a/modules/programs/zed-editor.nix +++ b/modules/programs/zed-editor.nix @@ -15,13 +15,16 @@ let cfg = config.programs.zed-editor; jsonFormat = pkgs.formats.json { }; + json5 = pkgs.python3Packages.toPythonApplication pkgs.python3Packages.json5; impureConfigMerger = empty: jqOperation: path: staticSettings: '' mkdir -p $(dirname ${lib.escapeShellArg path}) if [ ! -e ${lib.escapeShellArg path} ]; then # No file? Create it echo ${lib.escapeShellArg empty} > ${lib.escapeShellArg path} fi - config="$(${pkgs.jq}/bin/jq -s ${lib.escapeShellArg jqOperation} ${lib.escapeShellArg path} ${lib.escapeShellArg staticSettings})" + dynamic="$(${lib.getExe json5} --as-json ${lib.escapeShellArg path})" + static="$(cat ${lib.escapeShellArg staticSettings})" + config="$(${lib.getExe pkgs.jq} -s ${lib.escapeShellArg jqOperation} --argjson dynamic "$dynamic" --argjson static "$static")" printf '%s\n' "$config" > ${lib.escapeShellArg path} unset config ''; @@ -170,7 +173,7 @@ in home.activation = mkMerge [ (mkIf (mergedSettings != { }) { zedSettingsActivation = lib.hm.dag.entryAfter [ "linkGeneration" ] ( - impureConfigMerger "{}" ".[0] * .[1]" "${config.xdg.configHome}/zed/settings.json" ( + impureConfigMerger "{}" "$dynamic * $static" "${config.xdg.configHome}/zed/settings.json" ( jsonFormat.generate "zed-user-settings" mergedSettings ) ); @@ -178,7 +181,7 @@ in (mkIf (cfg.userKeymaps != [ ]) { zedKeymapActivation = lib.hm.dag.entryAfter [ "linkGeneration" ] ( impureConfigMerger "[]" - ".[0] + .[1] | group_by(.context) | map(reduce .[] as $item ({}; . * $item))" + "$dynamic + $static | group_by(.context) | map(reduce .[] as $item ({}; . * $item))" "${config.xdg.configHome}/zed/keymap.json" (jsonFormat.generate "zed-user-keymaps" cfg.userKeymaps) ); diff --git a/tests/modules/programs/zed-editor/keymap.nix b/tests/modules/programs/zed-editor/keymap.nix index 03a3a3fc..c83da741 100644 --- a/tests/modules/programs/zed-editor/keymap.nix +++ b/tests/modules/programs/zed-editor/keymap.nix @@ -30,6 +30,7 @@ let preexistingKeymaps = builtins.toFile "preexisting.json" '' [ + // Things changed interactively { "bindings": { "down": "menu::SelectNext" @@ -41,12 +42,14 @@ }, "context": "Terminal" }, + + /* Manually changed */ { "bindings": { "enter": "newline" }, - "context": "Editor" - } + "context": "Editor", + }, ] ''; diff --git a/tests/modules/programs/zed-editor/settings.nix b/tests/modules/programs/zed-editor/settings.nix index 2e95507e..25b34d32 100644 --- a/tests/modules/programs/zed-editor/settings.nix +++ b/tests/modules/programs/zed-editor/settings.nix @@ -27,7 +27,10 @@ let preexistingSettings = builtins.toFile "preexisting.json" '' { + // I chose this theme interactively "theme": "Default", + + /* I change AI settings interactively */ "features": { "copilot": true, "ai_assist": true