Skip to content
Open
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
76 changes: 44 additions & 32 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -euo pipefail

# Rootless-DevBox Installer
#
#
# This script automates the installation of DevBox in a rootless environment
# using nix-user-chroot without requiring root privileges.
#
Expand Down Expand Up @@ -45,7 +45,7 @@ print_error() {
check_user_namespace_support() {
print_step "Checking system compatibility"
echo "Verifying if your system supports unprivileged user namespaces (required for nix-user-chroot)..."

if ! unshare --user --pid echo "YES" &>/dev/null; then
print_error "Your system does not support unprivileged user namespaces, which is required for nix-user-chroot to work.

Expand All @@ -62,8 +62,9 @@ For more information, see: https://github.com/nix-community/nix-user-chroot"

# Detect system architecture
get_architecture() {
local arch=$(uname -m)

local arch
arch=$(uname -m)

case "$arch" in
x86_64)
echo "x86_64-unknown-linux-musl"
Expand All @@ -87,7 +88,7 @@ get_architecture() {
download_file() {
local url="$1"
local output_file="$2"

if command -v curl &>/dev/null; then
curl -fsSL "$url" -o "$output_file" || return 1
elif command -v wget &>/dev/null; then
Expand Down Expand Up @@ -130,8 +131,10 @@ main() {
local nix_user_chroot_path="${local_bin_dir}/nix-user-chroot"
local nix_dir="${HOME}/.nix"
local nix_user_chroot_version="1.2.2"
local arch=$(get_architecture)
local temp_dir=$(create_temp_dir)
local arch
local temp_dir
arch=$(get_architecture)
temp_dir=$(create_temp_dir)

if [ -x "$devbox_path" ] && [ -x "$nix_chroot_path" ] && [ -x "$nix_user_chroot_path" ]; then
echo_color "$GREEN" "All components are already installed!"
Expand Down Expand Up @@ -203,10 +206,10 @@ EOF

# Step 6: Continue with DevBox installation process
print_step "Preparing DevBox installation"

local devbox_install_script="${temp_dir}/devbox_install_user.sh"
local permanent_devbox_install_script="${HOME}/devbox_install_user.sh"
local permanent_devbox_install_script="${HOME}/devbox_install_user.sh"

cat > "$devbox_install_script" <<'EOF'
#!/bin/bash
#
Expand Down Expand Up @@ -425,16 +428,16 @@ main() {

main "$@"
EOF

chmod +x "$devbox_install_script"
cp "$devbox_install_script" "$permanent_devbox_install_script"
chmod +x "$permanent_devbox_install_script"
print_success "Created DevBox installation script: ${devbox_install_script}"
print_success "Created permanent backup at: ${permanent_devbox_install_script}"

# Step 7: Set Nix mirror if needed
set_nix_mirror_if_needed

echo ""
echo_color "$BOLD" "Next: Activate Nix Environment and Install DevBox"
echo_color "$BOLD" "⚠️ IMPORTANT: Installation requires TWO steps! ⚠️"
Expand All @@ -445,16 +448,16 @@ EOF
echo ""
echo "Would you like this script to attempt to run '${local_bin_dir}/nix-chroot' for you now? [Y/n]"
echo_color "$GREY" "(If you choose yes, after nix-chroot starts, you MUST MANUALLY run the second command shown above)"

if [ -t 0 ]; then
read -r response
else
response="y"
echo "Non-interactive mode detected. Defaulting to 'y'."
fi

response=${response:-y}

if [[ "$response" =~ ^[Yy] ]]; then
echo ""
echo_color "$CYAN" "Attempting to start ${local_bin_dir}/nix-chroot..."
Expand All @@ -467,16 +470,19 @@ EOF
if [ ! -x "${local_bin_dir}/nix-chroot" ]; then
print_error "${local_bin_dir}/nix-chroot not found or not executable. Please check previous steps."
fi


# shellcheck disable=2016
if ! grep -qF 'export PATH="$HOME/.local/bin:$PATH" # Added by Rootless-DevBox' ~/.bashrc; then
echo '' >> ~/.bashrc
echo '# Added by Rootless-DevBox installer' >> ~/.bashrc
echo 'export PATH="$HOME/.local/bin:$PATH" # Added by Rootless-DevBox' >> ~/.bashrc
cat >> "${HOME}/.bashrc" <<EOF

# Added by Rootless-DevBox installer
export PATH="\$HOME/.local/bin:\$PATH" # Added by Rootless-DevBox
EOF
echo "Added ~/.local/bin to PATH in ~/.bashrc"
fi

if ! grep -qF '# Rootless-DevBox nix-chroot environment indicator' ~/.bashrc; then
echo '' >> ~/.bashrc
echo '' >> ~/.bashrc
cat >> ~/.bashrc <<EOF
# Rootless-DevBox nix-chroot environment indicator
if [ "\$NIX_CHROOT" = "1" ]; then
Expand All @@ -485,7 +491,7 @@ fi
EOF
echo "Added nix-chroot environment indicator to ~/.bashrc"
fi

echo_color "$YELLOW" "Environment variables configured. Now installing DevBox automatically in nix-chroot environment..."
"${local_bin_dir}/nix-user-chroot" "${nix_dir}" env NIX_CHROOT=1 bash "${permanent_devbox_install_script}"
if [ -x "${local_bin_dir}/devbox" ]; then
Expand All @@ -510,20 +516,24 @@ EOF
read -r configure_bashrc
if [[ "$configure_bashrc" =~ ^[Yy] ]]; then
print_step "Configuring environment variables in ~/.bashrc"

local bashrc_modified_count=0
# shellcheck disable=2016
if ! grep -qF 'export PATH="$HOME/.local/bin:$PATH" # Added by Rootless-DevBox' ~/.bashrc; then
echo '' >> ~/.bashrc
echo '# Added by Rootless-DevBox installer' >> ~/.bashrc
echo 'export PATH="$HOME/.local/bin:$PATH" # Added by Rootless-DevBox' >> ~/.bashrc
cat >> "${HOME}/.bashrc" <<EOF

# Added by Rootless-DevBox installer
export PATH="\$HOME/.local/bin:\$PATH" # Added by Rootless-DevBox
EOF
echo "Added ~/.local/bin to PATH in ~/.bashrc"
((bashrc_modified_count++))
else
# shellcheck disable=SC2088
echo "~/.local/bin PATH entry by Rootless-DevBox already in ~/.bashrc."
fi

if ! grep -qF '# Rootless-DevBox nix-chroot environment indicator' ~/.bashrc; then
echo '' >> ~/.bashrc
echo '' >> ~/.bashrc
cat >> ~/.bashrc <<EOF
# Rootless-DevBox nix-chroot environment indicator
if [ "\$NIX_CHROOT" = "1" ]; then
Expand All @@ -533,13 +543,15 @@ EOF
echo "Added nix-chroot environment indicator to ~/.bashrc"
((bashrc_modified_count++))
else
# shellcheck disable=SC2088
echo "nix-chroot environment indicator already in ~/.bashrc."
fi

if [ "$bashrc_modified_count" -gt 0 ]; then
print_success "Configuration changes applied to ~/.bashrc."
echo "Please run 'source ~/.bashrc' or open a new terminal for these changes to take full effect."
else
# shellcheck disable=SC2088
print_success "~/.bashrc already contains the necessary configurations or no changes were made."
fi
echo ""
Expand All @@ -555,7 +567,7 @@ EOF
echo "to your ~/.bashrc (or equivalent shell configuration file)."
fi
fi

echo ""
echo_color "$YELLOW" "⚠️ Verify Installation ⚠️"
echo "After installation, please confirm the following files exist:"
Expand All @@ -565,7 +577,7 @@ EOF
echo ""
echo "If devbox doesn't exist, please enter the nix-chroot environment and run:"
echo_color "$CYAN" "${permanent_devbox_install_script}"

echo ""
echo "If you encounter any issues, please report them at: https://github.com/nebstudio/Rootless-DevBox/issues"
echo "If this script was helpful, please consider giving a Star on GitHub! ⭐"
Expand Down
9 changes: 6 additions & 3 deletions uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ BLUE="\033[0;34m"
YELLOW="\033[0;33m"
RED="\033[0;31m"
RESET="\033[0m"
GREY="\033[90m"
# GREY="\033[90m"
CYAN="\033[0;36m"

# Echo with color
Expand Down Expand Up @@ -80,6 +80,7 @@ main() {
local nix_user_chroot_path="${local_bin_dir}/nix-user-chroot"

# Check if any component is installed
# shellcheck disable=2016
if [ ! -f "$devbox_path" ] && \
[ ! -f "$nix_chroot_path" ] && \
[ ! -f "$nix_user_chroot_path" ] && \
Expand Down Expand Up @@ -110,8 +111,10 @@ main() {
if [ ! -f "$bashrc_file" ]; then
print_warning "${bashrc_file} not found. Skipping .bashrc modifications."
else
# shellcheck disable=SC2016
if grep -qE '(# Added by Rootless-DevBox installer|export PATH="\$HOME/\.local/bin:\$PATH" # Added by Rootless-DevBox|# Rootless-DevBox nix-chroot environment indicator)' "$bashrc_file"; then
local bashrc_backup="${HOME}/.bashrc.devbox_uninstall_$(date +%Y%m%d%H%M%S).bak"
local bashrc_backup
bashrc_backup="${HOME}/.bashrc.devbox_uninstall_$(date +%Y%m%d%H%M%S).bak"
echo "Modifying ${bashrc_file} to remove Rootless-DevBox configurations."
echo "Backing up current ${bashrc_file} to ${bashrc_backup}"
if cp "${bashrc_file}" "${bashrc_backup}"; then
Expand Down Expand Up @@ -205,4 +208,4 @@ main() {

# Run the main function
main "$@"
exit 0
exit 0