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