fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
5.4 KiB176 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.powerline-go;
8
9  # Convert an option value to a string to be passed as argument to
10  # powerline-go:
11  valueToString = value:
12    if builtins.isList value then
13      builtins.concatStringsSep "," (builtins.map valueToString value)
14    else if builtins.isAttrs value then
15      valueToString
16      (mapAttrsToList (key: val: "${valueToString key}=${valueToString val}")
17        value)
18    else
19      builtins.toString value;
20
21  modulesArgument = optionalString (cfg.modules != null)
22    " -modules ${valueToString cfg.modules}";
23
24  modulesRightArgument = optionalString (cfg.modulesRight != null)
25    " -modules-right ${valueToString cfg.modulesRight}";
26
27  evalMode = cfg.modulesRight != null;
28
29  evalArgument = optionalString (evalMode) " -eval";
30
31  newlineArgument = optionalString cfg.newline " -newline";
32
33  pathAliasesArgument = optionalString (cfg.pathAliases != null)
34    " -path-aliases ${valueToString cfg.pathAliases}";
35
36  otherSettingPairArgument = name: value:
37    if value == true then " -${name}" else " -${name} ${valueToString value}";
38
39  otherSettingsArgument = optionalString (cfg.settings != { })
40    (concatStringsSep ""
41      (mapAttrsToList otherSettingPairArgument cfg.settings));
42
43  commandLineArguments = ''
44    ${evalArgument}${modulesArgument}${modulesRightArgument}${newlineArgument}${pathAliasesArgument}${otherSettingsArgument}
45  '';
46
47in {
48  meta.maintainers = [ maintainers.DamienCassou ];
49
50  options = {
51    programs.powerline-go = {
52      enable = mkEnableOption
53        "Powerline-go, a beautiful and useful low-latency prompt for your shell";
54
55      modules = mkOption {
56        default = null;
57        type = types.nullOr (types.listOf types.str);
58        description = ''
59          List of module names to load. The list of all available
60          modules as well as the choice of default ones are at
61          <https://github.com/justjanne/powerline-go>.
62        '';
63        example = [ "host" "ssh" "cwd" "gitlite" "jobs" "exit" ];
64      };
65
66      modulesRight = mkOption {
67        default = null;
68        type = types.nullOr (types.listOf types.str);
69        description = ''
70          List of module names to load to be displayed on the right side.
71          Currently not supported by bash. Specifying a value for this
72          option will force powerline-go to use the eval format to set
73          the prompt.
74        '';
75        example = [ "host" "venv" "git" ];
76      };
77
78      newline = mkOption {
79        default = false;
80        type = types.bool;
81        description = ''
82          Set to true if the prompt should be on a line of its own.
83        '';
84        example = true;
85      };
86
87      pathAliases = mkOption {
88        default = null;
89        type = types.nullOr (types.attrsOf types.str);
90        description = ''
91          Pairs of full-path and corresponding desired short name. You
92          may use '~' to represent your home directory but you should
93          protect it to avoid shell substitution.
94        '';
95        example = literalExpression ''
96          { "\\~/projects/home-manager" = "prj:home-manager"; }
97        '';
98      };
99
100      settings = mkOption {
101        default = { };
102        type = with types; attrsOf (oneOf [ bool int str (listOf str) ]);
103        description = ''
104          This can be any key/value pair as described in
105          <https://github.com/justjanne/powerline-go>.
106        '';
107        example = literalExpression ''
108          {
109            hostname-only-if-ssh = true;
110            numeric-exit-codes = true;
111            cwd-max-depth = 7;
112            ignore-repos = [ "/home/me/big-project" "/home/me/huge-project" ];
113          }
114        '';
115      };
116
117      extraUpdatePS1 = mkOption {
118        default = "";
119        description = "Shell code to execute after the prompt is set.";
120        example = ''
121          PS1=$PS1"NixOS> ";
122        '';
123        type = types.str;
124      };
125    };
126  };
127
128  config = {
129    programs.bash.initExtra =
130      mkIf (cfg.enable && config.programs.bash.enable) ''
131        function _update_ps1() {
132          local old_exit_status=$?
133          ${
134            if evalMode then "eval " else "PS1="
135          }"$(${pkgs.powerline-go}/bin/powerline-go -error $old_exit_status -shell bash${commandLineArguments})"
136          ${cfg.extraUpdatePS1}
137          return $old_exit_status
138        }
139
140        if [ "$TERM" != "linux" ]; then
141          PROMPT_COMMAND="_update_ps1;$PROMPT_COMMAND"
142        fi
143      '';
144
145    programs.zsh.initExtra = mkIf (cfg.enable && config.programs.zsh.enable) ''
146      function powerline_precmd() {
147        ${
148          if evalMode then "eval " else "PS1="
149        }"$(${pkgs.powerline-go}/bin/powerline-go -error $? -shell zsh${commandLineArguments})"
150        ${cfg.extraUpdatePS1}
151      }
152
153      function install_powerline_precmd() {
154        for s in "$\{precmd_functions[@]}"; do
155          if [ "$s" = "powerline_precmd" ]; then
156            return
157          fi
158        done
159        precmd_functions+=(powerline_precmd)
160      }
161
162      if [ "$TERM" != "linux" ]; then
163        install_powerline_precmd
164      fi
165    '';
166
167    # https://github.com/justjanne/powerline-go#fish
168    programs.fish.interactiveShellInit =
169      mkIf (cfg.enable && config.programs.fish.enable) ''
170        function fish_prompt
171            eval ${pkgs.powerline-go}/bin/powerline-go -error $status -jobs (count (jobs -p))${commandLineArguments}
172            ${cfg.extraUpdatePS1}
173        end
174      '';
175  };
176}