From b3af5d991c0eae328d652e0a2e412025ade6c354 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Wed, 11 Oct 2023 12:40:31 -0400 Subject: [PATCH] Introduce nix-installe restore-shell to fix the init scripts, called by the nix-hook plist on every login --- src/action/macos/create_nix_hook_service.rs | 2 +- src/cli/mod.rs | 1 + src/cli/subcommand/mod.rs | 3 + src/cli/subcommand/restore_shell.rs | 80 +++++++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/cli/subcommand/restore_shell.rs diff --git a/src/action/macos/create_nix_hook_service.rs b/src/action/macos/create_nix_hook_service.rs index 2c59d78c8..8d01fc0b9 100644 --- a/src/action/macos/create_nix_hook_service.rs +++ b/src/action/macos/create_nix_hook_service.rs @@ -184,7 +184,7 @@ async fn generate_plist(service_label: &str) -> Result plan.execute().await, NixInstallerSubcommand::SelfTest(self_test) => self_test.execute().await, NixInstallerSubcommand::Install(install) => install.execute().await, + NixInstallerSubcommand::RestoreShell(restore_shell) => restore_shell.execute().await, NixInstallerSubcommand::Uninstall(revert) => revert.execute().await, } } diff --git a/src/cli/subcommand/mod.rs b/src/cli/subcommand/mod.rs index 41f4ef73f..46cdfc6dd 100644 --- a/src/cli/subcommand/mod.rs +++ b/src/cli/subcommand/mod.rs @@ -2,6 +2,8 @@ mod plan; use plan::Plan; mod install; use install::Install; +mod restore_shell; +use restore_shell::RestoreShell; mod uninstall; use uninstall::Uninstall; mod self_test; @@ -11,6 +13,7 @@ use self_test::SelfTest; #[derive(Debug, clap::Subcommand)] pub enum NixInstallerSubcommand { Install(Install), + RestoreShell(RestoreShell), Uninstall(Uninstall), SelfTest(SelfTest), Plan(Plan), diff --git a/src/cli/subcommand/restore_shell.rs b/src/cli/subcommand/restore_shell.rs new file mode 100644 index 000000000..fa9e4a63c --- /dev/null +++ b/src/cli/subcommand/restore_shell.rs @@ -0,0 +1,80 @@ +use std::{os::unix::prelude::PermissionsExt, process::ExitCode}; + +use crate::{ + action::common::ConfigureShellProfile, + cli::{ensure_root, CommandExecute}, + planner::{PlannerError, ShellProfileLocations}, +}; +use clap::{ArgAction, Parser}; + +/** +Update the macOS startup files to make Nix usable after system upgrades. +*/ +#[derive(Debug, Parser)] +#[command(args_conflicts_with_subcommands = true)] +pub struct RestoreShell { + #[clap( + long, + env = "NIX_INSTALLER_NO_CONFIRM", + action(ArgAction::SetTrue), + default_value = "false", + global = true + )] + pub no_confirm: bool, + + #[clap( + long, + env = "NIX_INSTALLER_EXPLAIN", + action(ArgAction::SetTrue), + default_value = "false", + global = true + )] + pub explain: bool, +} + +#[async_trait::async_trait] +impl CommandExecute for RestoreShell { + #[tracing::instrument(level = "trace", skip_all)] + async fn execute(self) -> eyre::Result { + let Self { + no_confirm: _, + explain: _, + } = self; + + ensure_root()?; + + let mut reconfigure = ConfigureShellProfile::plan(ShellProfileLocations::default()) + .await + .map_err(PlannerError::Action)? + .boxed(); + + if let Err(err) = reconfigure.try_execute().await { + println!("{:#?}", err); + + /* + let err = NixInstallerError::Action(err); + #[cfg(feature = "diagnostics")] + if let Some(diagnostic_data) = &self.diagnostic_data { + diagnostic_data + .clone() + .failure(&err) + .send( + crate::diagnostics::DiagnosticAction::RestoreShell, + crate::diagnostics::DiagnosticStatus::Failure, + ) + .await?; + }*/ + Ok(ExitCode::FAILURE) + } else { + Ok(ExitCode::SUCCESS) + } + } +} + +#[tracing::instrument(level = "debug")] +async fn copy_self_to_nix_dir() -> Result<(), std::io::Error> { + let path = std::env::current_exe()?; + tokio::fs::copy(path, "/nix/nix-installer").await?; + tokio::fs::set_permissions("/nix/nix-installer", PermissionsExt::from_mode(0o0755)).await?; + Ok(()) +}