#!/bin/bash # 1. Define your strings BASE_FOLDER="/sf/bernina/applications/python" ENVS_DIR="${BASE_FOLDER}/environments" ENV_NAME=$1 # Helper function to list environments cleanly list_envs() { echo "Available Pixi environments in $ENVS_DIR:" if [ -d "$ENVS_DIR" ]; then # Find all directories that contain a 'bin' folder find -L "$ENVS_DIR" -maxdepth 2 -mindepth 2 -type d -name "bin" | while read -r bin_dir; do # Extract the parent folder name (the environment name) basename "$(dirname "$bin_dir")" | sed 's/^/ - /' done else echo " (Environments directory not found)" fi } # 2. Check if an environment argument was passed and is valid ENV_PATH="${ENVS_DIR}/${ENV_NAME}" BIN_PATH="${ENV_PATH}/bin" if [ -z "$ENV_NAME" ] || [ ! -d "$BIN_PATH" ]; then if [ -n "$ENV_NAME" ]; then echo "Error: Could not find an environment folder at ${BIN_PATH}" echo "" fi echo "Usage: source activate.sh " echo "" list_envs return 1 2>/dev/null || exit 1 fi # 3. Clean up the PATH # Strips out any previous paths belonging to your environments folder NEW_PATH="" IFS=':' read -ra PATH_ARRAY <<< "$PATH" for segment in "${PATH_ARRAY[@]}"; do if [[ "$segment" != *"${ENVS_DIR}"* ]]; then if [ -z "$NEW_PATH" ]; then NEW_PATH="$segment" else NEW_PATH="$NEW_PATH:$segment" fi fi done # 4. Rebuild PATH and export environment variables export PATH="${BIN_PATH}:${NEW_PATH}" export CONDA_PREFIX="${ENV_PATH}" export CONDA_DEFAULT_ENV="${ENV_NAME}" export PIXI_ENVIRONMENT_NAME="${ENV_NAME}" # Optional: Source package-specific startup scripts if they exist if [ -d "${ENV_PATH}/etc/conda/activate.d" ]; then for f in "${ENV_PATH}/etc/conda/activate.d"/*.sh; do [ -r "$f" ] && source "$f" done fi echo "Activated '${ENV_NAME}' successfully (Raw Path Mode)."