This commit is contained in:
Menno van Leeuwen 2024-11-08 14:11:53 +01:00
parent 7ba5ab2067
commit 792af5e4c6
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

486
setup.sh
View File

@ -1,30 +1,73 @@
#!/usr/bin/env bash #!/usr/bin/env bash
NIXOS_RELEASE=24.05 set -euo pipefail
GIT_REPO=https://git.mvl.sh/vleeuwenmenno/dotfiles.git IFS=$'\n\t'
# Check if $HOME/.dotfiles-setup exists, if so exit because setup has already been run # Constants
if [ -f $HOME/.dotfiles-setup ]; then readonly NIXOS_RELEASE="24.05"
echo "Setup has already been run, exiting..." readonly GIT_REPO="https://git.mvl.sh/vleeuwenmenno/dotfiles.git"
exit 0 readonly DOTFILES_DIR="${HOME}/dotfiles"
fi readonly SETUP_MARKER="${HOME}/.dotfiles-setup"
# Check if $HOME/dotfiles exists, if not clone the dotfiles repo # Color constants
if [ ! -d $HOME/dotfiles ]; then readonly RED='\033[0;31m'
tput setaf 3 readonly GREEN='\033[0;32m'
echo "Cloning dotfiles repo..." readonly YELLOW='\033[0;33m'
tput sgr0 readonly NC='\033[0m' # No Color
git clone $GIT_REPO $HOME/dotfiles
fi # Helper functions
log_info() {
echo -e "${YELLOW}$1${NC}"
}
log_success() {
echo -e "${GREEN}$1${NC}"
}
log_error() {
echo -e "${RED}$1${NC}" >&2
}
die() {
log_error "$1"
exit 1
}
confirm_symlink() {
local link="$1"
local msg="$2"
if [ ! -L "$link" ]; then
die "$msg"
fi
}
backup_file() {
local file="$1"
if [ -f "$file" ]; then
log_info "Backing up $file to $file.bak..."
mv "$file" "$file.bak" || die "Failed to 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"
}
validate_hostname() {
local hostname="$1"
if [[ -z "$hostname" || ! "$hostname" =~ ^[a-zA-Z0-9_-]+$ || ${#hostname} -gt 64 ]]; then
return 1
fi
return 0
}
create_hardware_config() { create_hardware_config() {
hostname=$1 local hostname="$1"
log_info "Creating hardware configuration for $hostname..."
tput setaf 3 local config_file="$DOTFILES_DIR/config/nixos/hardware/$hostname.nix"
echo "Creating hardware configuration for $hostname..." local template=$(cat << 'EOF'
tput sgr0
template="
{ {
config, config,
lib, lib,
@ -34,49 +77,62 @@ create_hardware_config() {
}: }:
{ {
imports = [ /etc/nixos/hardware-configuration.nix ]; imports = [ /etc/nixos/hardware-configuration.nix ];
networking.hostName = \"$hostname\"; networking.hostName = "%s";
}" }
EOF
)
echo "$template" > $HOME/dotfiles/config/nixos/hardware/$hostname.nix printf "$template" "$hostname" > "$config_file" || \
die "Failed to create hardware configuration"
if [ -f $HOME/dotfiles/config/nixos/hardware/$hostname.nix ]; then log_success "Hardware configuration created successfully."
tput setaf 2 log_info "Consider adding additional hardware configuration to $config_file"
echo "Hardware configuration created successfully."
echo "Consider adding additional hardware configuration to ~/dotfiles/config/nixos/hardware/$hostname.nix."
tput sgr0
else
tput setaf 1
echo "Failed to create hardware configuration. Exiting..."
tput sgr0
exit 1
fi
# Ask if this is a server or workstation # System type selection
tput setaf 3 local systemType
echo "Is this a server or workstation? (s/w)"
tput sgr0
while true; do while true; do
read -p "(s/w): " systemType log_info "Is this a server or workstation? (s/w)"
if [[ $systemType == "s" || $systemType == "w" ]]; then read -r -p "(s/w): " systemType
if [[ "$systemType" =~ ^[sw]$ ]]; then
break break
fi fi
log_error "Invalid input. Please enter 's' for server or 'w' for workstation."
echo "Invalid input. Please enter 's' for server or 'w' for workstation."
done done
local isServer="false"
if [ $systemType == "s" ]; then local isWorkstation="false"
if [ "$systemType" = "s" ]; then
isServer="true" isServer="true"
isWorkstation="false"
else else
isServer="false"
isWorkstation="true" isWorkstation="true"
fi fi
flakeConfiguration=" # Update flake configurations
update_nixos_flake "$hostname" "$isServer" "$isWorkstation" || \
die "Failed to update NixOS flake configuration"
update_home_manager_flake "$hostname" "$isServer" || \
die "Failed to update Home Manager flake configuration"
"$hostname" = nixpkgs.lib.nixosSystem { # Add new files to git
git -C "$DOTFILES_DIR" add \
"config/nixos/hardware/$hostname.nix" \
"config/nixos/flake.nix" \
"config/home-manager/flake.nix" || \
die "Failed to add files to git"
log_info "\nDon't forget to commit and push the changes to the dotfiles repo after testing."
git -C "$DOTFILES_DIR" status
echo
}
update_nixos_flake() {
local hostname="$1"
local isServer="$2"
local isWorkstation="$3"
local flake_config="
\"$hostname\" = nixpkgs.lib.nixosSystem {
inherit system; inherit system;
modules = [ modules = [
./hardware/$hostname.nix ./hardware/$hostname.nix
@ -89,266 +145,180 @@ create_hardware_config() {
isServer = $isServer; isServer = $isServer;
}; };
}; };
" "
# Insert flakeConfiguration into flake.nix at nixosConfigurations = { ... } local flake_file="$DOTFILES_DIR/config/nixos/flake.nix"
sed -i "s/nixosConfigurations = {/nixosConfigurations = { $flakeConfiguration/g" $HOME/dotfiles/config/nixos/flake.nix sed -i "s/nixosConfigurations = {/nixosConfigurations = { $flake_config/g" "$flake_file"
# Validate flake.nix with nixfmt # Validate flake.nix
nix-shell -p nixfmt --run "nixfmt $HOME/dotfiles/config/nixos/flake.nix" nix-shell -p nixfmt --run "nixfmt $flake_file" || return 1
log_success "NixOS Flake configuration added successfully."
}
if [ $? -ne 0 ]; then update_home_manager_flake() {
tput setaf 1 local hostname="$1"
echo "Something went wrong adding the flake configuration for NixOS." local isServer="$2"
echo "Failed to validate flake.nix. Exiting..."
tput sgr0
exit 1
fi
tput setaf 2 local flake_config="
echo "NixOS Flake configuration added successfully." \"$hostname\" = home-manager.lib.homeManagerConfiguration {
tput sgr0
haFlakeConfiguration="
"$hostname" = home-manager.lib.homeManagerConfiguration {
inherit pkgs; inherit pkgs;
modules = [ ./home.nix ]; modules = [ ./home.nix ];
extraSpecialArgs = { extraSpecialArgs = {
inherit pkgs pkgs-unstable; inherit pkgs pkgs-unstable;
isServer = $isServer; isServer = $isServer;
hostname = "$hostname"; hostname = \"$hostname\";
}; };
}; };
" "
# Insert haFlakeConfiguration into flake.nix of home-manager at homeConfigurations = { local flake_file="$DOTFILES_DIR/config/home-manager/flake.nix"
sed -i "s/homeConfigurations = {/homeConfigurations = { $haFlakeConfiguration/g" $HOME/dotfiles/config/home-manager/flake.nix sed -i "s/homeConfigurations = {/homeConfigurations = { $flake_config/g" "$flake_file"
# Validate flake.nix with nixfmt # Validate flake.nix
nix-shell -p nixfmt --run "nixfmt $HOME/dotfiles/config/home-manager/flake.nix" nix-shell -p nixfmt --run "nixfmt $flake_file" || return 1
log_success "Home Manager Flake configuration added successfully."
if [ $? -ne 0 ]; then
tput setaf 1
echo "Something went wrong adding the flake configuration for Home Manager."
echo "Failed to validate flake.nix. Exiting..."
tput sgr0
exit 1
fi
tput setaf 2
echo "Home Manager Flake configuration added successfully."
tput sgr0
# Add to git
git add $HOME/dotfiles/config/nixos/hardware/$hostname.nix $HOME/dotfiles/config/nixos/flake.nix $HOME/dotfiles/config/home-manager/flake.nix
tput setaf 3
echo ""
echo "Don't forget to commit and push the changes to the dotfiles repo after testing."
tput sgr0
git status
echo ""
} }
install_nix() { install_nix() {
if [ -x "$(command -v nixos-version)" ]; then if command -v nixos-version >/dev/null 2>&1; then
tput setaf 2 log_success "Detected NixOS, skipping Nix setup."
echo "Detected NixOS, skipping Nix setup." return 0
tput sgr0 fi
return
else
tput setaf 3
echo "NixOS not detected, installing Nix..."
tput sgr0
sh <(curl -L https://nixos.org/nix/install) --daemon
if [ $? -ne 0 ]; then log_info "NixOS not detected, installing Nix..."
tput setaf 1 if ! sh <(curl -L https://nixos.org/nix/install) --daemon; then
echo "Failed to install Nix. Exiting..." die "Failed to install Nix"
tput sgr0
exit 1
fi
fi fi
} }
clear_files() { setup_symlinks() {
tput setaf 3 log_info "Setting up symlinks..."
echo "Setting up symlinks..."
tput sgr0
# Link .bashrc # Backup and create symlinks
if [ -f $HOME/.bashrc ]; then backup_file "$HOME/.bashrc"
echo "Backing up $HOME/.bashrc to $HOME/.bashrc.bak..." backup_file "$HOME/.profile"
mv $HOME/.bashrc $HOME/.bashrc.bak
if [ -d "$HOME/.config/home-manager" ]; then
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 fi
# Link proper home-manager configs log_info "Linking ~/.config/home-manager to $DOTFILES_DIR/config/home-manager..."
if [ -d ~/.config/home-manager ]; then ln -s "$DOTFILES_DIR/config/home-manager" "$HOME/.config/home-manager" || \
echo "Backing up ~/.config/home-manager to ~/.config/home-manager.bak..." die "Failed to create home-manager symlink"
mv ~/.config/home-manager ~/.config/home-manager.bak
fi
echo "Linking ~/.config/home-manager to $HOME/dotfiles/config/home-manager..."
ln -s $HOME/dotfiles/config/home-manager ~/.config/home-manager
# Link proper nixos configs if [ -d "/etc/nixos" ]; then
if [ -d /etc/nixos ]; then backup_file "/etc/nixos/configuration.nix"
echo "Backing up /etc/nixos/configuration.nix to /etc/nixos/configuration.nix.bak..."
sudo mv /etc/nixos/configuration.nix /etc/nixos/configuration.nix.bak
fi fi
echo "Linking /etc/nixos/configuration.nix to $HOME/dotfiles/config/nixos/configuration.nix..."
sudo ln -s $HOME/dotfiles/config/nixos/configuration.nix /etc/nixos/configuration.nix
# Confirm paths are now proper symlinks log_info "Linking /etc/nixos/configuration.nix to $DOTFILES_DIR/config/nixos/configuration.nix..."
if [ -L ~/.config/home-manager ] && [ -L /etc/nixos/configuration.nix ]; then sudo ln -s "$DOTFILES_DIR/config/nixos/configuration.nix" "/etc/nixos/configuration.nix" || \
# Confirm .bashrc and .profile are no longer present to prevent conflicts in nixoos-rebuild/home-manager switch die "Failed to create nixos configuration symlink"
if [ ! -f $HOME/.bashrc ] && [ ! -f $HOME/.profile ]; then
tput setaf 2 # Verify symlinks
echo "Symlinks set up successfully." confirm_symlink "$HOME/.config/home-manager" "Failed to set up home-manager symlink"
tput sgr0 confirm_symlink "/etc/nixos/configuration.nix" "Failed to set up nixos configuration symlink"
else
tput setaf 1 log_success "Symlinks set up successfully."
echo "Failed to set up symlinks. Exiting..."
tput sgr0
exit 1
fi
else
tput setaf 1
echo "Failed to set up symlinks. Exiting..."
tput sgr0
exit 1
fi
} }
install_home_manager() { install_home_manager() {
if [ -x "$(command -v home-manager)" ]; then if command -v home-manager >/dev/null 2>&1; then
tput setaf 2 log_success "Home Manager already installed. Skipping..."
echo "Home Manager already installed. Skipping..." return 0
tput sgr0
return
fi fi
tput setaf 3 log_info "Installing Home Manager..."
echo "Installing Home Manager..."
tput sgr0 sudo 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"
sudo nix-channel --add https://github.com/nix-community/home-manager/archive/release-$NIXOS_RELEASE.tar.gz home-manager
sudo nix-channel --update sudo nix-channel --update || die "Failed to update channels"
sudo nix-shell '<home-manager>' -A install
nix-shell '<home-manager>' -A install sudo nix-shell '<home-manager>' -A install || die "Failed to install home-manager (sudo)"
nix-shell '<home-manager>' -A install || die "Failed to install home-manager"
if [ $? -ne 0 ]; then
tput setaf 1
echo "Failed to install Home Manager. Exiting..."
tput sgr0
exit 1
fi
} }
prepare_hostname() { prepare_hostname() {
# Check if $HOME/.hostname exists, if skip hostname setup local hostname_file="$HOME/.hostname"
if [ -f $HOME/.hostname ]; then local hostname
hostname=$(cat $HOME/.hostname)
tput setaf 2
echo "Hostname already found in $HOME/.hostname. Using $hostname."
tput sgr0
# Check if config/nixos/hardware/ contains config/nixos/hardware/$hostname.nix if [ -f "$hostname_file" ]; then
if [ ! -f $HOME/dotfiles/config/nixos/hardware/$hostname.nix ]; then hostname=$(cat "$hostname_file")
echo "No hardware configuration found for $hostname. Please create a hardware configuration for this machine." log_success "Hostname already found in $hostname_file. Using $hostname."
exit 1
if [ ! -f "$DOTFILES_DIR/config/nixos/hardware/$hostname.nix" ]; then
die "No hardware configuration found for $hostname. Please create a hardware configuration for this machine."
fi fi
tput setaf 2 log_success "Hardware configuration found for $hostname. Continuing setup..."
echo "Hardware configuration found for $hostname. Continuing setup..."
tput sgr0
return return
fi fi
# Ask the user what hostname this machine should have while true; do
tput setaf 3 log_info "Enter the hostname for this machine:"
echo "Enter the hostname for this machine:" read -r hostname
tput sgr0 if validate_hostname "$hostname"; then
read hostname break
fi
# Validate hostname to ensure it's not empty, contains only alphanumeric characters, and is less than 64 characters log_error "Invalid hostname. Please enter a valid hostname:"
while [[ -z $hostname || ! $hostname =~ ^[a-zA-Z0-9_-]+$ || ${#hostname} -gt 64 ]]; do
echo "Invalid hostname. Please enter a valid hostname:"
read hostname
done done
# Check if config/nixos/hardware/ contains config/nixos/hardware/$hostname.nix if [ ! -f "$DOTFILES_DIR/config/nixos/hardware/$hostname.nix" ]; then
if [ ! -f $HOME/dotfiles/config/nixos/hardware/$hostname.nix ]; then log_info "No hardware configuration found for $hostname."
echo "No hardware configuration found for $hostname." create_hardware_config "$hostname"
create_hardware_config $hostname
return
fi
tput setaf 2
echo "Hardware configuration found for $hostname. Continuing setup..."
tput sgr0
# Set the hostname by dumping it into $HOME/.hostname
touch $HOME/.hostname
echo $hostname > $HOME/.hostname
# Confirm we saved the hostname to $HOME/.hostname
if [ -f $HOME/.hostname ] && [ $(cat $HOME/.hostname) == $hostname ]; then
tput setaf 2
echo "Hostname set successfully."
tput sgr0
else else
tput setaf 1 log_success "Hardware configuration found for $hostname. Continuing setup..."
echo "Failed to set hostname. Exiting..."
tput sgr0
exit 1
fi fi
echo "$hostname" > "$hostname_file" || die "Failed to save hostname"
log_success "Hostname set successfully."
} }
prepare_hostname main() {
clear_files # Check if setup has already been run
install_nix if [ -f "$SETUP_MARKER" ]; then
install_home_manager log_info "Setup has already been run, exiting..."
exit 0
fi
# Rebuild NixOS # Check prerequisites
cd $HOME/dotfiles/config/nixos && sudo nixos-rebuild switch --flake .#$hostname --impure check_prerequisites
if [ $? -ne 0 ]; then
tput setaf 1
echo "Failed to rebuild NixOS. Exiting..."
tput sgr0
exit 1
fi
# Rebuild Home Manager # Clone dotfiles if needed
cd $HOME/dotfiles/config/home-manager && NIXPKGS_ALLOW_UNFREE=1 home-manager switch --flake .#$hostname --impure if [ ! -d "$DOTFILES_DIR" ]; then
if [ $? -ne 0 ]; then log_info "Cloning dotfiles repo..."
tput setaf 1 git clone "$GIT_REPO" "$DOTFILES_DIR" || die "Failed to clone dotfiles repository"
echo "Failed to rebuild Home Manager. Exiting..." fi
tput sgr0
exit 1
fi
# Make .profile a symlink to .bashrc # Run setup steps
if [ -f $HOME/.profile ]; then prepare_hostname
echo "Backup up $HOME/.profile to $HOME/.profile.bak..." setup_symlinks
mv $HOME/.profile $HOME/.profile.bak install_nix
fi install_home_manager
tput setaf 2 # Get hostname
echo local hostname
echo "Setup complete. Please logout / restart to continue with 'dotf update'." hostname=$(cat "$HOME/.hostname") || die "Failed to read hostname"
echo
tput sgr0
touch $HOME/.dotfiles-setup # Rebuild NixOS
cd "$DOTFILES_DIR/config/nixos" || die "Failed to change to nixos config directory"
sudo nixos-rebuild switch --flake ".#$hostname" --impure || \
die "Failed to rebuild NixOS"
tput setaf 1 # Rebuild Home Manager
echo cd "$DOTFILES_DIR/config/home-manager" || die "Failed to change to home-manager config directory"
echo "!!! Please logout / restart to continue !!!" NIXPKGS_ALLOW_UNFREE=1 home-manager switch --flake ".#$hostname" --impure || \
echo "~~~ Proceed by running 'dotf update' ~~~" die "Failed to rebuild Home Manager"
echo
tput sgr0 # Create setup marker
touch "$SETUP_MARKER" || die "Failed to create setup marker"
# Final success message
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 "$@"