1{ config, lib, pkgs, ... }:
2
3let
4 cfg = config.programs.starship;
5
6 settingsFormat = pkgs.formats.toml { };
7
8 userSettingsFile = settingsFormat.generate "starship.toml" cfg.settings;
9
10 settingsFile = if cfg.presets == [] then userSettingsFile else pkgs.runCommand "starship.toml"
11 {
12 nativeBuildInputs = [ pkgs.yq ];
13 } ''
14 tomlq -s -t 'reduce .[] as $item ({}; . * $item)' \
15 ${lib.concatStringsSep " " (map (f: "${cfg.package}/share/starship/presets/${f}.toml") cfg.presets)} \
16 ${userSettingsFile} \
17 > $out
18 '';
19
20 initOption =
21 if cfg.interactiveOnly then
22 "promptInit"
23 else
24 "shellInit";
25
26in
27{
28 options.programs.starship = {
29 enable = lib.mkEnableOption "the Starship shell prompt";
30
31 package = lib.mkPackageOption pkgs "starship" { };
32
33 interactiveOnly = lib.mkEnableOption ''
34 starship only when the shell is interactive.
35 Some plugins require this to be set to false to function correctly
36 '' // { default = true; };
37
38 presets = lib.mkOption {
39 default = [ ];
40 example = [ "nerd-font-symbols" ];
41 type = with lib.types; listOf str;
42 description = ''
43 Presets files to be merged with settings in order.
44 '';
45 };
46
47 settings = lib.mkOption {
48 inherit (settingsFormat) type;
49 default = { };
50 description = ''
51 Configuration included in `starship.toml`.
52
53 See https://starship.rs/config/#prompt for documentation.
54 '';
55 };
56 };
57
58 config = lib.mkIf cfg.enable {
59 programs.bash.${initOption} = ''
60 if [[ $TERM != "dumb" ]]; then
61 # don't set STARSHIP_CONFIG automatically if there's a user-specified
62 # config file. starship appears to use a hardcoded config location
63 # rather than one inside an XDG folder:
64 # https://github.com/starship/starship/blob/686bda1706e5b409129e6694639477a0f8a3f01b/src/configure.rs#L651
65 if [[ ! -f "$HOME/.config/starship.toml" ]]; then
66 export STARSHIP_CONFIG=${settingsFile}
67 fi
68 eval "$(${cfg.package}/bin/starship init bash)"
69 fi
70 '';
71
72 programs.fish.${initOption} = ''
73 if test "$TERM" != "dumb"
74 # don't set STARSHIP_CONFIG automatically if there's a user-specified
75 # config file. starship appears to use a hardcoded config location
76 # rather than one inside an XDG folder:
77 # https://github.com/starship/starship/blob/686bda1706e5b409129e6694639477a0f8a3f01b/src/configure.rs#L651
78 if not test -f "$HOME/.config/starship.toml";
79 set -x STARSHIP_CONFIG ${settingsFile}
80 end
81 eval (${cfg.package}/bin/starship init fish)
82 end
83 '';
84
85 programs.zsh.${initOption} = ''
86 if [[ $TERM != "dumb" ]]; then
87 # don't set STARSHIP_CONFIG automatically if there's a user-specified
88 # config file. starship appears to use a hardcoded config location
89 # rather than one inside an XDG folder:
90 # https://github.com/starship/starship/blob/686bda1706e5b409129e6694639477a0f8a3f01b/src/configure.rs#L651
91 if [[ ! -f "$HOME/.config/starship.toml" ]]; then
92 export STARSHIP_CONFIG=${settingsFile}
93 fi
94 eval "$(${cfg.package}/bin/starship init zsh)"
95 fi
96 '';
97 };
98
99 meta.maintainers = pkgs.starship.meta.maintainers;
100}