ghostty.nix 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. { config, lib, pkgs, ... }:
  2. let
  3. cfg = config.programs.ghostty-darwin;
  4. keyValueSettings = {
  5. listsAsDuplicateKeys = true;
  6. mkKeyValue = lib.generators.mkKeyValueDefault { } " = ";
  7. };
  8. keyValue = pkgs.formats.keyValue keyValueSettings;
  9. in {
  10. meta.maintainers = [ lib.maintainers.HeitorAugustoLN ];
  11. options.programs.ghostty-darwin = {
  12. enable = lib.mkEnableOption "Ghostty";
  13. package = lib.mkPackageOption pkgs "ghostty" { };
  14. settings = lib.mkOption {
  15. inherit (keyValue) type;
  16. default = { };
  17. example = lib.literalExpression ''
  18. {
  19. theme = "catppuccin-mocha";
  20. font-size = 10;
  21. keybind = [
  22. "ctrl+h=goto_split:left"
  23. "ctrl+l=goto_split:right"
  24. ];
  25. }
  26. '';
  27. description = ''
  28. Configuration written to {file}`$XDG_CONFIG_HOME/ghostty/config`.
  29. See <https://ghostty.org/docs/config/reference> for more information.
  30. '';
  31. };
  32. themes = lib.mkOption {
  33. type = lib.types.attrsOf keyValue.type;
  34. default = { };
  35. example = {
  36. catppuccin-mocha = {
  37. palette = [
  38. "0=#45475a"
  39. "1=#f38ba8"
  40. "2=#a6e3a1"
  41. "3=#f9e2af"
  42. "4=#89b4fa"
  43. "5=#f5c2e7"
  44. "6=#94e2d5"
  45. "7=#bac2de"
  46. "8=#585b70"
  47. "9=#f38ba8"
  48. "10=#a6e3a1"
  49. "11=#f9e2af"
  50. "12=#89b4fa"
  51. "13=#f5c2e7"
  52. "14=#94e2d5"
  53. "15=#a6adc8"
  54. ];
  55. background = "1e1e2e";
  56. foreground = "cdd6f4";
  57. cursor-color = "f5e0dc";
  58. selection-background = "353749";
  59. selection-foreground = "cdd6f4";
  60. };
  61. };
  62. description = ''
  63. Custom themes written to {file}`$XDG_CONFIG_HOME/ghostty/themes`.
  64. See <https://ghostty.org/docs/features/theme#authoring-a-custom-theme> for more information.
  65. '';
  66. };
  67. clearDefaultKeybinds = lib.mkEnableOption "" // {
  68. description = "Whether to clear default keybinds.";
  69. };
  70. installVimSyntax =
  71. lib.mkEnableOption "installation of Ghostty configuration syntax for Vim";
  72. installBatSyntax =
  73. lib.mkEnableOption "installation of Ghostty configuration syntax for bat"
  74. // {
  75. default = true;
  76. };
  77. enableBashIntegration = lib.mkEnableOption ''
  78. bash shell integration.
  79. This is ensures that shell integration works in more scenarios, such as switching shells within Ghostty.
  80. But it is not needed to have shell integration.
  81. See <https://ghostty.org/docs/features/shell-integration#manual-shell-integration-setup> for more information
  82. '';
  83. enableFishIntegration = lib.mkEnableOption ''
  84. fish shell integration.
  85. This is ensures that shell integration works in more scenarios, such as switching shells within Ghostty.
  86. But it is not needed to have shell integration.
  87. See <https://ghostty.org/docs/features/shell-integration#manual-shell-integration-setup> for more information
  88. '';
  89. enableZshIntegration = lib.mkEnableOption ''
  90. zsh shell integration.
  91. This is ensures that shell integration works in more scenarios, such as switching shells within Ghostty.
  92. But it is not needed to have shell integration.
  93. See <https://ghostty.org/docs/features/shell-integration#manual-shell-integration-setup> for more information
  94. '';
  95. };
  96. config = lib.mkIf cfg.enable (lib.mkMerge [
  97. {
  98. programs.ghostty.settings = lib.mkIf cfg.clearDefaultKeybinds {
  99. keybind = lib.mkBefore [ "clear" ];
  100. };
  101. # MacOS also supports XDG configuration directory, so we use it for both
  102. # Linux and macOS to reduce complexity
  103. xdg.configFile = lib.mkMerge [
  104. {
  105. "ghostty/config" = lib.mkIf (cfg.settings != { }) {
  106. source = keyValue.generate "ghostty-config" cfg.settings;
  107. onChange = "ghostty +validate-config";
  108. };
  109. }
  110. (lib.mkIf (cfg.themes != { }) (lib.mapAttrs' (name: value: {
  111. name = "ghostty/themes/${name}";
  112. value.source = keyValue.generate "ghostty-${name}-theme" value;
  113. }) cfg.themes))
  114. ];
  115. }
  116. # (lib.mkIf cfg.installVimSyntax {
  117. # programs.vim.plugins = [ cfg.package.vim ];
  118. # })
  119. # (lib.mkIf cfg.installBatSyntax {
  120. # programs.bat = {
  121. # syntaxes.ghostty = {
  122. # src = cfg.package;
  123. # file = "share/bat/syntaxes/ghostty.sublime-syntax";
  124. # };
  125. # config.map-syntax =
  126. # [ "${config.xdg.configHome}/ghostty/config:Ghostty Config" ];
  127. # };
  128. # })
  129. (lib.mkIf cfg.enableBashIntegration {
  130. # Make order 101 to be placed exactly after bash completions, as Ghostty
  131. # documentation suggests sourcing the script as soon as possible
  132. programs.bash.initExtra = lib.mkOrder 101 ''
  133. if [[ -n "''${GHOSTTY_RESOURCES_DIR}" ]]; then
  134. builtin source "''${GHOSTTY_RESOURCES_DIR}/shell-integration/bash/ghostty.bash"
  135. fi
  136. '';
  137. })
  138. (lib.mkIf cfg.enableFishIntegration {
  139. programs.fish.shellInit = ''
  140. if set -q GHOSTTY_RESOURCES_DIR
  141. source "$GHOSTTY_RESOURCES_DIR/shell-integration/fish/vendor_conf.d/ghostty-shell-integration.fish"
  142. end
  143. '';
  144. })
  145. (lib.mkIf cfg.enableZshIntegration {
  146. programs.zsh.initExtra = ''
  147. if [[ -n $GHOSTTY_RESOURCES_DIR ]]; then
  148. source "$GHOSTTY_RESOURCES_DIR"/shell-integration/zsh/ghostty-integration
  149. fi
  150. '';
  151. })
  152. ]);
  153. }