lix-installer/src/settings.rs

211 lines
6.7 KiB
Rust
Raw Normal View History

2022-10-25 18:57:09 +00:00
use clap::ArgAction;
2022-09-14 22:18:13 +00:00
use url::Url;
2022-10-25 18:57:09 +00:00
pub const NIX_X64_64_LINUX_URL: &str =
"https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-x86_64-linux.tar.xz";
pub const NIX_AARCH64_LINUX_URL: &str =
"https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-aarch64-linux.tar.xz";
pub const NIX_X64_64_DARWIN_URL: &str =
"https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-x86_64-darwin.tar.xz";
pub const NIX_AARCH64_DARWIN_URL: &str =
"https://releases.nixos.org/nix/nix-2.11.0/nix-2.11.0-aarch64-darwin.tar.xz";
2022-10-13 19:03:42 +00:00
#[serde_with::serde_as]
2022-10-25 18:57:09 +00:00
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, clap::Parser)]
pub struct CommonSettings {
2022-10-25 18:57:09 +00:00
/// Channel(s) to add by default, pass multiple times for multiple channels
#[clap(
long,
value_parser,
name = "channel",
action = clap::ArgAction::Append,
env = "HARMONIC_CHANNEL",
default_value = "nixpkgs=https://nixos.org/channels/nixpkgs-unstable",
)]
pub(crate) channels: Vec<crate::cli::arg::ChannelValue>,
2022-10-25 18:57:09 +00:00
/// Modify the user profile to automatically load nix
#[clap(
long,
action(ArgAction::SetFalse),
default_value = "true",
global = true,
env = "HARMONIC_NO_MODIFY_PROFILE",
name = "no-modify-profile"
)]
2022-09-15 17:29:22 +00:00
pub(crate) modify_profile: bool,
2022-10-25 18:57:09 +00:00
/// Number of build users to create
#[clap(long, default_value = "32", env = "HARMONIC_DAEMON_USER_COUNT")]
pub(crate) daemon_user_count: usize,
2022-10-25 18:57:09 +00:00
#[clap(long, default_value = "nixbld", env = "HARMONIC_NIX_BUILD_GROUP_NAME")]
2022-09-15 17:29:22 +00:00
pub(crate) nix_build_group_name: String,
2022-10-25 18:57:09 +00:00
#[clap(long, default_value_t = 3000, env = "HARMONIC_NIX_BUILD_GROUP_ID")]
2022-09-15 17:29:22 +00:00
pub(crate) nix_build_group_id: usize,
2022-10-25 18:57:09 +00:00
#[clap(long, env = "HARMONIC_NIX_BUILD_USER_PREFIX")]
#[cfg_attr(target_os = "macos", clap(default_value = "_nixbld"))]
#[cfg_attr(target_os = "linux", clap(default_value = "nixbld"))]
2022-09-15 17:29:22 +00:00
pub(crate) nix_build_user_prefix: String,
2022-10-25 18:57:09 +00:00
#[clap(long, env = "HARMONIC_NIX_BUILD_USER_ID_BASE")]
#[cfg_attr(target_os = "macos", clap(default_value_t = 300))]
#[cfg_attr(target_os = "linux", clap(default_value_t = 3000))]
2022-09-15 17:29:22 +00:00
pub(crate) nix_build_user_id_base: usize,
2022-10-25 18:57:09 +00:00
#[clap(long, env = "HARMONIC_NIX_PACKAGE_URL")]
#[cfg_attr(
all(target_os = "macos", target_arch = "x86_64"),
clap(
default_value = NIX_X64_64_DARWIN_URL,
)
)]
#[cfg_attr(
all(target_os = "macos", target_arch = "aarch64"),
clap(
default_value = NIX_AARCH64_DARWIN_URL,
)
)]
#[cfg_attr(
all(target_os = "linux", target_arch = "x86_64"),
clap(
default_value = NIX_X64_64_LINUX_URL,
)
)]
#[cfg_attr(
all(target_os = "linux", target_arch = "aarch64"),
clap(
default_value = NIX_AARCH64_LINUX_URL,
)
)]
pub(crate) nix_package_url: Url,
2022-10-25 18:57:09 +00:00
#[clap(long, env = "HARMONIC_EXTRA_CONF")]
pub(crate) extra_conf: Option<String>,
2022-10-25 18:57:09 +00:00
#[clap(
long,
action(ArgAction::SetTrue),
default_value = "false",
global = true,
env = "HARMONIC_FORCE"
)]
pub(crate) force: bool,
2022-09-14 22:18:13 +00:00
}
impl CommonSettings {
2022-10-13 18:26:43 +00:00
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) => {
2022-10-25 18:57:09 +00:00
url = NIX_X64_64_LINUX_URL;
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) => {
2022-10-25 18:57:09 +00:00
url = NIX_AARCH64_LINUX_URL;
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-25 18:57:09 +00:00
url = NIX_X64_64_DARWIN_URL;
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-25 18:57:09 +00:00
url = NIX_AARCH64_DARWIN_URL;
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-09-15 19:11:46 +00:00
daemon_user_count: Default::default(),
2022-10-25 18:57:09 +00:00
channels: Vec::default(),
modify_profile: true,
2022-09-15 19:11:46 +00:00
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-25 18:57:09 +00:00
nix_package_url: url.parse()?,
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 CommonSettings {
2022-09-14 22:18:13 +00:00
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 {
2022-10-25 18:57:09 +00:00
self.channels = channels.into_iter().map(Into::into).collect();
2022-09-14 22:18:13 +00:00
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-25 18:57:09 +00:00
#[error("Parsing URL")]
Parse(
2022-10-14 22:14:03 +00:00
#[source]
#[from]
2022-10-25 18:57:09 +00:00
url::ParseError,
2022-10-14 22:14:03 +00:00
),
2022-10-13 18:26:43 +00:00
}