From 5738fa02ecbe45d043a80c5325aba2a61580b1ef Mon Sep 17 00:00:00 2001 From: Muhammad Saghir Date: Fri, 14 Feb 2025 16:22:36 -0500 Subject: [PATCH] zed-editor: add userThemes option Adding support for custom local themes --- modules/programs/zed-editor.nix | 61 ++++++++++++---- tests/flake.lock | 0 tests/modules/programs/zed-editor/default.nix | 1 + .../modules/programs/zed-editor/settings.nix | 6 +- tests/modules/programs/zed-editor/themes.nix | 71 +++++++++++++++++++ 5 files changed, 121 insertions(+), 18 deletions(-) create mode 100644 tests/flake.lock create mode 100644 tests/modules/programs/zed-editor/themes.nix diff --git a/modules/programs/zed-editor.nix b/modules/programs/zed-editor.nix index cfc55c073..771d095d3 100644 --- a/modules/programs/zed-editor.nix +++ b/modules/programs/zed-editor.nix @@ -1,22 +1,25 @@ { config, lib, pkgs, ... }: - with lib; - let cfg = config.programs.zed-editor; jsonFormat = pkgs.formats.json { }; mergedSettings = cfg.userSettings // (lib.optionalAttrs (builtins.length cfg.extensions > 0) { - # this part by @cmacrae + # by @cmacrae auto_install_extensions = lib.genAttrs cfg.extensions (_: true); }); + + buildThemes = themes: + lib.listToAttrs (map (theme: { + name = "zed/themes/${theme.name}.json"; + value = { source = jsonFormat.generate "zed-theme-${theme.name}" theme; }; + }) themes); in { meta.maintainers = [ hm.maintainers.libewa ]; options = { - # TODO: add vscode option parity (installing extensions, configuring - # keybinds with nix etc.) + # TODO: add vscode option parity (installing extensions, configuring, keybinds with nix etc.) programs.zed-editor = { enable = mkEnableOption "Zed, the high performance, multiplayer code editor from the creators of Atom and Tree-sitter"; @@ -61,7 +64,7 @@ in { bindings = { ctrl-shift-t = "workspace::NewTerminal"; }; - }; + } ] ''; description = '' @@ -69,6 +72,32 @@ in { ''; }; + userThemes = mkOption { + type = types.listOf jsonFormat.type; + default = [ ]; + example = literalExpression '' + [ + { + name = "example 1"; + author = "user"; + themes = [ + { + name = "example 1"; + appearance = "dark"; + style = { + background = "#000000"; + }; + } + ]; + } + ]; + ''; + description = '' + Local themes written to {file}`$XDG_CONFIG_HOME/zed/themes`. + See for more information. + ''; + }; + extensions = mkOption { type = types.listOf types.str; default = [ ]; @@ -122,15 +151,19 @@ in { binaryName = "zed-remote-server-stable-${version}"; in { ".zed_server/${binaryName}".source = - lib.getExe' remote_server binaryName; + lib.getExe' remote_server binaryName; }); - xdg.configFile."zed/settings.json" = (mkIf (mergedSettings != { }) { - source = jsonFormat.generate "zed-user-settings" mergedSettings; - }); - - xdg.configFile."zed/keymap.json" = (mkIf (cfg.userKeymaps != { }) { - source = jsonFormat.generate "zed-user-keymaps" cfg.userKeymaps; - }); + xdg.configFile = mkMerge [ + { + "zed/settings.json" = mkIf (mergedSettings != { }) { + source = jsonFormat.generate "zed-user-settings" mergedSettings; + }; + "zed/keymap.json" = mkIf (cfg.userKeymaps != { }) { + source = jsonFormat.generate "zed-user-keymaps" cfg.userKeymaps; + }; + } + (lib.mkIf (cfg.userThemes != [ ]) (buildThemes cfg.userThemes)) + ]; }; } diff --git a/tests/flake.lock b/tests/flake.lock new file mode 100644 index 000000000..e69de29bb diff --git a/tests/modules/programs/zed-editor/default.nix b/tests/modules/programs/zed-editor/default.nix index 2fcd8da2c..39d474a85 100644 --- a/tests/modules/programs/zed-editor/default.nix +++ b/tests/modules/programs/zed-editor/default.nix @@ -3,4 +3,5 @@ zed-install-remote-server = ./install-remote-server.nix; zed-keymap = ./keymap.nix; zed-settings = ./settings.nix; + zed-themes = ./themes.nix; } diff --git a/tests/modules/programs/zed-editor/settings.nix b/tests/modules/programs/zed-editor/settings.nix index e6f077423..19a15a3a0 100644 --- a/tests/modules/programs/zed-editor/settings.nix +++ b/tests/modules/programs/zed-editor/settings.nix @@ -1,7 +1,5 @@ -# Test custom keymap functionality -{ config, ... }: - -{ +# Test custom settings functionality +{ config, ... }: { programs.zed-editor = { enable = true; package = config.lib.test.mkStubPackage { }; diff --git a/tests/modules/programs/zed-editor/themes.nix b/tests/modules/programs/zed-editor/themes.nix new file mode 100644 index 000000000..c2816d890 --- /dev/null +++ b/tests/modules/programs/zed-editor/themes.nix @@ -0,0 +1,71 @@ +# Test custom theme functionality +{ config, ... }: { + programs.zed-editor = { + enable = true; + package = config.lib.test.mkStubPackage { }; + userThemes = [{ + name = "Test"; + author = "user"; + themes = [{ + name = "Test Catppuccin"; + appearance = "light"; + style = { + editor = { + foreground = "#4c4f69"; + background = "#eff1f5"; + gutter.background = "#eff1f5"; + subheader.background = "#e6e9ef"; + active_line.background = "#4c4f690d"; + highlighted_line.background = null; + line_number = "#8c8fa1"; + active_line_number = "#8839ef"; + invisible = "#7c7f9366"; + wrap_guide = "#acb0be"; + }; + }; + }]; + }]; + }; + + nmt.script = let + expectedContent = builtins.toFile "expected.json" '' + { + "author": "user", + "name": "test", + "themes": [ + { + "appearance": "light", + "name": "Test Catppuccin", + "style": { + "editor": { + "active_line": { + "background": "#4c4f690d" + }, + "active_line_number": "#8839ef", + "background": "#eff1f5", + "foreground": "#4c4f69", + "gutter": { + "background": "#eff1f5" + }, + "highlighted_line": { + "background": null + }, + "invisible": "#7c7f9366", + "line_number": "#8c8fa1", + "subheader": { + "background": "#e6e9ef" + }, + "wrap_guide": "#acb0be" + } + } + } + ] + } + ''; + + testPath = ".config/zed/themes/test.json"; + in '' + assertFileExists "home-files/${testPath}" + assertFileContent "home-files/${testPath}" "${expectedContent}" + ''; +}