Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mostly replace profile scripts with Rust #738

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 52 additions & 18 deletions src/action/common/configure_shell_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::path::{Path, PathBuf};
use tokio::task::JoinSet;
use tracing::{span, Instrument, Span};

const PROFILE_NIX_FILE_SHELL: &str = "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh";
const PROFILE_NIX_FILE_FISH: &str = "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.fish";
pub(crate) const PROFILE_NIX_FILE_SHELL: &str = "/nix/nix-installer.d/profile.sh";
pub(crate) const PROFILE_NIX_FILE_FISH: &str = "/nix/nix-installer.d/profile.fish";

/**
Configure any detected shell profiles to include Nix support
Expand All @@ -31,14 +31,47 @@ impl ConfigureShellProfile {
let mut create_directories = Vec::default();

let shell_buf = format!(
"\n\
# Nix\n\
if [ -e '{PROFILE_NIX_FILE_SHELL}' ]; then\n\
{inde}. '{PROFILE_NIX_FILE_SHELL}'\n\
fi\n\
# End Nix\n
\n",
inde = " ", // indent
r#"

# Begin Nix
if [ -f '{PROFILE_NIX_FILE_SHELL}' ]; then
. '{PROFILE_NIX_FILE_SHELL}'
fi
# End Nix

"#
);

create_directories.push(
CreateDirectory::plan("/nix/nix-installer.d", None, None, 0o0755, false)
.await
.map_err(Self::error)?,
);

create_or_insert_files.push(
CreateOrInsertIntoFile::plan(
PROFILE_NIX_FILE_SHELL,
None,
None,
0o644,
include_str!("./profiles/profile.sh").to_string(),
create_or_insert_into_file::Position::Beginning,
)
.await
.map_err(Self::error)?,
);

create_or_insert_files.push(
CreateOrInsertIntoFile::plan(
PROFILE_NIX_FILE_FISH,
None,
None,
0o644,
include_str!("./profiles/profile.fish").to_string(),
create_or_insert_into_file::Position::Beginning,
)
.await
.map_err(Self::error)?,
);

for profile_target in locations.bash.iter().chain(locations.zsh.iter()) {
Expand Down Expand Up @@ -67,14 +100,15 @@ impl ConfigureShellProfile {
}

let fish_buf = format!(
"\n\
# Nix\n\
if test -e '{PROFILE_NIX_FILE_FISH}'\n\
{inde}. '{PROFILE_NIX_FILE_FISH}'\n\
end\n\
# End Nix\n\
\n",
inde = " ", // indent
r#"

# Begin Nix
if [ -f {PROFILE_NIX_FILE_FISH} ]; then
. {PROFILE_NIX_FILE_FISH}
fi
# End Nix

"#
);

for fish_prefix in &locations.fish.confd_prefixes {
Expand Down
6 changes: 6 additions & 0 deletions src/action/common/profiles/profile.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if [ -f /nix/nix-installer ] && [ -x /nix/nix-installer ] && [ -z "${__ETC_PROFILE_NIX_SOURCED:-}" ]; then
/nix/nix-installer export --format null-separated \
| while read --null key
read --export --null "$key"
end
fi
11 changes: 11 additions & 0 deletions src/action/common/profiles/profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# shellcheck shell=sh
if [ -f /nix/nix-installer ] && [ -x /nix/nix-installer ] && [ -z "${__ETC_PROFILE_NIX_SOURCED:-}" ]; then
NIX_INSTALLER_EXPORT_DATA=$(/nix/nix-installer export --verbose --verbose --format space-newline-separated);
while read -r nix_installer_export_key nix_installer_export_value; do
export "$nix_installer_export_key=$nix_installer_export_value";
done <<DATA_INPUT
$NIX_INSTALLER_EXPORT_DATA
DATA_INPUT

unset NIX_INSTALLER_EXPORT_DATA nix_installer_export_key nix_installer_export_value
fi
12 changes: 7 additions & 5 deletions src/action/macos/configure_remote_building.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::action::base::{create_or_insert_into_file, CreateOrInsertIntoFile};
use crate::action::{Action, ActionDescription, ActionError, ActionTag, StatefulAction};

use std::path::Path;

use tracing::{span, Instrument, Span};

const PROFILE_NIX_FILE_SHELL: &str = "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh";
use crate::action::base::{create_or_insert_into_file, CreateOrInsertIntoFile};
use crate::action::common::configure_shell_profile::PROFILE_NIX_FILE_SHELL;
use crate::action::{Action, ActionDescription, ActionError, ActionTag, StatefulAction};

/**
Configure macOS's zshenv to load the Nix environment when ForceCommand is used.
Expand All @@ -20,12 +20,14 @@ impl ConfigureRemoteBuilding {
pub async fn plan() -> Result<StatefulAction<Self>, ActionError> {
let shell_buf = format!(
r#"

# Set up Nix only on SSH connections
# See: https://github.com/DeterminateSystems/nix-installer/pull/714
if [ -e '{PROFILE_NIX_FILE_SHELL}' ] && [ -n "${{SSH_CONNECTION}}" ] && [ "${{SHLVL}}" -eq 1 ]; then
if [ -n "${{SSH_CONNECTION}}" ] && [ "${{SHLVL}}" -eq 1 ] && [ -f '{PROFILE_NIX_FILE_SHELL}' ]; then
. '{PROFILE_NIX_FILE_SHELL}'
fi
# End Nix

"#
);

Expand Down
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl CommandExecute for NixInstallerCli {
NixInstallerSubcommand::Install(install) => install.execute().await,
NixInstallerSubcommand::Repair(restore_shell) => restore_shell.execute().await,
NixInstallerSubcommand::Uninstall(revert) => revert.execute().await,
NixInstallerSubcommand::Export(export) => export.execute().await,
}
}
}
Expand Down
Loading