Remove nix channel placement (#304)

* Remove place_channel_configuration.rs

* Remove channels option

* Add fixture updates
This commit is contained in:
Ana Hobden 2023-03-08 09:24:41 -08:00 committed by GitHub
parent 6219b2c48d
commit 5fe7dd9828
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 14 additions and 366 deletions

View file

@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
use crate::{
action::{ActionError, ActionTag, StatefulAction},
execute_command, set_env, ChannelValue,
execute_command, set_env,
};
use glob::glob;
@ -16,14 +16,12 @@ use crate::action::{Action, ActionDescription};
Setup the default Nix profile with `nss-cacert` and `nix` itself.
*/
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct SetupDefaultProfile {
channels: Vec<ChannelValue>,
}
pub struct SetupDefaultProfile {}
impl SetupDefaultProfile {
#[tracing::instrument(level = "debug", skip_all)]
pub async fn plan(channels: Vec<ChannelValue>) -> Result<StatefulAction<Self>, ActionError> {
Ok(Self { channels }.into())
pub async fn plan() -> Result<StatefulAction<Self>, ActionError> {
Ok(Self {}.into())
}
}
@ -38,16 +36,7 @@ impl Action for SetupDefaultProfile {
}
fn tracing_span(&self) -> Span {
span!(
tracing::Level::DEBUG,
"setup_default_profile",
channels = self
.channels
.iter()
.map(|ChannelValue(channel, url)| format!("{channel}={url}"))
.collect::<Vec<_>>()
.join(","),
)
span!(tracing::Level::DEBUG, "setup_default_profile",)
}
fn execute_description(&self) -> Vec<ActionDescription> {
@ -56,8 +45,6 @@ impl Action for SetupDefaultProfile {
#[tracing::instrument(level = "debug", skip_all)]
async fn execute(&mut self) -> Result<(), ActionError> {
let Self { channels } = self;
// Find an `nix` package
let nix_pkg_glob = "/nix/store/*-nix-*";
let mut found_nix_pkg = None;
@ -200,29 +187,13 @@ impl Action for SetupDefaultProfile {
"/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt",
);
if !channels.is_empty() {
let mut command = Command::new(nix_pkg.join("bin/nix-channel"));
command.process_group(0);
command.arg("--update");
for channel in channels {
command.arg(channel.0.clone());
}
command.env(
"NIX_SSL_CERT_FILE",
"/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt",
);
command.stdin(std::process::Stdio::null());
execute_command(&mut command).await?;
}
Ok(())
}
fn revert_description(&self) -> Vec<ActionDescription> {
vec![ActionDescription::new(
"Unset the default Nix profile".to_string(),
vec!["TODO".to_string()],
vec![],
)]
}

View file

@ -1,7 +1,7 @@
use crate::{
action::{
base::SetupDefaultProfile,
common::{ConfigureShellProfile, PlaceChannelConfiguration, PlaceNixConfiguration},
common::{ConfigureShellProfile, PlaceNixConfiguration},
Action, ActionDescription, ActionError, ActionTag, StatefulAction,
},
settings::CommonSettings,
@ -16,14 +16,13 @@ Configure Nix and start it
pub struct ConfigureNix {
setup_default_profile: StatefulAction<SetupDefaultProfile>,
configure_shell_profile: Option<StatefulAction<ConfigureShellProfile>>,
place_channel_configuration: StatefulAction<PlaceChannelConfiguration>,
place_nix_configuration: StatefulAction<PlaceNixConfiguration>,
}
impl ConfigureNix {
#[tracing::instrument(level = "debug", skip_all)]
pub async fn plan(settings: &CommonSettings) -> Result<StatefulAction<Self>, ActionError> {
let setup_default_profile = SetupDefaultProfile::plan(settings.channels.clone())
let setup_default_profile = SetupDefaultProfile::plan()
.await
.map_err(|e| ActionError::Child(SetupDefaultProfile::action_tag(), Box::new(e)))?;
@ -34,12 +33,6 @@ impl ConfigureNix {
} else {
None
};
let place_channel_configuration =
PlaceChannelConfiguration::plan(settings.channels.clone(), settings.force)
.await
.map_err(|e| {
ActionError::Child(PlaceChannelConfiguration::action_tag(), Box::new(e))
})?;
let place_nix_configuration = PlaceNixConfiguration::plan(
settings.nix_build_group_name.clone(),
settings.extra_conf.clone(),
@ -49,7 +42,6 @@ impl ConfigureNix {
.map_err(|e| ActionError::Child(PlaceNixConfiguration::action_tag(), Box::new(e)))?;
Ok(Self {
place_channel_configuration,
place_nix_configuration,
setup_default_profile,
configure_shell_profile,
@ -76,13 +68,11 @@ impl Action for ConfigureNix {
let Self {
setup_default_profile,
place_nix_configuration,
place_channel_configuration,
configure_shell_profile,
} = &self;
let mut buf = setup_default_profile.describe_execute();
buf.append(&mut place_nix_configuration.describe_execute());
buf.append(&mut place_channel_configuration.describe_execute());
if let Some(configure_shell_profile) = configure_shell_profile {
buf.append(&mut configure_shell_profile.describe_execute());
}
@ -94,18 +84,12 @@ impl Action for ConfigureNix {
let Self {
setup_default_profile,
place_nix_configuration,
place_channel_configuration,
configure_shell_profile,
} = self;
if let Some(configure_shell_profile) = configure_shell_profile {
let setup_default_profile_span = tracing::Span::current().clone();
let (
place_nix_configuration_span,
place_channel_configuration_span,
configure_shell_profile_span,
) = (
setup_default_profile_span.clone(),
let (place_nix_configuration_span, configure_shell_profile_span) = (
setup_default_profile_span.clone(),
setup_default_profile_span.clone(),
);
@ -128,18 +112,6 @@ impl Action for ConfigureNix {
ActionError::Child(place_nix_configuration.action_tag(), Box::new(e))
})
},
async move {
place_channel_configuration
.try_execute()
.instrument(place_channel_configuration_span)
.await
.map_err(|e| {
ActionError::Child(
place_channel_configuration.action_tag(),
Box::new(e),
)
})
},
async move {
configure_shell_profile
.try_execute()
@ -151,11 +123,8 @@ impl Action for ConfigureNix {
},
)?;
} else {
let place_channel_configuration_span = tracing::Span::current().clone();
let (setup_default_profile_span, place_nix_configuration_span) = (
place_channel_configuration_span.clone(),
place_channel_configuration_span.clone(),
);
let setup_default_profile_span = tracing::Span::current().clone();
let place_nix_configuration_span = setup_default_profile_span.clone();
tokio::try_join!(
async move {
setup_default_profile
@ -175,18 +144,6 @@ impl Action for ConfigureNix {
ActionError::Child(place_nix_configuration.action_tag(), Box::new(e))
})
},
async move {
place_channel_configuration
.try_execute()
.instrument(place_channel_configuration_span)
.await
.map_err(|e| {
ActionError::Child(
place_channel_configuration.action_tag(),
Box::new(e),
)
})
},
)?;
};
@ -197,7 +154,6 @@ impl Action for ConfigureNix {
let Self {
setup_default_profile,
place_nix_configuration,
place_channel_configuration,
configure_shell_profile,
} = &self;
@ -205,7 +161,6 @@ impl Action for ConfigureNix {
if let Some(configure_shell_profile) = configure_shell_profile {
buf.append(&mut configure_shell_profile.describe_revert());
}
buf.append(&mut place_channel_configuration.describe_revert());
buf.append(&mut place_nix_configuration.describe_revert());
buf.append(&mut setup_default_profile.describe_revert());
@ -219,12 +174,6 @@ impl Action for ConfigureNix {
ActionError::Child(configure_shell_profile.action_tag(), Box::new(e))
})?;
}
self.place_channel_configuration
.try_revert()
.await
.map_err(|e| {
ActionError::Child(self.place_channel_configuration.action_tag(), Box::new(e))
})?;
self.place_nix_configuration
.try_revert()
.await

View file

@ -5,7 +5,6 @@ pub(crate) mod configure_nix;
pub(crate) mod configure_shell_profile;
pub(crate) mod create_nix_tree;
pub(crate) mod create_users_and_groups;
pub(crate) mod place_channel_configuration;
pub(crate) mod place_nix_configuration;
pub(crate) mod provision_nix;
@ -14,6 +13,5 @@ pub use configure_nix::ConfigureNix;
pub use configure_shell_profile::ConfigureShellProfile;
pub use create_nix_tree::CreateNixTree;
pub use create_users_and_groups::CreateUsersAndGroups;
pub use place_channel_configuration::{PlaceChannelConfiguration, PlaceChannelConfigurationError};
pub use place_nix_configuration::PlaceNixConfiguration;
pub use provision_nix::ProvisionNix;

View file

@ -1,115 +0,0 @@
use crate::action::base::CreateFile;
use crate::action::{Action, ActionDescription, StatefulAction};
use crate::action::{ActionError, ActionTag};
use crate::ChannelValue;
use tracing::{span, Span};
/**
Place a channel configuration containing `channels` to the `$ROOT_HOME/.nix-channels` file
*/
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct PlaceChannelConfiguration {
channels: Vec<ChannelValue>,
create_file: StatefulAction<CreateFile>,
}
impl PlaceChannelConfiguration {
#[tracing::instrument(level = "debug", skip_all)]
pub async fn plan(
channels: Vec<ChannelValue>,
force: bool,
) -> Result<StatefulAction<Self>, ActionError> {
let buf = channels
.iter()
.map(|ChannelValue(name, url)| format!("{} {}", url, name))
.collect::<Vec<_>>()
.join("\n");
let create_file = CreateFile::plan(
dirs::home_dir()
.ok_or_else(|| {
ActionError::Custom(Box::new(PlaceChannelConfigurationError::NoRootHome))
})?
.join(".nix-channels"),
None,
None,
0o0644,
buf,
force,
)
.await
.map_err(|e| ActionError::Child(CreateFile::action_tag(), Box::new(e)))?;
Ok(Self {
create_file,
channels,
}
.into())
}
}
#[async_trait::async_trait]
#[typetag::serde(name = "place_channel_configuration")]
impl Action for PlaceChannelConfiguration {
fn action_tag() -> ActionTag {
ActionTag("place_channel_configuration")
}
fn tracing_synopsis(&self) -> String {
format!(
"Place channel configuration at `{}`",
self.create_file.inner().path.display()
)
}
fn tracing_span(&self) -> Span {
span!(
tracing::Level::DEBUG,
"place_channel_configuration",
channels = self
.channels
.iter()
.map(|ChannelValue(c, u)| format!("{c}={u}"))
.collect::<Vec<_>>()
.join(", "),
)
}
fn execute_description(&self) -> Vec<ActionDescription> {
vec![ActionDescription::new(self.tracing_synopsis(), vec![])]
}
#[tracing::instrument(level = "debug", skip_all)]
async fn execute(&mut self) -> Result<(), ActionError> {
self.create_file
.try_execute()
.await
.map_err(|e| ActionError::Child(self.create_file.action_tag(), Box::new(e)))?;
Ok(())
}
fn revert_description(&self) -> Vec<ActionDescription> {
vec![ActionDescription::new(
format!(
"Remove channel configuration at `{}`",
self.create_file.inner().path.display()
),
vec![],
)]
}
#[tracing::instrument(level = "debug", skip_all)]
async fn revert(&mut self) -> Result<(), ActionError> {
self.create_file
.try_revert()
.await
.map_err(|e| ActionError::Child(self.create_file.action_tag(), Box::new(e)))?;
Ok(())
}
}
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum PlaceChannelConfigurationError {
#[error("No root home found to place channel configuration in")]
NoRootHome,
}

View file

@ -1,50 +0,0 @@
use reqwest::Url;
use serde::{Deserialize, Serialize};
/**
A pair of [`String`] and [`Url`] destined for the list of subscribed channels for [`nix-channel`](https://nixos.org/manual/nix/stable/command-ref/nix-channel.html)
*/
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChannelValue(pub String, pub Url);
#[cfg(feature = "cli")]
impl clap::builder::ValueParserFactory for ChannelValue {
type Parser = ChannelValueParser;
fn value_parser() -> Self::Parser {
ChannelValueParser
}
}
impl From<(String, Url)> for ChannelValue {
fn from((string, url): (String, Url)) -> Self {
Self(string, url)
}
}
#[derive(Clone, Debug)]
pub struct ChannelValueParser;
#[cfg(feature = "cli")]
impl clap::builder::TypedValueParser for ChannelValueParser {
type Value = ChannelValue;
fn parse_ref(
&self,
_cmd: &clap::Command,
_arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
let buf = value.to_str().ok_or_else(|| {
clap::Error::raw(clap::error::ErrorKind::InvalidValue, "Should be all UTF-8")
})?;
let (name, url) = buf.split_once('=').ok_or_else(|| {
clap::Error::raw(
clap::error::ErrorKind::InvalidValue,
"`--channel` should be formatted `name=url`, eg `--channel nixpkgs=https://nixos.org/channels/nixpkgs-unstable`. To not use a channel, pass `--channel`",
)
})?;
let name = name.to_owned();
let url = url.parse().unwrap();
Ok(ChannelValue(name, url))
}
}

View file

@ -70,7 +70,6 @@ match plan.install(None).await {
*/
pub mod action;
mod channel_value;
#[cfg(feature = "cli")]
pub mod cli;
#[cfg(feature = "diagnostics")]
@ -85,7 +84,6 @@ use std::{ffi::OsStr, process::Output};
use action::{Action, ActionError};
pub use channel_value::ChannelValue;
pub use error::NixInstallerError;
pub use plan::InstallPlan;
use planner::BuiltinPlanner;

View file

@ -6,8 +6,6 @@ use std::collections::HashMap;
use clap::ArgAction;
use url::Url;
use crate::channel_value::ChannelValue;
/// Default [`nix_package_url`](CommonSettings::nix_package_url) for Linux x86_64
pub const NIX_X64_64_LINUX_URL: &str =
"https://releases.nixos.org/nix/nix-2.13.3/nix-2.13.3-x86_64-linux.tar.xz";
@ -57,20 +55,6 @@ Settings which only apply to certain [`Planner`](crate::planner::Planner)s shoul
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
pub struct CommonSettings {
/// Channel(s) to add, for no default channel, pass `--channel`
#[cfg_attr(
feature = "cli",
clap(
value_parser,
long = "channel",
num_args = 0..,
action = clap::ArgAction::Append,
env = "NIX_INSTALLER_CHANNELS",
default_value = "nixpkgs=https://nixos.org/channels/nixpkgs-unstable",
)
)]
pub(crate) channels: Vec<ChannelValue>,
/// Modify the user profile to automatically load nix
#[cfg_attr(
feature = "cli",
@ -281,11 +265,6 @@ impl CommonSettings {
Ok(Self {
nix_build_user_count: 32,
channels: vec![ChannelValue(
"nixpkgs".into(),
reqwest::Url::parse("https://nixos.org/channels/nixpkgs-unstable")
.expect("Embedded default URL was not a URL, please report this"),
)],
modify_profile: true,
nix_build_group_name: String::from("nixbld"),
nix_build_group_id: 30_000,
@ -304,7 +283,6 @@ impl CommonSettings {
/// A listing of the settings, suitable for [`Planner::settings`](crate::planner::Planner::settings)
pub fn settings(&self) -> Result<HashMap<String, serde_json::Value>, InstallSettingsError> {
let Self {
channels,
modify_profile,
nix_build_user_count,
nix_build_group_name,
@ -319,15 +297,6 @@ impl CommonSettings {
} = self;
let mut map = HashMap::default();
map.insert(
"channels".into(),
serde_json::to_value(
channels
.iter()
.map(|ChannelValue(k, v)| format!("{k}={v}"))
.collect::<Vec<_>>(),
)?,
);
map.insert(
"modify_profile".into(),
serde_json::to_value(modify_profile)?,
@ -405,12 +374,6 @@ impl CommonSettings {
self
}
/// Channel(s) to add
pub fn channels(&mut self, channels: impl IntoIterator<Item = (String, Url)>) -> &mut Self {
self.channels = channels.into_iter().map(Into::into).collect();
self
}
/// Modify the user profile to automatically load nix
pub fn modify_profile(&mut self, toggle: bool) -> &mut Self {
self.modify_profile = toggle;

View file

@ -831,28 +831,6 @@
},
"state": "Uncompleted"
},
"place_channel_configuration": {
"action": {
"channels": [
[
"nixpkgs",
"https://nixos.org/channels/nixpkgs-unstable"
]
],
"create_file": {
"action": {
"path": "/root/.nix-channels",
"user": null,
"group": null,
"mode": 436,
"buf": "https://nixos.org/channels/nixpkgs-unstable nixpkgs",
"force": false
},
"state": "Uncompleted"
}
},
"state": "Uncompleted"
},
"place_nix_configuration": {
"action": {
"create_directory": {
@ -918,4 +896,4 @@
"start_daemon": true
}
}
}
}

View file

@ -875,28 +875,6 @@
},
"state": "Uncompleted"
},
"place_channel_configuration": {
"action": {
"channels": [
[
"nixpkgs",
"https://nixos.org/channels/nixpkgs-unstable"
]
],
"create_file": {
"action": {
"path": "/home/ubuntu/.nix-channels",
"user": null,
"group": null,
"mode": 436,
"buf": "https://nixos.org/channels/nixpkgs-unstable nixpkgs",
"force": false
},
"state": "Uncompleted"
}
},
"state": "Uncompleted"
},
"place_nix_configuration": {
"action": {
"create_directory": {
@ -967,4 +945,4 @@
"force": false
}
}
}
}

View file

@ -885,28 +885,6 @@
},
"state": "Uncompleted"
},
"place_channel_configuration": {
"action": {
"channels": [
[
"nixpkgs",
"https://nixos.org/channels/nixpkgs-unstable"
]
],
"create_file": {
"action": {
"path": "/Users/ephemeraladmin/.nix-channels",
"user": null,
"group": null,
"mode": 436,
"buf": "https://nixos.org/channels/nixpkgs-unstable nixpkgs",
"force": false
},
"state": "Uncompleted"
}
},
"state": "Uncompleted"
},
"place_nix_configuration": {
"action": {
"create_directory": {
@ -972,4 +950,4 @@
"volume_label": "Nix Store",
"root_disk": "disk1"
}
}
}