1
0

ghostty-darwin.nix 5.6 KB

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