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

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ which = "4.4.0"
sysctl = "0.5.4"
walkdir = "2.3.3"
indexmap = { version = "2.0.2", features = ["serde"] }
export = { git = "https://github.com/DeterminateSystems/export" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR can merge this git dependency either needs to be published to crates.io or pulled into this repo as a workspace.

Since nix-installer is published to crates.io, we cannot have git deps.


[dev-dependencies]
eyre = { version = "0.6.8", default-features = false, features = [ "track-caller" ] }
Expand Down
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
3 changes: 3 additions & 0 deletions src/action/common/profiles/profile.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if [ -f /nix/nix-installer ] && [ -x /nix/nix-installer ] && not set -q __ETC_PROFILE_NIX_SOURCED;
eval "$(/nix/nix-installer export --format fish)"
end
5 changes: 5 additions & 0 deletions src/action/common/profiles/profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# shellcheck shell=sh

if [ -f /nix/nix-installer ] && [ -x /nix/nix-installer ] && [ -z "${__ETC_PROFILE_NIX_SOURCED:-}" ]; then
eval "$(/nix/nix-installer export --format sh)"
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
Loading