tmux-sessionizer.nix 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. { config, lib, pkgs, ... }:
  2. with lib;
  3. let
  4. cfg = config.programs.tmux.sessionizer;
  5. # Generate the configuration file content
  6. configContent = ''
  7. # tmux-sessionizer configuration
  8. ${optionalString (cfg.searchPaths != []) ''
  9. TS_SEARCH_PATHS=(${concatStringsSep " " cfg.searchPaths})
  10. ''}
  11. ${optionalString (cfg.extraSearchPaths != []) ''
  12. TS_EXTRA_SEARCH_PATHS=(${concatStringsSep " " (map (entry:
  13. if entry.depth != null
  14. then ''"${entry.path}:${toString entry.depth}"''
  15. else ''"${entry.path}"''
  16. ) cfg.extraSearchPaths)})
  17. ''}
  18. ${optionalString (cfg.maxDepth != null) ''
  19. TS_MAX_DEPTH=${toString cfg.maxDepth}
  20. ''}
  21. ${optionalString (cfg.sessionCommands != []) ''
  22. TS_SESSION_COMMANDS=(${concatStringsSep " " (map (cmd: ''"${cmd}"'') cfg.sessionCommands)})
  23. ''}
  24. ${optionalString cfg.logging.enable ''
  25. TS_LOG="${cfg.logging.type}"
  26. ${optionalString (cfg.logging.file != null) ''
  27. TS_LOG_FILE="${cfg.logging.file}"
  28. ''}
  29. ''}
  30. ${cfg.extraConfig}
  31. '';
  32. in
  33. {
  34. options.programs.tmux.sessionizer = {
  35. enable = mkEnableOption "tmux-sessionizer integration";
  36. package = mkOption {
  37. type = types.package;
  38. default = pkgs.tmux-sessionizer;
  39. description = "The tmux-sessionizer package to use.";
  40. };
  41. searchPaths = mkOption {
  42. type = types.listOf types.str;
  43. default = [ "~/" "~/personal" "~/personal/dev/env/.config" ];
  44. description = ''
  45. List of paths to search for directories. These override the default search paths.
  46. '';
  47. example = [ "~/" "~/projects" "~/work" ];
  48. };
  49. extraSearchPaths = mkOption {
  50. type = types.listOf (types.submodule {
  51. options = {
  52. path = mkOption {
  53. type = types.str;
  54. description = "Path to search in addition to the main search paths.";
  55. };
  56. depth = mkOption {
  57. type = types.nullOr types.ints.positive;
  58. default = null;
  59. description = "Maximum depth to search in this path.";
  60. };
  61. };
  62. });
  63. default = [ ];
  64. description = ''
  65. Additional search paths with optional depth specification.
  66. '';
  67. example = [
  68. { path = "~/ghq"; depth = 3; }
  69. { path = "~/Git"; depth = 3; }
  70. { path = "~/.config"; depth = 2; }
  71. ];
  72. };
  73. maxDepth = mkOption {
  74. type = types.nullOr types.ints.positive;
  75. default = null;
  76. description = ''
  77. Maximum depth for directory searches. Overrides the default depth of 1.
  78. '';
  79. example = 2;
  80. };
  81. sessionCommands = mkOption {
  82. type = types.listOf types.str;
  83. default = [ ];
  84. description = ''
  85. List of commands that can be executed in tmux sessions using the -s flag.
  86. These commands run in windows with indices starting from 69.
  87. '';
  88. example = [ "nvim" "git status" "npm run dev" ];
  89. };
  90. logging = {
  91. enable = mkEnableOption "logging for tmux-sessionizer";
  92. type = mkOption {
  93. type = types.enum [ "echo" "file" "true" ];
  94. default = "file";
  95. description = ''
  96. Type of logging to use:
  97. - "echo": Print logs to stdout
  98. - "file": Write logs to a file
  99. - "true": Enable logging (same as "file")
  100. '';
  101. };
  102. file = mkOption {
  103. type = types.nullOr types.str;
  104. default = null;
  105. description = ''
  106. Custom log file path. If not specified, defaults to
  107. ~/.local/share/tmux-sessionizer/tmux-sessionizer.logs
  108. '';
  109. example = "~/.cache/tmux-sessionizer.log";
  110. };
  111. };
  112. extraConfig = mkOption {
  113. type = types.lines;
  114. default = "";
  115. description = ''
  116. Extra configuration to add to the tmux-sessionizer config file.
  117. '';
  118. example = ''
  119. # Custom configuration
  120. export CUSTOM_VAR="value"
  121. '';
  122. };
  123. keyBinding = mkOption {
  124. type = types.nullOr types.str;
  125. default = "f";
  126. description = ''
  127. Key binding to launch tmux-sessionizer. Set to null to disable.
  128. '';
  129. example = "f";
  130. };
  131. };
  132. config = mkIf (config.programs.tmux.enable && cfg.enable) {
  133. # Add the package to home packages
  134. home.packages = [ cfg.package ];
  135. # Create the configuration file
  136. xdg.configFile."tmux-sessionizer/tmux-sessionizer.conf" = mkIf (configContent != "") {
  137. text = configContent;
  138. };
  139. # Add key binding to tmux configuration
  140. programs.tmux.extraConfig = mkIf (cfg.keyBinding != null) ''
  141. # tmux-sessionizer key binding
  142. bind-key -r ${cfg.keyBinding} run-shell "tmux neww ${lib.getExe cfg.package}"
  143. '';
  144. };
  145. }