Don't modify shell profile files if they are symlinks (#767)

* Don't modify shell profile files if they are symlinks

* Fixup remote building step
This commit is contained in:
Ana Hobden 2023-12-05 11:14:46 -08:00 committed by GitHub
parent 0419422de0
commit b84ebf0841
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 50 deletions

View file

@ -44,6 +44,8 @@ impl ConfigureShellProfile {
for profile_target in locations.bash.iter().chain(locations.zsh.iter()) {
let profile_target_path = Path::new(profile_target);
if let Some(parent) = profile_target_path.parent() {
// Some tools (eg `nix-darwin`) create symlinks to these files, don't write to them if that's the case.
if !profile_target_path.is_symlink() {
if !parent.exists() {
create_directories.push(
CreateDirectory::plan(parent, None, None, 0o0755, false)
@ -51,6 +53,7 @@ impl ConfigureShellProfile {
.map_err(Self::error)?,
);
}
create_or_insert_files.push(
CreateOrInsertIntoFile::plan(
profile_target_path,
@ -65,6 +68,7 @@ impl ConfigureShellProfile {
);
}
}
}
let fish_buf = format!(
"\n\
@ -88,9 +92,12 @@ impl ConfigureShellProfile {
let mut profile_target = fish_prefix_path;
profile_target.push(locations.fish.confd_suffix.clone());
// Some tools (eg `nix-darwin`) create symlinks to these files, don't write to them if that's the case.
if !profile_target.is_symlink() {
if let Some(conf_d) = profile_target.parent() {
create_directories.push(
CreateDirectory::plan(conf_d.to_path_buf(), None, None, 0o755, false).await?,
CreateDirectory::plan(conf_d.to_path_buf(), None, None, 0o755, false)
.await?,
);
}
@ -106,6 +113,7 @@ impl ConfigureShellProfile {
.await?,
);
}
}
for fish_prefix in &locations.fish.vendor_confd_prefixes {
let fish_prefix_path = PathBuf::from(fish_prefix);

View file

@ -12,7 +12,7 @@ This enables remote building, which requires `ssh host nix` to work.
*/
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct ConfigureRemoteBuilding {
create_or_insert_into_file: StatefulAction<CreateOrInsertIntoFile>,
create_or_insert_into_file: Option<StatefulAction<CreateOrInsertIntoFile>>,
}
impl ConfigureRemoteBuilding {
@ -29,8 +29,12 @@ fi
"#
);
let create_or_insert_into_file = CreateOrInsertIntoFile::plan(
Path::new("/etc/zshenv"),
let zshenv = Path::new("/etc/zshenv");
let create_or_insert_into_file = if !zshenv.is_symlink() {
Some(
CreateOrInsertIntoFile::plan(
zshenv,
None,
None,
0o644,
@ -38,7 +42,11 @@ fi
create_or_insert_into_file::Position::Beginning,
)
.await
.map_err(Self::error)?;
.map_err(Self::error)?,
)
} else {
None
};
Ok(Self {
create_or_insert_into_file,
@ -63,7 +71,11 @@ impl Action for ConfigureRemoteBuilding {
fn execute_description(&self) -> Vec<ActionDescription> {
vec![ActionDescription::new(
self.tracing_synopsis(),
if self.create_or_insert_into_file.is_none() {
"Skipping configuring zsh to support using Nix in non-interactive shells, `/etc/zshenv` is a symlink".to_string()
} else {
self.tracing_synopsis()
},
vec!["Update `/etc/zshenv` to import Nix".to_string()],
)]
}
@ -71,11 +83,13 @@ impl Action for ConfigureRemoteBuilding {
#[tracing::instrument(level = "debug", skip_all)]
async fn execute(&mut self) -> Result<(), ActionError> {
let span = tracing::Span::current().clone();
self.create_or_insert_into_file
if let Some(create_or_insert_into_file) = &mut self.create_or_insert_into_file {
create_or_insert_into_file
.try_execute()
.instrument(span)
.await
.map_err(Self::error)?;
}
Ok(())
}
@ -89,7 +103,9 @@ impl Action for ConfigureRemoteBuilding {
#[tracing::instrument(level = "debug", skip_all)]
async fn revert(&mut self) -> Result<(), ActionError> {
self.create_or_insert_into_file.try_revert().await?;
if let Some(create_or_insert_into_file) = &mut self.create_or_insert_into_file {
create_or_insert_into_file.try_revert().await?
};
Ok(())
}