2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig};
|
2019-11-06 12:59:12 +00:00
|
|
|
|
|
|
|
use crate::configs::nix_shell::NixShellConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-08-25 15:41:20 +00:00
|
|
|
|
2022-12-27 13:59:40 +00:00
|
|
|
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 {
|
2023-01-27 09:33:24 +00:00
|
|
|
fn detect_shell_type(use_heuristic: bool, context: &Context) -> Option<Self> {
|
|
|
|
use NixShellType::{Impure, Pure, Unknown};
|
2022-12-27 13:59:40 +00:00
|
|
|
|
|
|
|
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 {
|
2023-12-30 16:29:37 +00:00
|
|
|
Self::in_new_nix_shell(context).map(|()| Unknown)
|
2022-12-27 13:59:40 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:42:46 +00:00
|
|
|
// Hack to detect if we're in a `nix shell` (in contrast to a `nix-shell`).
|
2022-12-27 13:59:40 +00:00
|
|
|
// 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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 15:41:20 +00:00
|
|
|
/// Creates a module showing if inside a nix-shell
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
2023-03-22 18:42:46 +00:00
|
|
|
/// The following options are available:
|
2022-12-27 13:59:40 +00:00
|
|
|
/// - `impure_msg` (string) // change the impure msg
|
|
|
|
/// - `pure_msg` (string) // change the pure msg
|
|
|
|
/// - `unknown_msg` (string) // change the unknown message
|
2019-08-25 15:41:20 +00:00
|
|
|
///
|
|
|
|
/// Will display the following:
|
2020-07-07 22:45:32 +00:00
|
|
|
/// - pure (name) // $name == "name" in a pure nix-shell
|
|
|
|
/// - impure (name) // $name == "name" in an impure nix-shell
|
|
|
|
/// - pure // $name == "" in a pure nix-shell
|
|
|
|
/// - impure // $name == "" in an impure nix-shell
|
2022-12-27 13:59:40 +00:00
|
|
|
/// - unknown (name) // $name == "name" in an unknown nix-shell
|
|
|
|
/// - unknown // $name == "" in an unknown nix-shell
|
2019-08-25 15:41:20 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("nix_shell");
|
2019-11-06 12:59:12 +00:00
|
|
|
let config: NixShellConfig = NixShellConfig::try_load(module.config);
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let shell_name = context.get_env("name");
|
2022-12-27 13:59:40 +00:00
|
|
|
let shell_type = NixShellType::detect_shell_type(config.heuristic, context)?;
|
|
|
|
let shell_type_format = match shell_type {
|
|
|
|
NixShellType::Pure => config.pure_msg,
|
|
|
|
NixShellType::Impure => config.impure_msg,
|
|
|
|
NixShellType::Unknown => config.unknown_msg,
|
2019-11-06 12:59:12 +00:00
|
|
|
};
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
"state" => Some(shell_type_format),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"name" => shell_name.as_ref().map(Ok),
|
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `nix_shell`:\n{}", error);
|
|
|
|
return None;
|
2019-11-06 12:59:12 +00:00
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
2019-08-25 15:41:20 +00:00
|
|
|
|
2019-11-06 12:59:12 +00:00
|
|
|
Some(module)
|
2019-08-25 15:41:20 +00:00
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::test::ModuleRenderer;
|
2022-09-04 16:44:54 +00:00
|
|
|
use nu_ansi_term::Color;
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn no_env_variables() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nix_shell").collect();
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn invalid_env_variables() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nix_shell")
|
|
|
|
.env("IN_NIX_SHELL", "something_wrong")
|
|
|
|
.collect();
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn pure_shell() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nix_shell")
|
|
|
|
.env("IN_NIX_SHELL", "pure")
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!("via {} ", Color::Blue.bold().paint("❄️ pure")));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn impure_shell() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nix_shell")
|
|
|
|
.env("IN_NIX_SHELL", "impure")
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!("via {} ", Color::Blue.bold().paint("❄️ impure")));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn pure_shell_name() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nix_shell")
|
|
|
|
.env("IN_NIX_SHELL", "pure")
|
|
|
|
.env("name", "starship")
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"via {} ",
|
|
|
|
Color::Blue.bold().paint("❄️ pure (starship)")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn impure_shell_name() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nix_shell")
|
|
|
|
.env("IN_NIX_SHELL", "impure")
|
|
|
|
.env("name", "starship")
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"via {} ",
|
|
|
|
Color::Blue.bold().paint("❄️ impure (starship)")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2022-12-27 13:59:40 +00:00
|
|
|
|
|
|
|
#[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);
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
}
|