Skip to content

Commit

Permalink
Introduce nix-installe restore-shell to fix the init scripts, called …
Browse files Browse the repository at this point in the history
…by the nix-hook plist on every login
  • Loading branch information
grahamc committed Oct 11, 2023
1 parent dd40f0f commit b3af5d9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/action/macos/create_nix_hook_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ async fn generate_plist(service_label: &str) -> Result<LaunchctlHookPlist, Actio
let plist = LaunchctlHookPlist {
run_at_load: true,
label: service_label.into(),
program_arguments: vec!["/nix/nix-installer".into(), "restore-rc".into()],
program_arguments: vec!["/nix/nix-installer".into(), "restore-shell".into()],
standard_error_path: "/nix/.nix-installer-hook.err.log".into(),
standard_out_path: "/nix/.nix-installer-hook.out.log".into(),
};
Expand Down
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl CommandExecute for NixInstallerCli {
NixInstallerSubcommand::Plan(plan) => 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,
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/cli/subcommand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand Down
80 changes: 80 additions & 0 deletions src/cli/subcommand/restore_shell.rs
Original file line number Diff line number Diff line change
@@ -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<ExitCode> {
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(())
}

0 comments on commit b3af5d9

Please sign in to comment.