brew.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env bash
  2. BREW_PREFIX=${BREW_PREFIX:-"/home/vscode/.linuxbrew"}
  3. SHALLOW_CLONE=${SHALLOWCLONE:-"true"}
  4. USERNAME=${USERNAME:-"automatic"}
  5. ARCHITECTURE="$(uname -m)"
  6. if [ "${ARCHITECTURE}" != "amd64" ] && [ "${ARCHITECTURE}" != "x86_64" ] && [ "${ARCHITECTURE}" != "aarch64" ]; then
  7. echo "(!) Architecture $ARCHITECTURE unsupported"
  8. exit 1
  9. fi
  10. cleanup() {
  11. source /etc/os-release
  12. case "${ID}" in
  13. debian|ubuntu)
  14. rm -rf /var/lib/apt/lists/*
  15. ;;
  16. esac
  17. }
  18. if [ "$(id -u)" -ne 0 ]; then
  19. echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
  20. exit 1
  21. fi
  22. # Ensure that login shells get the correct path if the user updated the PATH using ENV.
  23. rm -f /etc/profile.d/00-restore-env.sh
  24. echo "export PATH=${PATH//$(sh -lc 'echo $PATH')/\$PATH}" > /etc/profile.d/00-restore-env.sh
  25. chmod +x /etc/profile.d/00-restore-env.sh
  26. # Determine the appropriate non-root user
  27. if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
  28. USERNAME=""
  29. POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
  30. for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
  31. if id -u ${CURRENT_USER} > /dev/null 2>&1; then
  32. USERNAME=${CURRENT_USER}
  33. break
  34. fi
  35. done
  36. if [ "${USERNAME}" = "" ]; then
  37. USERNAME=root
  38. fi
  39. elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
  40. USERNAME=root
  41. fi
  42. apt_get_update() {
  43. if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
  44. echo "Running apt-get update..."
  45. apt-get update -y
  46. fi
  47. }
  48. # Checks if packages are installed and installs them if not
  49. check_packages() {
  50. source /etc/os-release
  51. case "${ID}" in
  52. debian|ubuntu)
  53. if ! dpkg -s "$@" >/dev/null 2>&1; then
  54. apt_get_update
  55. apt-get -y install --no-install-recommends "$@"
  56. fi
  57. ;;
  58. alpine)
  59. if ! apk -e info "$@" >/dev/null 2>&1; then
  60. apk add --no-cache "$@"
  61. fi
  62. ;;
  63. esac
  64. }
  65. updaterc() {
  66. if [ "${UPDATE_RC}" = "true" ]; then
  67. echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..."
  68. if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then
  69. echo -e "$1" >> /etc/bash.bashrc
  70. fi
  71. if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then
  72. echo -e "$1" >> /etc/zsh/zshrc
  73. fi
  74. fi
  75. }
  76. updatefishconfig() {
  77. if [ "${UPDATE_RC}" = "true" ]; then
  78. echo "Updating /etc/fish/config.fish..."
  79. if [ -f "/etc/fish/config.fish" ]; then
  80. echo -e "$1" >> /etc/fish/config.fish
  81. fi
  82. fi
  83. }
  84. export DEBIAN_FRONTEND=noninteractive
  85. # Clean up
  86. cleanup
  87. # Install dependencies if missing
  88. check_packages \
  89. bzip2 \
  90. ca-certificates \
  91. curl \
  92. file \
  93. fonts-dejavu-core \
  94. g++ \
  95. git \
  96. less \
  97. libz-dev \
  98. locales \
  99. make \
  100. netbase \
  101. openssh-client \
  102. patch \
  103. sudo \
  104. tzdata \
  105. uuid-runtime
  106. # Install Homebrew
  107. mkdir -p "${BREW_PREFIX}"
  108. echo "Installing Homebrew..."
  109. if [ "${SHALLOW_CLONE}" = "false" ]; then
  110. git clone https://github.com/Homebrew/brew "${BREW_PREFIX}/Homebrew"
  111. mkdir -p "${BREW_PREFIX}/Homebrew/Library/Taps/homebrew"
  112. git clone https://github.com/Homebrew/homebrew-core "${BREW_PREFIX}/Homebrew/Library/Taps/homebrew/homebrew-core"
  113. else
  114. echo "Using shallow clone..."
  115. git clone --depth 1 https://github.com/Homebrew/brew "${BREW_PREFIX}/Homebrew"
  116. mkdir -p "${BREW_PREFIX}/Homebrew/Library/Taps/homebrew"
  117. git clone --depth 1 https://github.com/Homebrew/homebrew-core "${BREW_PREFIX}/Homebrew/Library/Taps/homebrew/homebrew-core"
  118. # Disable automatic updates as they are not allowed with shallow clone installation
  119. updaterc "export HOMEBREW_NO_AUTO_UPDATE=1"
  120. updatefishconfig "set -gx HOMEBREW_NO_AUTO_UPDATE 1"
  121. fi
  122. "${BREW_PREFIX}/Homebrew/bin/brew" config
  123. mkdir "${BREW_PREFIX}/bin"
  124. ln -s "${BREW_PREFIX}/Homebrew/bin/brew" "${BREW_PREFIX}/bin"
  125. chown -R ${USERNAME} "${BREW_PREFIX}"
  126. echo "Done!"