fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
6.6 KiB227 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.kitty;
8
9  eitherStrBoolInt = with types; either str (either bool int);
10
11  optionalPackage = opt:
12    optional (opt != null && opt.package != null) opt.package;
13
14  toKittyConfig = generators.toKeyValue {
15    mkKeyValue = key: value:
16      let
17        value' =
18          (if isBool value then lib.hm.booleans.yesNo else toString) value;
19      in "${key} ${value'}";
20  };
21
22  toKittyKeybindings = generators.toKeyValue {
23    mkKeyValue = key: command: "map ${key} ${command}";
24  };
25
26  toKittyEnv =
27    generators.toKeyValue { mkKeyValue = name: value: "env ${name}=${value}"; };
28
29  shellIntegrationInit = {
30    bash = ''
31      if test -n "$KITTY_INSTALLATION_DIR"; then
32        source "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"
33      fi
34    '';
35    fish = ''
36      if set -q KITTY_INSTALLATION_DIR
37        source "$KITTY_INSTALLATION_DIR/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish"
38        set --prepend fish_complete_path "$KITTY_INSTALLATION_DIR/shell-integration/fish/vendor_completions.d"
39      end
40    '';
41    zsh = ''
42      if test -n "$KITTY_INSTALLATION_DIR"; then
43        autoload -Uz -- "$KITTY_INSTALLATION_DIR"/shell-integration/zsh/kitty-integration
44        kitty-integration
45        unfunction kitty-integration
46      fi
47    '';
48  };
49
50  shellIntegrationDefaultOpt = {
51    default = cfg.shellIntegration.mode != "disabled";
52    defaultText = literalExpression ''
53      config.programs.kitty.shellIntegration.mode != "disabled"
54    '';
55  };
56in {
57  options.programs.kitty = {
58    enable = mkEnableOption "Kitty terminal emulator";
59
60    package = mkOption {
61      type = types.package;
62      default = pkgs.kitty;
63      defaultText = literalExpression "pkgs.kitty";
64      description = ''
65        Kitty package to install.
66      '';
67    };
68
69    darwinLaunchOptions = mkOption {
70      type = types.nullOr (types.listOf types.str);
71      default = null;
72      description = "Command-line options to use when launched by Mac OS GUI";
73      example = literalExpression ''
74        [
75          "--single-instance"
76          "--directory=/tmp/my-dir"
77          "--listen-on=unix:/tmp/my-socket"
78        ]
79      '';
80    };
81
82    settings = mkOption {
83      type = types.attrsOf eitherStrBoolInt;
84      default = { };
85      example = literalExpression ''
86        {
87          scrollback_lines = 10000;
88          enable_audio_bell = false;
89          update_check_interval = 0;
90        }
91      '';
92      description = ''
93        Configuration written to
94        {file}`$XDG_CONFIG_HOME/kitty/kitty.conf`. See
95        <https://sw.kovidgoyal.net/kitty/conf.html>
96        for the documentation.
97      '';
98    };
99
100    theme = mkOption {
101      type = types.nullOr types.str;
102      default = null;
103      description = ''
104        Apply a Kitty color theme. This option takes the friendly name of
105        any theme given by the command {command}`kitty +kitten themes`.
106        See <https://github.com/kovidgoyal/kitty-themes>
107        for more details.
108      '';
109      example = "Space Gray Eighties";
110    };
111
112    font = mkOption {
113      type = types.nullOr hm.types.fontType;
114      default = null;
115      description = "The font to use.";
116    };
117
118    keybindings = mkOption {
119      type = types.attrsOf types.str;
120      default = { };
121      description = "Mapping of keybindings to actions.";
122      example = literalExpression ''
123        {
124          "ctrl+c" = "copy_or_interrupt";
125          "ctrl+f>2" = "set_font_size 20";
126        }
127      '';
128    };
129
130    environment = mkOption {
131      type = types.attrsOf types.str;
132      default = { };
133      description = "Environment variables to set or override.";
134      example = literalExpression ''
135        {
136          "LS_COLORS" = "1";
137        }
138      '';
139    };
140
141    shellIntegration = {
142      mode = mkOption {
143        type = types.str;
144        default = "enabled";
145        example = "no-cursor";
146        description = ''
147          Set the mode of the shell integration. This accepts the same options
148          as the `shell_integration` option of Kitty. Note that
149          `no-rc` is always implied. See
150          <https://sw.kovidgoyal.net/kitty/shell-integration>
151          for more details.
152        '';
153      };
154
155      enableBashIntegration = mkEnableOption "Kitty Bash integration"
156        // shellIntegrationDefaultOpt;
157
158      enableFishIntegration = mkEnableOption "Kitty fish integration"
159        // shellIntegrationDefaultOpt;
160
161      enableZshIntegration = mkEnableOption "Kitty Z Shell integration"
162        // shellIntegrationDefaultOpt;
163    };
164
165    extraConfig = mkOption {
166      default = "";
167      type = types.lines;
168      description = "Additional configuration to add.";
169    };
170  };
171
172  config = mkIf cfg.enable {
173    home.packages = [ cfg.package ] ++ optionalPackage cfg.font;
174
175    xdg.configFile."kitty/kitty.conf" = {
176      text = ''
177        # Generated by Home Manager.
178        # See https://sw.kovidgoyal.net/kitty/conf.html
179      '' + concatStringsSep "\n" ([
180
181        (optionalString (cfg.font != null) ''
182          font_family ${cfg.font.name}
183          ${optionalString (cfg.font.size != null)
184          "font_size ${toString cfg.font.size}"}
185        '')
186
187        (optionalString (cfg.theme != null) ''
188          include ${pkgs.kitty-themes}/share/kitty-themes/${
189            let
190              matching = filter (x: x.name == cfg.theme) (builtins.fromJSON
191                (builtins.readFile
192                  "${pkgs.kitty-themes}/share/kitty-themes/themes.json"));
193            in throwIf (length matching == 0)
194            "kitty-themes does not contain a theme named ${cfg.theme}"
195            (head matching).file
196          }
197        '')
198        ''
199          # Shell integration is sourced and configured manually
200          shell_integration no-rc ${cfg.shellIntegration.mode}
201        ''
202        (toKittyConfig cfg.settings)
203        (toKittyKeybindings cfg.keybindings)
204        (toKittyEnv cfg.environment)
205        cfg.extraConfig
206      ]);
207    } // optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
208      onChange = ''
209        ${pkgs.procps}/bin/pkill -USR1 -u $USER kitty || true
210      '';
211    };
212
213    xdg.configFile."kitty/macos-launch-services-cmdline" = mkIf
214      (cfg.darwinLaunchOptions != null && pkgs.stdenv.hostPlatform.isDarwin) {
215        text = concatStringsSep " " cfg.darwinLaunchOptions;
216      };
217
218    programs.bash.initExtra =
219      mkIf cfg.shellIntegration.enableBashIntegration shellIntegrationInit.bash;
220
221    programs.fish.interactiveShellInit =
222      mkIf cfg.shellIntegration.enableFishIntegration shellIntegrationInit.fish;
223
224    programs.zsh.initExtra =
225      mkIf cfg.shellIntegration.enableZshIntegration shellIntegrationInit.zsh;
226  };
227}