Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

om health: Shell dotfiles are Nix-managed #306

Merged
merged 46 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
11406d4
nix_health: Add Check for Shell Configurations.
rsrohitsingh682 Oct 8, 2024
b68077e
Merge branch 'main' into shell-configurations
rsrohitsingh682 Oct 8, 2024
1b92c1c
nix_health: add comments and improve msgs.
rsrohitsingh682 Oct 8, 2024
d101e14
nix_health: refactor and use struct for dotfiles.
rsrohitsingh682 Oct 10, 2024
5cc216c
nix_health: use enum for Shell.
rsrohitsingh682 Oct 10, 2024
06f7c65
Merge branch 'main' into shell-configurations
rsrohitsingh682 Oct 10, 2024
f985955
nix_health: refactored to reduce complexity.
rsrohitsingh682 Oct 23, 2024
8fdb606
Merge branch 'main' into shell-configurations
rsrohitsingh682 Oct 23, 2024
ab472b8
nix_health: Don't panic if shell is not supported.
rsrohitsingh682 Oct 23, 2024
76fd955
nix_health: remove unused import
rsrohitsingh682 Oct 23, 2024
446490c
nix_health: use read_link instead of canonicalize for reading symlink
rsrohitsingh682 Oct 24, 2024
4cf05e3
nix_health: add print statements for debugging CI failure on aarch64-…
rsrohitsingh682 Oct 24, 2024
960fd92
nix_health: Don't panic if dotfiles are not found.
rsrohitsingh682 Oct 24, 2024
aa86f83
Merge branch 'main' into shell-configurations
rsrohitsingh682 Oct 24, 2024
b901872
nix_health: refactored and removed extra Shell types; separate functi…
rsrohitsingh682 Oct 25, 2024
5f0bff4
Merge branch 'main' into shell-configurations
srid Oct 29, 2024
4939822
Improve docs
srid Oct 29, 2024
3b18c66
Use &self
srid Oct 29, 2024
8d12420
*do not use* String by habit. Use the approriate type. This also inci…
srid Oct 29, 2024
d68d1d8
Use lossy conversation to obviate error handling
srid Oct 29, 2024
6efdd4f
This error is panic worthy, no point in handling it
srid Oct 29, 2024
0afe5b4
don't allocate strings unnecessarily
srid Oct 29, 2024
5aaa25e
panic out here as well, asking the user to file bug report. we *expec…
srid Oct 29, 2024
65ab62b
decouple intertwined check from shell type
srid Oct 29, 2024
b87379a
Remove redundant Display instance
srid Oct 29, 2024
7331861
Result no more useful
srid Oct 29, 2024
84e1c24
edit suggestion
srid Oct 29, 2024
e73010f
No need to handle error either (will address logic later)
srid Oct 29, 2024
c35b5f0
rewrite faulty logic
srid Oct 29, 2024
583cec3
reomve redundant error types
srid Oct 29, 2024
04dd908
add .zshenv
srid Oct 29, 2024
9a3fd67
fmt
srid Oct 29, 2024
185746e
improve panic msg
srid Oct 29, 2024
74a43c6
simplify logic
srid Oct 29, 2024
cb5b5bb
panic only when required=true
srid Oct 29, 2024
ad5d310
warn: add prefix
srid Oct 30, 2024
1fef170
docs
srid Nov 1, 2024
14548f6
add .zprofile
srid Nov 1, 2024
6f7dfe9
mv type below
srid Nov 1, 2024
3755015
decouple
srid Nov 1, 2024
7d70226
move
srid Nov 1, 2024
ddc2d85
separate functoin confusing here
srid Nov 1, 2024
3e24995
rewrite this to track managed/unmanaged files
srid Nov 1, 2024
0295623
health: display url
srid Nov 1, 2024
b719d67
display managed & unmanaged
srid Nov 1, 2024
a74f31c
add shell too
srid Nov 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ uuid = { version = "1.3.0", features = ["serde", "v4", "js"] }
which = { version = "4.4.2" }
clap_complete = "4.5.0"
whoami = "1.5.2"
dirs = "5.0.1"

[profile.release]
strip = true # Automatically strip symbols from the binary.
Expand Down
1 change: 1 addition & 0 deletions crates/nix_health/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ which = { workspace = true }
bytesize = { workspace = true }
semver = { workspace = true }
serde_repr = { workspace = true }
dirs = { workspace = true }
1 change: 1 addition & 0 deletions crates/nix_health/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ pub mod flake_enabled;
pub mod max_jobs;
pub mod min_nix_version;
pub mod rosetta;
pub mod shell_configurations;
pub mod system;
pub mod trusted_users;
97 changes: 97 additions & 0 deletions crates/nix_health/src/check/shell_configurations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use nix_rs::info;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

use crate::traits::{Check, CheckResult, Checkable};

#[derive(Debug, Serialize, Deserialize, Clone, Hash, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Shell {
Zsh,
Bash,
#[serde(other)]
Unknown,
}

impl Shell {
fn from_path(path: &str) -> Self {
let shell_name = Path::new(path)
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("");
srid marked this conversation as resolved.
Show resolved Hide resolved

match shell_name {
"zsh" => Shell::Zsh,
"bash" => Shell::Bash,
_ => Shell::Unknown,
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(default)]
pub struct ShellConfigurations {
dotfiles: HashMap<Shell, Vec<String>>,
}

impl Default for ShellConfigurations {
fn default() -> Self {
let mut dotfiles = HashMap::new();
dotfiles.insert(Shell::Zsh, vec![".zshrc".to_string()]);
dotfiles.insert(
Shell::Bash,
vec![
".bashrc".to_string(),
".bash_profile".to_string(),
".profile".to_string(),
],
);
Self { dotfiles }
}
}

impl ShellConfigurations {
fn check_shell_configuration(&self) -> bool {
std::env::var("SHELL")
.ok()
.map(|shell| {
let shell_name = Shell::from_path(&shell);
self.are_shell_dotfiles_nix_managed(&shell_name)
})
.unwrap_or(false)
}

fn are_shell_dotfiles_nix_managed(&self, shell: &Shell) -> bool {
dirs::home_dir()
.map(|home| {
self.dotfiles.get(shell).map_or(false, |shell_files| {
shell_files.iter().all(|dotfile| {
std::fs::canonicalize(home.join(dotfile))
.map(|canonical_path| canonical_path.starts_with("/nix/store/"))
.unwrap_or(false)
})
})
})
.unwrap_or(false)
}
}

impl Checkable for ShellConfigurations {
fn check(&self, _: &info::NixInfo, _: Option<&nix_rs::flake::url::FlakeUrl>) -> Vec<Check> {
let check = Check {
title: "Shell Configurations".to_string(),
info: "Dotfiles managed by Nix".to_string(),
result: if self.check_shell_configuration() {
CheckResult::Green
} else {
CheckResult::Red {
msg: "Shell Configurations are not managed by Nix".into(),
suggestion: "Manage shell (zsh, bash) configurations through https://github.com/juspay/nixos-unified-template".into(),
}
},
required: false,
};
vec![check]
}
}
4 changes: 3 additions & 1 deletion crates/nix_health/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use traits::Check;

use self::check::{
caches::Caches, flake_enabled::FlakeEnabled, max_jobs::MaxJobs, min_nix_version::MinNixVersion,
rosetta::Rosetta, trusted_users::TrustedUsers,
rosetta::Rosetta, shell_configurations::ShellConfigurations, trusted_users::TrustedUsers,
};

/// Nix Health check information for user's install
Expand All @@ -35,6 +35,7 @@ pub struct NixHealth {
pub trusted_users: TrustedUsers,
pub rosetta: Rosetta,
pub direnv: Direnv,
pub shell_configurations: ShellConfigurations,
}

impl<'a> IntoIterator for &'a NixHealth {
Expand All @@ -52,6 +53,7 @@ impl<'a> IntoIterator for &'a NixHealth {
&self.caches,
&self.trusted_users,
&self.direnv,
&self.shell_configurations,
];
items.into_iter()
}
Expand Down