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

Skip nix upgrade-nix when Nix is installed in a nix profile #622

Merged
merged 1 commit into from
Dec 21, 2023

Conversation

9999years
Copy link
Contributor

nix upgrade-nix errors if nix is installed in a nix profile:

error: directory '/Users/wiggles/.nix-profile/bin' does not appear to be part of a Nix profile

Upstream issue: NixOS/nix#5473

This patch duplicates the relevant Nix logic and skips running nix upgrade-nix if this error would occur: https://github.com/NixOS/nix/blob/f0180487a0e4c0091b46cb1469c44144f5400240/src/nix/upgrade-nix.cc#L102-L139

I've also split the nix upgrade-nix call into a separate step so we can use the SkipStep error messages appropriately.

Standards checklist:

  • The PR title is descriptive.
  • I have read CONTRIBUTING.md
  • The code compiles (cargo build)
  • The code passes rustfmt (cargo fmt)
  • The code passes clippy (cargo clippy)
  • The code passes tests (cargo test)
  • Optional: I have tested the code myself

For new steps

  • Optional: Topgrade skips this step where needed
  • Optional: The --dry-run option works with this step
  • Optional: The --yes option works with this step if it is supported by
    the underlying command

If you developed a feature or a bug fix for someone else and you do not have the
means to test it, please tag this person here.

Also check that Nix can be upgraded before running `nix upgrade-nix` to
work around a bug.

See: <NixOS/nix#5473>
Comment on lines +409 to +419
// Should we attempt to upgrade Nix with `nix upgrade-nix`?
#[allow(unused_mut)]
let mut should_self_upgrade = cfg!(target_os = "macos");

#[cfg(target_os = "linux")]
{
// We can't use `nix upgrade-nix` on NixOS.
if let Ok(Distribution::NixOS) = Distribution::detect() {
should_self_upgrade = false;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Should we attempt to upgrade Nix with `nix upgrade-nix`?
#[allow(unused_mut)]
let mut should_self_upgrade = cfg!(target_os = "macos");
#[cfg(target_os = "linux")]
{
// We can't use `nix upgrade-nix` on NixOS.
if let Ok(Distribution::NixOS) = Distribution::detect() {
should_self_upgrade = false;
}
}
// Should we attempt to upgrade Nix with `nix upgrade-nix`?
let mut should_self_upgrade = if cfg!(target_os = "macos") {
true
} else if cfg!(target_os = "linux") {
!matches!(Distribution::detect(), Distribution::NixOS)
} else {
unreachable!()
};

Maybe we can do this here, it has better readability, though the generated code looks bad:

// On Linux
    let should = if false {
        true
    } else if true {
        match Distribution::detect() {
            Distribution::NixOS => true,
            _ => false,
        }
    } else {
        ::core::panicking::panic("internal error: entered unreachable code")
    };

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I am sorry, I didn't realize that this Distribution type is only defined on Linux, and since cfg!(target_os = "macos") will be simply translated to a bool value, it requires that this type is also available on macOS, so it won't compile:(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, I'll revert.

Copy link
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

Thanks

@SteveLauC SteveLauC merged commit cbfb920 into topgrade-rs:main Dec 21, 2023
15 checks passed
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.

2 participants