diff --git a/src/action/common/configure_shell_profile.rs b/src/action/common/configure_shell_profile.rs index d6fc900..70c7300 100644 --- a/src/action/common/configure_shell_profile.rs +++ b/src/action/common/configure_shell_profile.rs @@ -44,25 +44,29 @@ 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() { - if !parent.exists() { - create_directories.push( - CreateDirectory::plan(parent, None, None, 0o0755, false) - .await - .map_err(Self::error)?, + // 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) + .await + .map_err(Self::error)?, + ); + } + + create_or_insert_files.push( + CreateOrInsertIntoFile::plan( + profile_target_path, + None, + None, + 0o644, + shell_buf.to_string(), + create_or_insert_into_file::Position::Beginning, + ) + .await + .map_err(Self::error)?, ); } - create_or_insert_files.push( - CreateOrInsertIntoFile::plan( - profile_target_path, - None, - None, - 0o644, - shell_buf.to_string(), - create_or_insert_into_file::Position::Beginning, - ) - .await - .map_err(Self::error)?, - ); } } @@ -88,23 +92,27 @@ impl ConfigureShellProfile { let mut profile_target = fish_prefix_path; profile_target.push(locations.fish.confd_suffix.clone()); - if let Some(conf_d) = profile_target.parent() { - create_directories.push( - CreateDirectory::plan(conf_d.to_path_buf(), None, None, 0o755, false).await?, + // 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?, + ); + } + + create_or_insert_files.push( + CreateOrInsertIntoFile::plan( + profile_target, + None, + None, + 0o644, + fish_buf.to_string(), + create_or_insert_into_file::Position::Beginning, + ) + .await?, ); } - - create_or_insert_files.push( - CreateOrInsertIntoFile::plan( - profile_target, - None, - None, - 0o644, - fish_buf.to_string(), - create_or_insert_into_file::Position::Beginning, - ) - .await?, - ); } for fish_prefix in &locations.fish.vendor_confd_prefixes { let fish_prefix_path = PathBuf::from(fish_prefix); diff --git a/src/action/macos/configure_remote_building.rs b/src/action/macos/configure_remote_building.rs index 3d9e036..81cadc4 100644 --- a/src/action/macos/configure_remote_building.rs +++ b/src/action/macos/configure_remote_building.rs @@ -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, + create_or_insert_into_file: Option>, } impl ConfigureRemoteBuilding { @@ -29,16 +29,24 @@ fi "# ); - let create_or_insert_into_file = CreateOrInsertIntoFile::plan( - Path::new("/etc/zshenv"), - None, - None, - 0o644, - shell_buf.to_string(), - create_or_insert_into_file::Position::Beginning, - ) - .await - .map_err(Self::error)?; + let zshenv = Path::new("/etc/zshenv"); + + let create_or_insert_into_file = if !zshenv.is_symlink() { + Some( + CreateOrInsertIntoFile::plan( + zshenv, + None, + None, + 0o644, + shell_buf.to_string(), + create_or_insert_into_file::Position::Beginning, + ) + .await + .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 { 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 - .try_execute() - .instrument(span) - .await - .map_err(Self::error)?; + 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(()) }