fork/home-manager

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

David Knaackstarship: use `use` instead of `source` for nushell6f882433

master
4.0 KiB144 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.starship;
8
9  tomlFormat = pkgs.formats.toml { };
10
11  starshipCmd = "${config.home.profileDirectory}/bin/starship";
12
13in {
14  meta.maintainers = [ ];
15
16  options.programs.starship = {
17    enable = mkEnableOption "starship";
18
19    package = mkOption {
20      type = types.package;
21      default = pkgs.starship;
22      defaultText = literalExpression "pkgs.starship";
23      description = "The package to use for the starship binary.";
24    };
25
26    settings = mkOption {
27      type = with types;
28        let
29          prim = either bool (either int str);
30          primOrPrimAttrs = either prim (attrsOf prim);
31          entry = either prim (listOf primOrPrimAttrs);
32          entryOrAttrsOf = t: either entry (attrsOf t);
33          entries = entryOrAttrsOf (entryOrAttrsOf entry);
34        in attrsOf entries // { description = "Starship configuration"; };
35      default = { };
36      example = literalExpression ''
37        {
38          add_newline = false;
39          format = lib.concatStrings [
40            "$line_break"
41            "$package"
42            "$line_break"
43            "$character"
44          ];
45          scan_timeout = 10;
46          character = {
47            success_symbol = "➜";
48            error_symbol = "➜";
49          };
50        }
51      '';
52      description = ''
53        Configuration written to
54        {file}`$XDG_CONFIG_HOME/starship.toml`.
55
56        See <https://starship.rs/config/> for the full list
57        of options.
58      '';
59    };
60
61    enableBashIntegration = mkEnableOption "Bash integration" // {
62      default = true;
63    };
64
65    enableZshIntegration = mkEnableOption "Zsh integration" // {
66      default = true;
67    };
68
69    enableFishIntegration = mkEnableOption "Fish integration" // {
70      default = true;
71    };
72
73    enableIonIntegration = mkEnableOption "Ion integration" // {
74      default = true;
75    };
76
77    enableNushellIntegration = mkEnableOption "Nushell integration" // {
78      default = true;
79    };
80
81    enableTransience = mkOption {
82      type = types.bool;
83      default = false;
84      description = ''
85        The TransientPrompt feature of Starship replaces previous prompts with a
86        custom string. This is only a valid option for the Fish shell.
87
88        For documentation on how to change the default replacement string and
89        for more information visit
90        https://starship.rs/advanced-config/#transientprompt-and-transientrightprompt-in-cmd
91      '';
92    };
93  };
94
95  config = mkIf cfg.enable {
96    home.packages = [ cfg.package ];
97
98    xdg.configFile."starship.toml" = mkIf (cfg.settings != { }) {
99      source = tomlFormat.generate "starship-config" cfg.settings;
100    };
101
102    programs.bash.initExtra = mkIf cfg.enableBashIntegration ''
103      if [[ $TERM != "dumb" ]]; then
104        eval "$(${starshipCmd} init bash --print-full-init)"
105      fi
106    '';
107
108    programs.zsh.initExtra = mkIf cfg.enableZshIntegration ''
109      if [[ $TERM != "dumb" ]]; then
110        eval "$(${starshipCmd} init zsh)"
111      fi
112    '';
113
114    programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration ''
115      if test "$TERM" != "dumb"
116        eval (${starshipCmd} init fish)
117        ${lib.optionalString cfg.enableTransience "enable_transience"}
118      end
119    '';
120
121    programs.ion.initExtra = mkIf cfg.enableIonIntegration ''
122      if test $TERM != "dumb"
123        eval $(${starshipCmd} init ion)
124      end
125    '';
126
127    programs.nushell = mkIf cfg.enableNushellIntegration {
128      # Unfortunately nushell doesn't allow conditionally sourcing nor
129      # conditionally setting (global) environment variables, which is why the
130      # check for terminal compatibility (as seen above for the other shells) is
131      # not done here.
132      extraEnv = ''
133        let starship_cache = "${config.xdg.cacheHome}/starship"
134        if not ($starship_cache | path exists) {
135          mkdir $starship_cache
136        }
137        ${starshipCmd} init nu | save --force ${config.xdg.cacheHome}/starship/init.nu
138      '';
139      extraConfig = ''
140        use ${config.xdg.cacheHome}/starship/init.nu
141      '';
142    };
143  };
144}