1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-06-02 08:30:50 +00:00

feat(nix): support new nix shell command (#4724)

* Support `nix shell`

* Remove unnecessary `Debug` implementation

* Add test to detect false positive

* Improve detection of `/nix/store` in $PATH

* Add docs about unknown state

* Gate under `heuristic` flag

* Regenerate config schema
This commit is contained in:
Julian Antonielli 2022-12-27 13:59:40 +00:00 committed by GitHub
parent 9093891acb
commit 19fdf9bba5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 131 additions and 18 deletions

View File

@ -989,10 +989,12 @@
"default": { "default": {
"disabled": false, "disabled": false,
"format": "via [$symbol$state( \\($name\\))]($style) ", "format": "via [$symbol$state( \\($name\\))]($style) ",
"heuristic": false,
"impure_msg": "impure", "impure_msg": "impure",
"pure_msg": "pure", "pure_msg": "pure",
"style": "bold blue", "style": "bold blue",
"symbol": "❄️ " "symbol": "❄️ ",
"unknown_msg": ""
}, },
"allOf": [ "allOf": [
{ {
@ -4066,9 +4068,17 @@
"default": "pure", "default": "pure",
"type": "string" "type": "string"
}, },
"unknown_msg": {
"default": "",
"type": "string"
},
"disabled": { "disabled": {
"default": false, "default": false,
"type": "boolean" "type": "boolean"
},
"heuristic": {
"default": false,
"type": "boolean"
} }
}, },
"additionalProperties": false "additionalProperties": false

View File

@ -2718,14 +2718,16 @@ The module will be shown when inside a nix-shell environment.
### Options ### Options
| Option | Default | Description | | Option | Default | Description |
| ------------ | -------------------------------------------- | ----------------------------------------------------- | | ------------- | -------------------------------------------- | --------------------------------------------------------------------- |
| `format` | `'via [$symbol$state( \($name\))]($style) '` | The format for the module. | | `format` | `'via [$symbol$state( \($name\))]($style) '` | The format for the module. |
| `symbol` | `'❄️ '` | A format string representing the symbol of nix-shell. | | `symbol` | `'❄️ '` | A format string representing the symbol of nix-shell. |
| `style` | `'bold blue'` | The style for the module. | | `style` | `'bold blue'` | The style for the module. |
| `impure_msg` | `'impure'` | A format string shown when the shell is impure. | | `impure_msg` | `'impure'` | A format string shown when the shell is impure. |
| `pure_msg` | `'pure'` | A format string shown when the shell is pure. | | `pure_msg` | `'pure'` | A format string shown when the shell is pure. |
| `disabled` | `false` | Disables the `nix_shell` module. | | `unknown_msg` | `''` | A format string shown when it is unknown if the shell is pure/impure. |
| `disabled` | `false` | Disables the `nix_shell` module. |
| `heuristic` | `false` | Attempts to detect new `nix shell`-style shells with a heuristic. |
### Variables ### Variables
@ -2747,6 +2749,7 @@ The module will be shown when inside a nix-shell environment.
disabled = true disabled = true
impure_msg = '[impure shell](bold red)' impure_msg = '[impure shell](bold red)'
pure_msg = '[pure shell](bold green)' pure_msg = '[pure shell](bold green)'
unknown_msg = '[unknown shell](bold yellow)'
format = 'via [☃️ $state( \($name\))](bold blue) ' format = 'via [☃️ $state( \($name\))](bold blue) '
``` ```

4
src/configs/nix_shell.rs Normal file → Executable file
View File

@ -13,7 +13,9 @@ pub struct NixShellConfig<'a> {
pub style: &'a str, pub style: &'a str,
pub impure_msg: &'a str, pub impure_msg: &'a str,
pub pure_msg: &'a str, pub pure_msg: &'a str,
pub unknown_msg: &'a str,
pub disabled: bool, pub disabled: bool,
pub heuristic: bool,
} }
/* The trailing double spaces in `symbol` are needed to work around issues with /* The trailing double spaces in `symbol` are needed to work around issues with
@ -27,7 +29,9 @@ impl<'a> Default for NixShellConfig<'a> {
style: "bold blue", style: "bold blue",
impure_msg: "impure", impure_msg: "impure",
pure_msg: "pure", pure_msg: "pure",
unknown_msg: "",
disabled: false, disabled: false,
heuristic: false,
} }
} }
} }

View File

@ -3,32 +3,70 @@ use super::{Context, Module, ModuleConfig};
use crate::configs::nix_shell::NixShellConfig; use crate::configs::nix_shell::NixShellConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
enum NixShellType {
Pure,
Impure,
/// We're in a Nix shell, but we don't know which type.
/// This can only happen in a `nix shell` shell (not a `nix-shell` one).
Unknown,
}
impl NixShellType {
fn detect_shell_type(use_heuristic: bool, context: &Context) -> Option<NixShellType> {
use NixShellType::*;
let shell_type = context.get_env("IN_NIX_SHELL");
match shell_type.as_deref() {
Some("pure") => return Some(Pure),
Some("impure") => return Some(Impure),
_ => {}
};
if use_heuristic {
Self::in_new_nix_shell(context).map(|_| Unknown)
} else {
None
}
}
// Hack to detect if we're in a `nix shell` (in constrast to a `nix-shell`).
// A better way to do this will be enabled by https://github.com/NixOS/nix/issues/6677.
fn in_new_nix_shell(context: &Context) -> Option<()> {
let path = context.get_env("PATH")?;
std::env::split_paths(&path)
.any(|path| path.starts_with("/nix/store"))
.then_some(())
}
}
/// Creates a module showing if inside a nix-shell /// Creates a module showing if inside a nix-shell
/// ///
/// The module will use the `$IN_NIX_SHELL` and `$name` environment variable to /// The module will use the `$IN_NIX_SHELL` and `$name` environment variable to
/// determine if it's inside a nix-shell and the name of it. /// determine if it's inside a nix-shell and the name of it.
/// ///
/// The following options are availables: /// The following options are availables:
/// - `impure_msg` (string) // change the impure msg /// - `impure_msg` (string) // change the impure msg
/// - `pure_msg` (string) // change the pure msg /// - `pure_msg` (string) // change the pure msg
/// - `unknown_msg` (string) // change the unknown message
/// ///
/// Will display the following: /// Will display the following:
/// - pure (name) // $name == "name" in a pure nix-shell /// - pure (name) // $name == "name" in a pure nix-shell
/// - impure (name) // $name == "name" in an impure nix-shell /// - impure (name) // $name == "name" in an impure nix-shell
/// - pure // $name == "" in a pure nix-shell /// - pure // $name == "" in a pure nix-shell
/// - impure // $name == "" in an impure nix-shell /// - impure // $name == "" in an impure nix-shell
/// - unknown (name) // $name == "name" in an unknown nix-shell
/// - unknown // $name == "" in an unknown nix-shell
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("nix_shell"); let mut module = context.new_module("nix_shell");
let config: NixShellConfig = NixShellConfig::try_load(module.config); let config: NixShellConfig = NixShellConfig::try_load(module.config);
let shell_name = context.get_env("name"); let shell_name = context.get_env("name");
let shell_type = context.get_env("IN_NIX_SHELL")?; let shell_type = NixShellType::detect_shell_type(config.heuristic, context)?;
let shell_type_format = match shell_type.as_ref() { let shell_type_format = match shell_type {
"impure" => config.impure_msg, NixShellType::Pure => config.pure_msg,
"pure" => config.pure_msg, NixShellType::Impure => config.impure_msg,
_ => { NixShellType::Unknown => config.unknown_msg,
return None;
}
}; };
let parsed = StringFormatter::new(config.format).and_then(|formatter| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
@ -130,4 +168,62 @@ mod tests {
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
#[test]
fn new_nix_shell() {
let actual = ModuleRenderer::new("nix_shell")
.env(
"PATH",
"/nix/store/v7qvqv81jp0cajvrxr9x072jgqc01yhi-nix-info/bin:/Users/user/.cargo/bin",
)
.config(toml::toml! {
[nix_shell]
heuristic = true
})
.collect();
let expected = Some(format!("via {} ", Color::Blue.bold().paint("❄️ ")));
assert_eq!(expected, actual);
}
#[test]
fn no_new_nix_shell() {
let actual = ModuleRenderer::new("nix_shell")
.env("PATH", "/Users/user/.cargo/bin")
.config(toml::toml! {
[nix_shell]
heuristic = true
})
.collect();
let expected = None;
assert_eq!(expected, actual);
}
#[test]
fn no_new_nix_shell_with_nix_store_subdirectory() {
let actual = ModuleRenderer::new("nix_shell")
.env("PATH", "/Users/user/some/nix/store/subdirectory")
.config(toml::toml! {
[nix_shell]
heuristic = true
})
.collect();
let expected = None;
assert_eq!(expected, actual);
}
#[test]
fn no_new_nix_shell_when_heuristic_is_disabled() {
let actual = ModuleRenderer::new("nix_shell")
.env(
"PATH",
"/nix/store/v7qvqv81jp0cajvrxr9x072jgqc01yhi-nix-info/bin:/Users/user/.cargo/bin",
)
.collect();
let expected = None;
assert_eq!(expected, actual);
}
} }