Skip to content

Commit b1308b5

Browse files
authored
feat(wrapperModules.starship): init (#488)
1 parent 6316917 commit b1308b5

2 files changed

Lines changed: 212 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
pkgs,
3+
self,
4+
tlib,
5+
...
6+
}:
7+
8+
let
9+
inherit (tlib)
10+
fileContains
11+
isFile
12+
test
13+
;
14+
wm = self.wrappers.starship;
15+
in
16+
test { wrapper = "starship"; } {
17+
18+
"wrapper should output correct version" =
19+
let
20+
wrapper = wm.wrap {
21+
inherit pkgs;
22+
};
23+
in
24+
''
25+
"${wrapper}/bin/starship" --version |
26+
grep -q "${wrapper.version}"
27+
'';
28+
29+
"starship.toml is properly configured" =
30+
let
31+
wrapper = wm.wrap {
32+
inherit pkgs;
33+
};
34+
configFile = "${wrapper}/starship.toml";
35+
in
36+
[
37+
(isFile configFile)
38+
(fileContains "${wrapper}/bin/starship" "STARSHIP_CONFIG.*${configFile}")
39+
];
40+
41+
"default settings order is respected" =
42+
let
43+
# See here for preset values: https://github.com/starship/starship/blob/main/docs/public/presets/toml/tokyo-night.toml#L22
44+
wrapper = wm.wrap {
45+
inherit pkgs;
46+
preset = "tokyo-night";
47+
settings.directory.style = "foo";
48+
};
49+
configFile = "${wrapper}/starship.toml";
50+
in
51+
[
52+
(isFile configFile)
53+
(fileContains configFile ''truncation_symbol = "…/"'')
54+
(fileContains configFile ''style = "foo"'')
55+
];
56+
57+
"custom settings order is respected" =
58+
let
59+
# See here for preset values: https://github.com/starship/starship/blob/main/docs/public/presets/toml/tokyo-night.toml#L22
60+
wrapper = wm.wrap {
61+
inherit pkgs;
62+
order = [
63+
"settings"
64+
"preset"
65+
];
66+
preset = "tokyo-night";
67+
settings.directory.style = "foo";
68+
};
69+
configFile = "${wrapper}/starship.toml";
70+
in
71+
[
72+
(isFile configFile)
73+
(fileContains configFile ''truncation_symbol = "…/"'')
74+
(fileContains configFile ''style = "fg:.* bg:.*"'')
75+
];
76+
77+
"preset list is merged in order" =
78+
let
79+
wrapper = wm.wrap {
80+
inherit pkgs;
81+
preset = [
82+
"jetpack"
83+
"pastel-powerline"
84+
];
85+
};
86+
configFile = "${wrapper}/starship.toml";
87+
in
88+
[
89+
(isFile configFile)
90+
# value exclusive to jetpack
91+
(fileContains configFile ''repo_root_style = "bold blue"'')
92+
# jetpack sets it to 2, pastel-powerline overrides it to 3
93+
(fileContains configFile "truncation_length = 3")
94+
];
95+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
{
2+
wlib,
3+
lib,
4+
config,
5+
pkgs,
6+
...
7+
}:
8+
let
9+
tomlFmt = pkgs.formats.toml { };
10+
11+
presetKey = "preset";
12+
settingsKey = "settings";
13+
14+
defaultOrder = [
15+
presetKey
16+
settingsKey
17+
];
18+
in
19+
{
20+
imports = [ wlib.modules.default ];
21+
22+
options = {
23+
settings = lib.mkOption {
24+
inherit (tomlFmt) type;
25+
default = { };
26+
description = ''
27+
Pure nix configuration of starship.toml.
28+
See <https://starship.rs/config/>
29+
'';
30+
example = {
31+
directory.format = "[ $path ]($style)";
32+
};
33+
};
34+
preset = lib.mkOption {
35+
type = with lib.types; either str (listOf str);
36+
default = [ ];
37+
apply = lib.toList;
38+
description = ''
39+
One or more built-in starship presets to use as configuration.
40+
When a list is provided, presets later in the list take precedence.
41+
See <https://starship.rs/presets/>.
42+
'';
43+
example = [
44+
"nerd-font-symbols"
45+
"tokyo-night"
46+
];
47+
};
48+
order = lib.mkOption {
49+
type = with lib.types; wlib.types.fixedList 2 (enum defaultOrder);
50+
default = defaultOrder;
51+
description = ''
52+
The order in which the specified settings are merged.
53+
Values later in the list will take precedence.
54+
55+
The allowed keys are:
56+
57+
- "${presetKey}": Settings from the the specified preset (`config.preset`)
58+
- "${settingsKey}": Settings specified as a nix attrs (`config.settings`)
59+
'';
60+
};
61+
};
62+
config = {
63+
package = lib.mkDefault pkgs.starship;
64+
constructFiles."starship.toml" = {
65+
content = builtins.toJSON config.settings;
66+
relPath = "starship.toml";
67+
builder =
68+
let
69+
# Holds the bash variable name to be used inside the builder script
70+
nixSettingsTomlPathVar = "nix_settings_toml";
71+
orderedSettings =
72+
let
73+
tomlSettingsMap = {
74+
${presetKey} = map (
75+
p: lib.escapeShellArg "${config.package}/share/starship/presets/${p}.toml"
76+
) config.preset;
77+
${settingsKey} = lib.optional (config.settings != { }) "\$${nixSettingsTomlPathVar}";
78+
};
79+
in
80+
lib.concatMap (key: tomlSettingsMap.${key}) config.order;
81+
in
82+
# Builds the nix settings TOML file and stores the path in the
83+
# bash variable named `${nixSettingsTomlPathVar}`
84+
lib.optionalString (config.settings != { }) ''
85+
${nixSettingsTomlPathVar}="$(mktemp)"
86+
mkdir -p "$(dirname ''$${nixSettingsTomlPathVar})"
87+
${pkgs.remarshal}/bin/json2toml "$1" "''$${nixSettingsTomlPathVar}"
88+
''
89+
# Merges all specified presets TOML files and the TOML file generated
90+
# from nix settings using tomlq
91+
+ ''
92+
mkdir -p "$(dirname "$2")"
93+
${pkgs.yq}/bin/tomlq -s -t 'reduce .[] as $item ({}; . * $item)' \
94+
${lib.concatStringsSep " " orderedSettings} > "$2"
95+
'';
96+
};
97+
env.STARSHIP_CONFIG = config.constructFiles."starship.toml".path;
98+
meta = {
99+
maintainers = with wlib.maintainers; [
100+
birdee
101+
zenoli
102+
];
103+
description = ''
104+
Wrapper Module for the [Starship Prompt](https://starship.rs/).
105+
106+
Starship is configured via a [TOML file](https://starship.rs/config/).
107+
This module provides two ways to do this:
108+
109+
- By specifying one (or many) of the built-in preset configurations.
110+
- By using pure Nix to write an attribute set that gets converted to TOML.
111+
112+
These two options are not mutually exclusive. If both are defined,
113+
they will be merged according to the order specified in `config.order`.
114+
'';
115+
};
116+
};
117+
}

0 commit comments

Comments
 (0)