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

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

rsrohitsingh682
Copy link
Member

resolves #299

Copy link
Member

@srid srid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rsrohitsingh682 Could you post screenshots of the behaviour on macOS, Linux and on some system where neither bash nor zsh is the current shell?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert the irrelevant changes to this file?

Comment on lines 35 to 37
// Check if shell configurations (zsh or bash) are managed by Nix
are_shell_dotfiles_nix_managed("zsh", &shell_dotfiles)
|| are_shell_dotfiles_nix_managed("bashrc", &shell_dotfiles)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is wrong here. We want to check the current shell, and current shell only.

Also, what happens if the user is using a different shell like fish?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, 'tis time to refactor this (encapsulate) by introducing a struct along with struct methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is wrong here. We want to check the current shell, and current shell only.

@srid I tried using SHELL environment variable for this but it turns out it gives the default shell of the user not the current active shell. There are other ways to determine the current active shell but those commands doesn't work well with rust. Hence I went ahead with this method for now.

Screenshot 2024-10-09 at 7 04 15 PM

Copy link
Member

@srid srid Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rsrohitsingh682 Do you understand why the logic is wrong? If I have both bash and zsh dotfiles, and bash dotfiles are managed by Nix but zsh dotfiles are not managed by Nix - then what would be the result of the check if zsh is my current shell?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh! Right!! Got it. I made the check to use OR condition thinking what if user is just using only one shell either of (zsh or bash). But yeah I should have considered the case that you mentioned.


// check if dotfile(s) for a given shell points to /nix/store
fn are_shell_dotfiles_nix_managed(shell: &str, dotfiles: &HashMap<&str, Vec<&str>>) -> bool {
let home = env::var("HOME").unwrap_or_default();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens in the default case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't panic instead will return empty string. I didn't want to throw error here because it wouldn't let the health process to be completed (break in between). Hence I thought if it returns empty string we would eventually show that shell configuration is not managed by nix ( However this is still not correct way).Would need your advice here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not reflexively use Default instance without understanding what it does..

Do you understand how a) an empty string for $HOME is meaningless, and b) how $HOME being empty affects the logic in your function? When (b) happens, is your function still correct?

Comment on lines 52 to 54
} else {
false
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function interface should be small enough to not have dummy code paths like this. I believe this will be addressed by introducing struct.

@srid srid changed the title nix_health: Check Shell configurations are Nix-Managed om health: Shell dotfiles are Nix-managed Oct 8, 2024
@rsrohitsingh682
Copy link
Member Author

@rsrohitsingh682 Could you post screenshots of the behaviour on macOS, Linux and on some system where neither bash nor zsh is the current shell?

MacOS where zsh is managed by nix.

Screenshot 2024-10-10 at 2 35 34 PM

MacOS where zsh is not managed by nix.

macOS

Linux (Ubuntu) where bash is not managed by nix.

Ubuntu

Copy link
Member

@srid srid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the struct encapsulation can still be improved (for one thing, you only need one struct not two). But the best way to understand why is to answer this question: How does this behave under the following situations?

  • User's shell is neither bash nor zsh. Its dotfiles are not managed by Nix.
  • User's shell is neither bash nor zsh. Its dotfiles are managed by Nix.

let shell_name = Path::new(path)
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you think empty string is better than None (in the context of the match below)? What do you think is the purpose of the Option type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a miss. I didn't realise I could write it like this as well.

fn from_path(path: &str) -> Self {
        Path::new(path)
            .file_name()
            .and_then(|name| name.to_str())
            .map(|shell_name| match shell_name {
                "zsh" => Shell::Zsh,
                "bash" => Shell::Bash,
                _ => Shell::Unknown,
            })
            .unwrap_or(Shell::Unknown)
    } 
 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still is not faithful to the environment. Are you really sure Shell::Unknown can never refer to when the user has bash or zsh as their current shell? In your code snippet above, there is in fact a condition under which your function will return Shell::Unknown despite the user having bash or zsh as their current shell.

Try to understand what each function does, including the nature of the range of values it can return.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall: we need a case to handle unexpected situations and errors, and then report that accordingly instead of giving a false negative.

@rsrohitsingh682
Copy link
Member Author

I think the struct encapsulation can still be improved (for one thing, you only need one struct not two). But the best way to understand why is to answer this question: How does this behave under the following situations?

  • User's shell is neither bash nor zsh. Its dotfiles are not managed by Nix.
  • User's shell is neither bash nor zsh. Its dotfiles are managed by Nix.

For both the cases it would return false and say it is not managed by Nix which would be wrong for the second part. Now when I think about it I am checking only dotfiles but for shells like fish the configurations are present in ~/.config/fish/config.fish. So ideally I should maintain a list where I can refer to the correct place be it .dotfiles or config files.

@rsrohitsingh682 rsrohitsingh682 marked this pull request as draft October 13, 2024 18:39
@rsrohitsingh682
Copy link
Member Author

@srid The CI seems to be failing for aarch64-darwin while running tests.

For checking the shell configuration it is looking into

/var/lib/github-runners/sambar-juspay-08/.bashrc

Any thoughts on this ?

Screenshot 2024-10-24 at 3 01 47 PM

@srid
Copy link
Member

srid commented Oct 24, 2024

That's because in CI $HOME is /var/lib/github-runners/sambar-juspay-08/.

How are you handling the case of a dotfile not even existing in the first place?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

om health: Check that user's shell is configured in Nix
2 participants