Merge pull request #50 from DeterminateSystems/hoverbear/ds-441-detect-nixos

Planner error if on NixOS
This commit is contained in:
Ana Hobden 2022-11-14 08:53:50 -08:00 committed by GitHub
commit 49b22b451b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 7 deletions

View file

@ -3,7 +3,7 @@ use std::path::PathBuf;
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum HarmonicError { pub enum HarmonicError {
#[error("Error executing action")] #[error("Error executing action")]
ActionError( Action(
#[source] #[source]
#[from] #[from]
Box<dyn std::error::Error + Send + Sync>, Box<dyn std::error::Error + Send + Sync>,

View file

@ -90,7 +90,7 @@ impl InstallPlan {
if let Err(err) = write_receipt(self.clone()).await { if let Err(err) = write_receipt(self.clone()).await {
tracing::error!("Error saving receipt: {:?}", err); tracing::error!("Error saving receipt: {:?}", err);
} }
return Err(HarmonicError::ActionError(err)); return Err(HarmonicError::Action(err));
} }
} }
@ -156,7 +156,7 @@ impl InstallPlan {
if let Err(err) = write_receipt(self.clone()).await { if let Err(err) = write_receipt(self.clone()).await {
tracing::error!("Error saving receipt: {:?}", err); tracing::error!("Error saving receipt: {:?}", err);
} }
return Err(HarmonicError::ActionError(err)); return Err(HarmonicError::Action(err));
} }
} }

View file

@ -6,7 +6,7 @@ use crate::{
planner::Planner, planner::Planner,
BuiltinPlanner, CommonSettings, InstallPlan, BuiltinPlanner, CommonSettings, InstallPlan,
}; };
use std::collections::HashMap; use std::{collections::HashMap, path::Path};
#[derive(Debug, Clone, clap::Parser, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, clap::Parser, serde::Serialize, serde::Deserialize)]
pub struct LinuxMulti { pub struct LinuxMulti {
@ -24,12 +24,30 @@ impl Planner for LinuxMulti {
} }
async fn plan(self) -> Result<InstallPlan, Box<dyn std::error::Error + Sync + Send>> { async fn plan(self) -> Result<InstallPlan, Box<dyn std::error::Error + Sync + Send>> {
// If on NixOS, running `harmonic` is pointless
// NixOS always sets up this file as part of setting up /etc itself: https://github.com/NixOS/nixpkgs/blob/bdd39e5757d858bd6ea58ed65b4a2e52c8ed11ca/nixos/modules/system/etc/setup-etc.pl#L145
if Path::new("/etc/NIXOS").exists() {
return Err(Error::NixOs.into());
}
Ok(InstallPlan { Ok(InstallPlan {
planner: Box::new(self.clone()), planner: Box::new(self.clone()),
actions: vec![ actions: vec![
Box::new(CreateDirectory::plan("/nix", None, None, 0o0755, true).await?), Box::new(
Box::new(ProvisionNix::plan(self.settings.clone()).await?), CreateDirectory::plan("/nix", None, None, 0o0755, true)
Box::new(ConfigureNix::plan(self.settings).await?), .await
.map_err(|v| Error::Action(v.into()))?,
),
Box::new(
ProvisionNix::plan(self.settings.clone())
.await
.map_err(|v| Error::Action(v.into()))?,
),
Box::new(
ConfigureNix::plan(self.settings)
.await
.map_err(|v| Error::Action(v.into()))?,
),
], ],
}) })
} }
@ -51,3 +69,15 @@ impl Into<BuiltinPlanner> for LinuxMulti {
BuiltinPlanner::LinuxMulti(self) BuiltinPlanner::LinuxMulti(self)
} }
} }
#[derive(thiserror::Error, Debug)]
enum Error {
#[error("NixOS already has Nix installed")]
NixOs,
#[error("Error planning action")]
Action(
#[source]
#[from]
Box<dyn std::error::Error + Send + Sync>,
),
}