|
| 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