Skip to content

Commit

Permalink
Suggest Determinate Nix to users who did not pass the flag
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamc committed Feb 7, 2025
1 parent b474905 commit 5e15f90
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
63 changes: 61 additions & 2 deletions src/cli/subcommand/install.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
io::IsTerminal as _,
os::unix::prelude::PermissionsExt,
path::{Path, PathBuf},
process::ExitCode,
Expand Down Expand Up @@ -75,7 +76,7 @@ pub struct Install {
#[async_trait::async_trait]
impl CommandExecute for Install {
#[tracing::instrument(level = "trace", skip_all)]
async fn execute<T>(self, feedback: T) -> eyre::Result<ExitCode>
async fn execute<T>(self, mut feedback: T) -> eyre::Result<ExitCode>
where
T: crate::feedback::Feedback,
{
Expand Down Expand Up @@ -119,7 +120,7 @@ impl CommandExecute for Install {
.wrap_err("Reading plan")?;
serde_json::from_str(&install_plan_string)?
} else {
let planner = match maybe_planner {
let mut planner = match maybe_planner {
Some(planner) => planner,
None => BuiltinPlanner::from_common_settings(settings.clone())
.await
Expand Down Expand Up @@ -157,6 +158,64 @@ impl CommandExecute for Install {
return Ok(ExitCode::SUCCESS);
},
None => {
let planner_settings = planner.common_settings_mut();

if !planner_settings.determinate_nix && !no_confirm {
if !std::io::stdin().is_terminal() {
let msg = feedback
.get_feature_ptr_payload::<String>("dni-det-msg-noninteractive-ptr")
.await
.unwrap_or("Consider using Determinate Nix, for less fuss: http://dtr.mn/determinate-nix".into());
tracing::info!("{}", msg);
} else {
let base_prompt = feedback
.get_feature_ptr_payload::<String>(
"dni-det-msg-interactive-prompt-ptr",
)
.await
.unwrap_or(
"Install Determinate Nix? See: http://dtr.mn/determinate-nix"
.into(),
);
let explanation = feedback
.get_feature_ptr_payload::<String>(
"dni-det-msg-interactive-explanation-ptr",
)
.await
.unwrap_or("Determinate Nix is the easy button for Nix.".into());

let mut currently_explaining = explain;

loop {
let prompt = if currently_explaining {
&format!("{base_prompt}\n{explanation}")
} else {
&base_prompt
};

let response = interaction::prompt(
prompt.green().to_string(),
PromptChoice::Yes,
currently_explaining,
)
.await?;

match response {
PromptChoice::Explain => {
currently_explaining = true;
},
PromptChoice::Yes => {
planner_settings.determinate_nix = true;
break;
},
PromptChoice::No => {
break;
},
}
}
}
}

let res = planner.plan().await;
match res {
Ok(plan) => plan,
Expand Down
9 changes: 9 additions & 0 deletions src/planner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ impl BuiltinPlanner {
Ok(built)
}

pub fn common_settings_mut(&mut self) -> &mut CommonSettings {
match self {
BuiltinPlanner::Linux(inner) => &mut inner.settings,
BuiltinPlanner::SteamDeck(inner) => &mut inner.settings,
BuiltinPlanner::Ostree(inner) => &mut inner.settings,
BuiltinPlanner::Macos(inner) => &mut inner.settings,
}
}

pub async fn configured_settings(
&self,
) -> Result<HashMap<String, serde_json::Value>, PlannerError> {
Expand Down

0 comments on commit 5e15f90

Please sign in to comment.