lix-installer/src/settings.rs

138 lines
4.6 KiB
Rust
Raw Normal View History

use crate::planner;
2022-10-13 19:03:42 +00:00
use target_lexicon::Triple;
2022-09-14 22:18:13 +00:00
use url::Url;
2022-10-13 19:03:42 +00:00
#[serde_with::serde_as]
2022-09-15 19:11:46 +00:00
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
2022-09-14 22:18:13 +00:00
pub struct InstallSettings {
2022-09-15 17:29:22 +00:00
pub(crate) daemon_user_count: usize,
pub(crate) channels: Vec<(String, Url)>,
pub(crate) modify_profile: bool,
pub(crate) nix_build_group_name: String,
pub(crate) nix_build_group_id: usize,
pub(crate) nix_build_user_prefix: String,
pub(crate) nix_build_user_id_base: usize,
pub(crate) nix_package_url: Url,
pub(crate) extra_conf: Option<String>,
pub(crate) force: bool,
2022-10-13 19:03:42 +00:00
#[serde_as(as = "serde_with::DisplayFromStr")]
pub(crate) triple: Triple,
2022-09-14 22:18:13 +00:00
}
2022-10-13 18:26:43 +00:00
impl InstallSettings {
pub fn default() -> Result<Self, InstallSettingsError> {
let url;
2022-10-24 23:16:18 +00:00
let nix_build_user_prefix;
let nix_build_user_id_base;
2022-10-13 18:26:43 +00:00
use target_lexicon::{Architecture, OperatingSystem};
match (Architecture::host(), OperatingSystem::host()) {
(Architecture::X86_64, OperatingSystem::Linux) => {
url = "https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-x86_64-linux.tar.xz";
2022-10-24 23:16:18 +00:00
nix_build_user_prefix = "nixbld";
nix_build_user_id_base = 3000;
2022-10-13 18:26:43 +00:00
},
(Architecture::Aarch64(_), OperatingSystem::Linux) => {
url = "https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-aarch64-linux.tar.xz";
2022-10-24 23:16:18 +00:00
nix_build_user_prefix = "nixbld";
nix_build_user_id_base = 3000;
2022-10-13 18:26:43 +00:00
},
2022-10-19 22:12:50 +00:00
(Architecture::X86_64, OperatingSystem::MacOSX { .. })
| (Architecture::X86_64, OperatingSystem::Darwin) => {
2022-10-13 18:26:43 +00:00
url = "https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-x86_64-darwin.tar.xz";
2022-10-24 23:16:18 +00:00
nix_build_user_prefix = "_nixbld";
nix_build_user_id_base = 300;
2022-10-13 18:26:43 +00:00
},
2022-10-19 22:12:50 +00:00
(Architecture::Aarch64(_), OperatingSystem::MacOSX { .. })
| (Architecture::Aarch64(_), OperatingSystem::Darwin) => {
2022-10-13 18:26:43 +00:00
url = "https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-aarch64-darwin.tar.xz";
2022-10-24 23:16:18 +00:00
nix_build_user_prefix = "_nixbld";
nix_build_user_id_base = 300;
2022-10-13 18:26:43 +00:00
},
_ => {
return Err(InstallSettingsError::UnsupportedArchitecture(
target_lexicon::HOST,
))
},
};
Ok(Self {
2022-10-13 19:03:42 +00:00
triple: target_lexicon::HOST,
2022-09-15 19:11:46 +00:00
daemon_user_count: Default::default(),
channels: Default::default(),
modify_profile: Default::default(),
nix_build_group_name: String::from("nixbld"),
nix_build_group_id: 3000,
2022-10-24 23:16:18 +00:00
nix_build_user_prefix: nix_build_user_prefix.to_string(),
nix_build_user_id_base,
2022-10-13 18:26:43 +00:00
nix_package_url: url
.parse()
.expect("Could not parse default Nix archive url, please report this issue"),
extra_conf: Default::default(),
force: false,
2022-10-13 18:26:43 +00:00
})
2022-09-15 19:11:46 +00:00
}
}
2022-09-14 22:18:13 +00:00
// Builder Pattern
impl InstallSettings {
pub fn daemon_user_count(&mut self, count: usize) -> &mut Self {
self.daemon_user_count = count;
self
}
pub fn channels(&mut self, channels: impl IntoIterator<Item = (String, Url)>) -> &mut Self {
self.channels = channels.into_iter().collect();
self
}
pub fn modify_profile(&mut self, toggle: bool) -> &mut Self {
self.modify_profile = toggle;
self
}
pub fn nix_build_group_name(&mut self, val: String) -> &mut Self {
self.nix_build_group_name = val;
self
}
pub fn nix_build_group_id(&mut self, count: usize) -> &mut Self {
self.nix_build_group_id = count;
self
}
pub fn nix_build_user_prefix(&mut self, val: String) -> &mut Self {
self.nix_build_user_prefix = val;
self
}
pub fn nix_build_user_id_base(&mut self, count: usize) -> &mut Self {
self.nix_build_user_id_base = count;
self
}
pub fn nix_package_url(&mut self, url: Url) -> &mut Self {
self.nix_package_url = url;
self
}
pub fn extra_conf(&mut self, extra_conf: Option<String>) -> &mut Self {
self.extra_conf = extra_conf;
self
}
pub fn force(&mut self, force: bool) -> &mut Self {
self.force = force;
self
}
2022-09-15 19:11:46 +00:00
}
2022-10-13 18:26:43 +00:00
#[derive(thiserror::Error, Debug)]
pub enum InstallSettingsError {
#[error("Harmonic does not support the `{0}` architecture right now")]
UnsupportedArchitecture(target_lexicon::Triple),
2022-10-14 22:14:03 +00:00
#[error("Planner error")]
Planner(
#[source]
#[from]
planner::PlannerError,
),
2022-10-13 18:26:43 +00:00
}