revert
Some checks failed
Nix Format Check / check-format (push) Has been cancelled

This commit is contained in:
Menno van Leeuwen 2025-03-11 11:09:47 +01:00
parent 3854200b18
commit 52aaa71f84
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

181
setup.sh
View File

@ -9,93 +9,37 @@ readonly GIT_REPO="https://git.mvl.sh/vleeuwenmenno/dotfiles.git" # Dotfiles r
readonly DOTFILES_PATH="${HOME}/.dotfiles" # Dotfiles directory
readonly SETUP_MARKER="${HOME}/.dotfiles-setup" # Setup marker file indicates setup has been run
#Color print function, usage: println "message" "color"
println() {
color=$2
printfe "%s\n" $color "$1"
# Color constants
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[0;33m'
readonly NC='\033[0m' # No Color
# Helper functions
log_info() {
echo -e "${YELLOW}$1${NC}"
}
# print colored with printf (args: format, color, message ...)
printfe() {
format=$1
color=$2
message=$3
show_time=true
# Check if $4 is explicitly set to false, otherwise default to true
if [ ! -z "$4" ] && [ "$4" == "false" ]; then
show_time=false
fi
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
normal=$(tput sgr0)
grey=$(tput setaf 8)
case $color in
"red")
color=$red
;;
"green")
color=$green
;;
"yellow")
color=$yellow
;;
"blue")
color=$blue
;;
"magenta")
color=$magenta
;;
"cyan")
color=$cyan
;;
"grey")
color=$grey
;;
*)
color=$normal
;;
esac
if [ "$show_time" == "false" ]; then
printf "$color$format$normal" "$message"
return
fi
printf $grey"%s" "$(date +'%H:%M:%S')"$normal
case $color in
$green | $cyan | $blue | $magenta | $normal)
printf "$green INF $normal"
;;
$yellow)
printf "$yellow WRN $normal"
;;
$red)
printf "$red ERR $normal"
;;
*)
printf "$normal"
;;
esac
printf "$color$format$normal" "$message"
log_success() {
echo -e "${GREEN}$1${NC}"
}
log_error() {
echo -e "${RED}$1${NC}" >&2
}
log_warning() {
echo -e "${YELLOW}$1${NC}" >&2
}
# Helper function for error & exit
die() {
println "$1" "red" >&2
log_error "$1"
exit 1
}
# Request sudo credentials upfront
request_sudo() {
println "Requesting sudo privileges to avoid interruptions later..." "green"
log_info "Requesting sudo privileges to avoid interruptions later..."
sudo -v || die "Failed to obtain sudo privileges"
# Keep sudo credentials refreshed in the background
@ -105,7 +49,7 @@ request_sudo() {
# Ensure we kill the keepalive process when the script exits
trap 'kill $SUDO_KEEPALIVE_PID 2>/dev/null || true' EXIT
println "Sudo privileges obtained." "green"
log_success "Sudo privileges obtained."
}
# Ensure we're running interactively
@ -129,7 +73,7 @@ backup_file() {
local need_sudo="${2:-false}"
if [ -f "$file" ]; then
println "Backing up $file to $file.bak..." "green"
log_info "Backing up $file to $file.bak..."
if [ "$need_sudo" = "true" ]; then
sudo mv "$file" "$file.bak" || die "Failed to backup $file (sudo)"
else
@ -138,6 +82,7 @@ backup_file() {
fi
}
check_prerequisites() {
command -v git >/dev/null 2>&1 || die "Git is required but not installed"
command -v sudo >/dev/null 2>&1 || die "Sudo is required but not installed"
@ -196,16 +141,16 @@ update_home_manager_flake() {
# Replace original file
mv "$temp_file" "$flake_file" || return 1
println "Home Manager Flake configuration added successfully." "green"
log_success "Home Manager Flake configuration added successfully."
}
install_nix() {
if command -v nix-channel >/dev/null 2>&1; then
println "Detected Nix, skipping Nix setup." "green"
log_success "Detected Nix, skipping Nix setup."
return 0
fi
println "Nix not detected, installing Nix..." "green"
log_info "Nix not detected, installing Nix..."
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix -o install-nix.sh || \
die "Failed to download Nix installer"
@ -215,34 +160,34 @@ install_nix() {
}
setup_symlinks() {
println "Setting up symlinks..." "green"
log_info "Setting up symlinks..."
# Backup and create symlinks for user files
backup_file "$HOME/.bashrc"
backup_file "$HOME/.profile"
if [ -d "$HOME/.config/home-manager" ]; then
println "Backing up ~/.config/home-manager to ~/.config/home-manager.bak..." "green"
log_info "Backing up ~/.config/home-manager to ~/.config/home-manager.bak..."
mv "$HOME/.config/home-manager" "$HOME/.config/home-manager.bak" || \
die "Failed to backup home-manager config"
fi
println "Linking ~/.config/home-manager to $DOTFILES_PATH/config/home-manager..." "green"
log_info "Linking ~/.config/home-manager to $DOTFILES_PATH/config/home-manager..."
ln -s "$DOTFILES_PATH/config/home-manager" "$HOME/.config/home-manager" || \
die "Failed to create home-manager symlink"
# Verify symlinks
confirm_symlink "$HOME/.config/home-manager" "Failed to set up home-manager symlink"
println "Symlinks set up successfully." "green"
log_success "Symlinks set up successfully."
}
install_home_manager() {
if command -v home-manager >/dev/null 2>&1; then
println "Home Manager already installed. Skipping..." "green"
log_success "Home Manager already installed. Skipping..."
return 0
fi
println "Installing Home Manager..." "green"
log_info "Installing Home Manager..."
nix-channel --add "https://github.com/nix-community/home-manager/archive/release-$NIXOS_RELEASE.tar.gz" home-manager || die "Failed to add home-manager channel"
nix-channel --update || die "Failed to update channels"
nix-shell '<home-manager>' -A install || die "Failed to install home-manager"
@ -257,57 +202,57 @@ prepare_hostname() {
if ! validate_hostname "$hostname"; then
die "Invalid hostname provided. Please use a valid hostname."
fi
println "Using provided hostname: $hostname" "green"
log_info "Using provided hostname: $hostname"
# Check if hostname is already set
elif [ -f "$hostname_file" ]; then
hostname=$(cat "$hostname_file")
println "Hostname already found in $hostname_file. Using $hostname." "green"
log_success "Hostname already found in $hostname_file. Using $hostname."
return
else
die "No hostname provided. Please provide a hostname as the first argument."
fi
println "Setting hostname to $hostname..." "green"
log_info "Setting hostname to $hostname..."
sudo hostnamectl set-hostname "$hostname" || die "Failed to set hostname"
echo "$hostname" > "$hostname_file" || die "Failed to save hostname"
println "Hostname set successfully." "green"
log_success "Hostname set successfully."
}
warning_prompt() {
println "This script will set up your machine using Menno's Dotfiles repository.\n" "green"
println "Please ensure you have a backup of your data before proceeding." "red"
println "This script will modify system files and may require sudo permissions.\n" "red"
println "This script works best on a fresh Fedora, Ubuntu or Arch Linux installation." "green"
println "Setup starts in 5 seconds, to abort use Ctrl+C to exit NOW." "green"
log_success "This script will set up your machine using Menno's Dotfiles repository.\n"
log_error "Please ensure you have a backup of your data before proceeding."
log_error "This script will modify system files and may require sudo permissions.\n"
log_info "This script works best on a fresh Fedora, Ubuntu or Arch Linux installation."
log_info "Setup starts in 5 seconds, to abort use Ctrl+C to exit NOW."
sleep 5
echo ""
println "Starting setup..." "green"
log_info "Starting setup..."
}
check_selinux() {
# Check if distro has SELinux at all:
if [ ! -d /etc/selinux ]; then
println "SELinux not found. Skipping..." "green"
log_success "SELinux not found. Skipping..."
return 0
fi
# Check if getenforce exists, if not it means we don't have SELinux
if ! command -v getenforce >/dev/null 2>&1; then
println "SELinux not found. Skipping..." "green"
log_success "SELinux not found. Skipping..."
return 0
fi
# Check if getenforce is returning Enforcing
if [ "$(getenforce)" = "Enforcing" ]; then
println "SELinux is enabled. Adjusting SELinux to permissive mode..." "yellow"
log_warning "SELinux is enabled. Adjusting SELinux to permissive mode..."
sudo setenforce Permissive || die "Failed to disable SELinux"
sudo tee /etc/selinux/config << EOF > /dev/null || die "Failed to write to /etc/selinux/config"
SELINUX=permissive
SELINUXTYPE=targeted
EOF
println "SELinux disabled successfully." "green"
log_success "SELinux disabled successfully."
fi
}
@ -336,12 +281,12 @@ attempt_package_install() {
elif command -v pacman >/dev/null; then
package_manager="pacman"
else
println "No supported package manager was found, aborting setup..." "red"
log_error "No supported package manager was found, aborting setup..."
exit 1
fi
if ! command -v "$package" >/dev/null 2>&1; then
println "Installing $package using $package_manager..." "green"
log_info "Installing $package using $package_manager..."
if [ "$package_manager" = "dnf" ]; then
sudo dnf install "$package" -y || die "Failed to install $package"
elif [ "$package_manager" = "apt" ]; then
@ -378,16 +323,16 @@ check_compatibility() {
case "$distro" in
Fedora*)
println "Detected Fedora. Proceeding with setup..." "green"
log_success "Detected Fedora. Proceeding with setup..."
check_command_availibility "dnf"
;;
Ubuntu)
println "Detected Ubuntu. Proceeding with setup..." "green"
log_success "Detected Ubuntu. Proceeding with setup..."
check_command_availibility "apt"
;;
Arch*)
println "Detected Arch Linux. Setup has not been tested on Arch Linux." "yellow"
println "Proceed at your own risk..." "yellow"
log_warning "Detected Arch Linux. Setup has not been tested on Arch Linux."
log_warning "Proceed at your own risk..."
check_command_availibility "pacman"
;;
*)
@ -401,9 +346,9 @@ ensure_shell() {
local shell
shell=$(getent passwd "$USER" | cut -d: -f7)
if [ "$shell" != "/bin/bash" ]; then
println "Setting default shell to bash..." "green"
log_info "Setting default shell to bash..."
chsh -s /bin/bash || die "Failed to set default shell to bash"
println "Default shell set to bash." "green"
log_success "Default shell set to bash."
fi
# Ensure shell is set for root user
@ -411,9 +356,9 @@ ensure_shell() {
local root_shell
root_shell=$(getent passwd root | cut -d: -f7)
if [ "$root_shell" != "/bin/bash" ]; then
println "Setting default shell for root to bash..." "green"
log_info "Setting default shell for root to bash..."
sudo chsh -s /bin/bash root || die "Failed to set default shell for root to bash"
println "Default shell for root set to bash." "green"
log_success "Default shell for root set to bash."
fi
fi
}
@ -423,7 +368,7 @@ main() {
# Check if setup has already been run
if [ -f "$SETUP_MARKER" ]; then
println "Setup has already been run, exiting..." "green"
log_info "Setup has already been run, exiting..."
exit 0
fi
@ -444,12 +389,12 @@ main() {
# Clone dotfiles if needed
if [ ! -d "$DOTFILES_PATH" ]; then
println "Cloning dotfiles repo..." "green"
log_info "Cloning dotfiles repo..."
git clone "$GIT_REPO" "$DOTFILES_PATH" || die "Failed to clone dotfiles repository"
fi
if [ "$continue_flag" = "--continue" ]; then
println "Continuing setup..." "green"
log_info "Continuing setup..."
else
warning_prompt
prepare_hostname "$hostname"
@ -480,9 +425,9 @@ EOF
touch "$SETUP_MARKER" || die "Failed to create setup marker"
# Final success message
println "\nSetup complete. Please logout / restart to continue with 'dotf update'.\n" "green"
println "\n!!! Please logout / restart to continue !!!" "red"
println "~~~ Proceed by running 'dotf update' ~~~\n" "red"
log_success "\nSetup complete. Please logout / restart to continue with 'dotf update'.\n"
log_error "\n!!! Please logout / restart to continue !!!"
log_error "~~~ Proceed by running 'dotf update' ~~~\n"
}
main "$@"