fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
3.8 KiB140 linesraw
1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.atuin;
8
9  tomlFormat = pkgs.formats.toml { };
10
11in {
12  meta.maintainers = [ maintainers.hawkw ];
13
14  options.programs.atuin = {
15    enable = mkEnableOption "atuin";
16
17    package = mkOption {
18      type = types.package;
19      default = pkgs.atuin;
20      defaultText = literalExpression "pkgs.atuin";
21      description = "The package to use for atuin.";
22    };
23
24    enableBashIntegration = mkOption {
25      type = types.bool;
26      default = true;
27      description = ''
28        Whether to enable Atuin's Bash integration. This will bind
29        `ctrl-r` to open the Atuin history.
30      '';
31    };
32
33    enableZshIntegration = mkOption {
34      type = types.bool;
35      default = true;
36      description = ''
37        Whether to enable Atuin's Zsh integration.
38
39        If enabled, this will bind `ctrl-r` and the up-arrow
40        key to open the Atuin history.
41      '';
42    };
43
44    enableFishIntegration = mkOption {
45      default = true;
46      type = types.bool;
47      description = ''
48        Whether to enable Atuin's Fish integration.
49
50        If enabled, this will bind the up-arrow key to open the Atuin history.
51      '';
52    };
53
54    flags = mkOption {
55      default = [ ];
56      type = types.listOf types.str;
57      example = [ "--disable-up-arrow" "--disable-ctrl-r" ];
58      description = ''
59        Flags to append to the shell hook.
60      '';
61    };
62
63    settings = mkOption {
64      type = with types;
65        let
66          prim = oneOf [ bool int str ];
67          primOrPrimAttrs = either prim (attrsOf prim);
68          entry = either prim (listOf primOrPrimAttrs);
69          entryOrAttrsOf = t: either entry (attrsOf t);
70          entries = entryOrAttrsOf (entryOrAttrsOf entry);
71        in attrsOf entries // { description = "Atuin configuration"; };
72      default = { };
73      example = literalExpression ''
74        {
75          auto_sync = true;
76          sync_frequency = "5m";
77          sync_address = "https://api.atuin.sh";
78          search_mode = "prefix";
79        }
80      '';
81      description = ''
82        Configuration written to
83        {file}`$XDG_CONFIG_HOME/atuin/config.toml`.
84
85        See <https://atuin.sh/docs/config/> for the full list
86        of options.
87      '';
88    };
89
90    enableNushellIntegration = mkOption {
91      default = true;
92      type = types.bool;
93      description = ''
94        Whether to enable Nushell integration.
95      '';
96    };
97  };
98
99  config = let flagsStr = escapeShellArgs cfg.flags;
100  in mkIf cfg.enable {
101
102    # Always add the configured `atuin` package.
103    home.packages = [ cfg.package ];
104
105    # If there are user-provided settings, generate the config file.
106    xdg.configFile."atuin/config.toml" = mkIf (cfg.settings != { }) {
107      source = tomlFormat.generate "atuin-config" cfg.settings;
108    };
109
110    programs.bash.initExtra = mkIf cfg.enableBashIntegration ''
111      if [[ :$SHELLOPTS: =~ :(vi|emacs): ]]; then
112        source "${pkgs.bash-preexec}/share/bash/bash-preexec.sh"
113        eval "$(${cfg.package}/bin/atuin init bash ${flagsStr})"
114      fi
115    '';
116
117    programs.zsh.initExtra = mkIf cfg.enableZshIntegration ''
118      if [[ $options[zle] = on ]]; then
119        eval "$(${cfg.package}/bin/atuin init zsh ${flagsStr})"
120      fi
121    '';
122
123    programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration ''
124      ${cfg.package}/bin/atuin init fish ${flagsStr} | source
125    '';
126
127    programs.nushell = mkIf cfg.enableNushellIntegration {
128      extraEnv = ''
129        let atuin_cache = "${config.xdg.cacheHome}/atuin"
130        if not ($atuin_cache | path exists) {
131          mkdir $atuin_cache
132        }
133        ${cfg.package}/bin/atuin init nu ${flagsStr} | save --force ${config.xdg.cacheHome}/atuin/init.nu
134      '';
135      extraConfig = ''
136        source ${config.xdg.cacheHome}/atuin/init.nu
137      '';
138    };
139  };
140}