Skip to content

Commit 2f3f715

Browse files
committed
om health: Nushell support
1 parent 140c293 commit 2f3f715

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

crates/omnix-health/src/check/shell.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use nix_rs::env::OS;
12
use serde::{Deserialize, Serialize};
23
use std::{
4+
borrow::Cow,
35
collections::HashMap,
46
hash::Hash,
57
path::{Path, PathBuf},
@@ -26,13 +28,14 @@ impl Default for ShellCheck {
2628
impl Checkable for ShellCheck {
2729
fn check(
2830
&self,
29-
_nix_info: &nix_rs::info::NixInfo,
31+
nix_info: &nix_rs::info::NixInfo,
3032
_flake: Option<&nix_rs::flake::url::FlakeUrl>,
3133
) -> Vec<(&'static str, Check)> {
3234
if !self.enable {
3335
return vec![];
3436
}
35-
let user_shell_env = match CurrentUserShellEnv::new() {
37+
let os = &nix_info.nix_env.os;
38+
let user_shell_env = match CurrentUserShellEnv::new(os) {
3639
Ok(shell) => shell,
3740
Err(err) => {
3841
tracing::error!("Skipping shell dotfile check! {:?}", err);
@@ -46,8 +49,8 @@ impl Checkable for ShellCheck {
4649
};
4750

4851
// Iterate over each dotfile and check if it is managed by Nix
49-
let mut managed: HashMap<&'static str, PathBuf> = HashMap::new();
50-
let mut unmanaged: HashMap<&'static str, PathBuf> = HashMap::new();
52+
let mut managed: HashMap<String, PathBuf> = HashMap::new();
53+
let mut unmanaged: HashMap<String, PathBuf> = HashMap::new();
5154
for (name, path) in user_shell_env.dotfiles {
5255
if super::direnv::is_path_in_nix_store(&path) {
5356
managed.insert(name, path.clone());
@@ -88,15 +91,15 @@ struct CurrentUserShellEnv {
8891
/// Current shell
8992
shell: Shell,
9093
/// *Absolute* paths to the dotfiles
91-
dotfiles: HashMap<&'static str, PathBuf>,
94+
dotfiles: HashMap<String, PathBuf>,
9295
}
9396

9497
impl CurrentUserShellEnv {
9598
/// Get the current user's shell environment
96-
fn new() -> Result<Self, ShellError> {
99+
fn new(os: &OS) -> Result<Self, ShellError> {
97100
let home = PathBuf::from(std::env::var("HOME")?);
98101
let shell = Shell::current_shell()?;
99-
let dotfiles = shell.get_dotfiles(&home)?;
102+
let dotfiles = shell.get_dotfiles(os, &home)?;
100103
let v = CurrentUserShellEnv {
101104
home,
102105
shell,
@@ -127,6 +130,7 @@ enum ShellError {
127130
enum Shell {
128131
Zsh,
129132
Bash,
133+
Nushell,
130134
}
131135

132136
impl Shell {
@@ -147,25 +151,43 @@ impl Shell {
147151
match shell_name.as_ref() {
148152
"zsh" => Ok(Shell::Zsh),
149153
"bash" => Ok(Shell::Bash),
154+
"nu" => Ok(Shell::Nushell),
150155
_ => Err(ShellError::UnsupportedShell),
151156
}
152157
}
153158

154159
/// Get shell dotfiles
155-
fn dotfile_names(&self) -> Vec<&'static str> {
160+
fn dotfile_names(&self, os: &OS) -> Vec<String> {
156161
match &self {
157-
Shell::Zsh => vec![".zshrc", ".zshenv", ".zprofile", ".zlogin", ".zlogout"],
158-
Shell::Bash => vec![".bashrc", ".bash_profile", ".profile"],
162+
Shell::Zsh => vec![
163+
".zshrc".into(),
164+
".zshenv".into(),
165+
".zprofile".into(),
166+
".zlogin".into(),
167+
".zlogout".into(),
168+
],
169+
Shell::Bash => vec![".bashrc".into(), ".bash_profile".into(), ".profile".into()],
170+
Shell::Nushell => {
171+
let base = match os {
172+
// https://www.nushell.sh/book/configuration.html#configuration-overview
173+
OS::MacOS { .. } => "Library/Application Support/nushell",
174+
_ => ".config/nushell",
175+
};
176+
vec!["env.nu", "config.nu", "login.nu"]
177+
.iter()
178+
.map(|f| format!("{}/{}", base, f).into())
179+
.collect()
180+
}
159181
}
160182
}
161183

162184
/// Get the currently existing dotfiles under $HOME
163185
///
164186
/// Returned paths will be absolute (i.e., symlinks are resolved).
165-
fn get_dotfiles(&self, home_dir: &Path) -> std::io::Result<HashMap<&'static str, PathBuf>> {
187+
fn get_dotfiles(&self, os: &OS, home_dir: &Path) -> std::io::Result<HashMap<String, PathBuf>> {
166188
let mut paths = HashMap::new();
167-
for dotfile in self.dotfile_names() {
168-
match std::fs::canonicalize(home_dir.join(dotfile)) {
189+
for dotfile in self.dotfile_names(os) {
190+
match std::fs::canonicalize(home_dir.join(&dotfile)) {
169191
Ok(path) => {
170192
paths.insert(dotfile, path);
171193
}

0 commit comments

Comments
 (0)