Skip to content

Commit

Permalink
feat(brew): add command-not-found by default after setup
Browse files Browse the repository at this point in the history
  • Loading branch information
tulilirockz committed Nov 3, 2024
1 parent de4cec2 commit 7c8d417
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
enable brew-setup.service
enable brew-update.timer
enable brew-upgrade.timer
enable brew-command-not-found.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Automatically install brew-command-not-found
Wants=network-online.target
After=network-online.target
ConditionPathIsSymbolicLink=/home/linuxbrew/.linuxbrew/bin/brew

[Service]
Type=oneshot
Environment=HOMEBREW_CELLAR=/home/linuxbrew/.linuxbrew/Cellar
Environment=HOMEBREW_PREFIX=/home/linuxbrew/.linuxbrew
Environment=HOMEBREW_REPOSITORY=/home/linuxbrew/.linuxbrew/Homebrew
# Override the user if different UID/User
ExecStart=/usr/bin/bash -c "/usr/bin/sudo -H -i -u \'#1000\' /usr/libexec/brew-command-not-found.sh"
ExecStart=/usr/bin/bash -c "/usr/libexec/brew-command-not-found.sh"

[Install]
WantedBy=default.target multi-user.target
40 changes: 40 additions & 0 deletions system_files/shared/usr/libexec/brew-command-not-found.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# Optionally installs Brew's command-not-found tool if it still isnt installed in the host system

set -xeuo pipefail

BREW_BINARY=/home/linuxbrew/.linuxbrew/bin/brew
BREW_ROOT=${HOMEBREW_REPOSITORY:-$($BREW_BINARY --repository)}

if [ ! -f "${BREW_ROOT}/Library/Taps/homebrew/homebrew-command-not-found" ] && [ "$(id -u)" -ne 0 ] ; then
if ! $BREW_BINARY -h > /dev/null; then
echo "Install Homebrew first!"
exit
fi

# This directory could exist without the user having installed command-not-found but then theres something super wrong with their system
HOMEBREW_NO_ENV_HINTS=true $BREW_BINARY tap homebrew/command-not-found
fi

# Fail quietly in the next steps since for some reason the service that is supposted to run this thinks that theyre root when running as $UID
if [ ! -f "/etc/profile.d/brew-command-not-found.sh" ] ; then
touch /etc/profile.d/brew-command-not-found.sh &> /dev/null || exit 0
tee /etc/profile.d/brew-command-not-found.sh > /dev/null <<EOF
# Check for interactive bash or zsh and that we haven't already been sourced
if [[ -d /home/linuxbrew/.linuxbrew && \$- == *i* && BREW_COMMAND_NOT_FOUND != 1 ]] ; then
HB_CNF_HANDLER="${BREW_ROOT}/Library/Taps/homebrew/homebrew-command-not-found/handler.sh"
[ -f "\$HB_CNF_HANDLER" ] && source "\$HB_CNF_HANDLER"
export BREW_COMMAND_NOT_FOUND=1
fi
EOF
fi

if [ ! -f "/etc/fish/conf.d/homebrew-command-not-found.fish" ] ; then
touch /etc/fish/conf.d/brew-command-not-found.fish &> /dev/null || exit 0
tee /etc/fish/conf.d/brew-command-not-found.fish > /dev/null <<EOF
set HB_CNF_HANDLER "${BREW_ROOT}/Library/Taps/homebrew/homebrew-command-not-found/handler.fish"
if test -f \$HB_CNF_HANDLER
source \$HB_CNF_HANDLER
end
EOF
fi

0 comments on commit 7c8d417

Please sign in to comment.