dotfiles/setup.sh
2024-02-19 13:46:45 +01:00

114 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
DOTFILES_REPO="git@github.com:vleeuwenmenno/dotfiles.git"
help() {
echo "Usage: $0 [option...]" >&2
echo
echo " -i, --install Run installation process"
echo " -h, --help Display this help message"
echo
exit 1
}
install_nix() {
# Check if nix is installed (nix command is available)
if command -v nix > /dev/null; then
echo 'NIX is already installed.'
else
echo 'Installing NIX'
sh <(curl -L https://nixos.org/nix/install) --daemon
echo 'Please restart your shell to continue... (Then run `./setup.sh -c`)'
touch ~/.setup-initial-done
fi
}
hush_login() {
# Check if distro is Ubuntu
if [ -f /etc/os-release ]; then
. /etc/os-release
if [ "$ID" != "ubuntu" ]; then
return 1
fi
else
return 1
fi
# If .hushlogin does not exist, create it
if [ -f ~/.hushlogin ]; then
echo 'Login message is already disabled.'
else
echo 'Disabling login message.'
touch ~/.hushlogin
fi
}
nix_experimental() {
if command -v nix > /dev/null; then
echo 'NIX is already installed.'
else
install_nix
fi
echo "Adding experimental-features to nix.conf..."
grep -qxF 'experimental-features = nix-command flakes' /etc/nix/nix.conf || echo "experimental-features = nix-command flakes" | sudo tee -a /etc/nix/nix.conf
}
nix_config_import() {
# Check if nix configuration is already inserted
if [ -f ~/.config/nix/nix.conf ]; then
echo "Nix configuration is already inserted."
exit 1
else
echo "Inserting nix configuration..."
mkdir -p ~/.config/nix
curl -s $NIX_CONFIG_URL -o ~/.config/nix/nix.conf
fi
}
clone_dotfiles() {
if [ -d ~/.dotfiles ]; then
echo "Dotfiles already cloned."
else
echo "Cloning dotfiles..."
git clone $DOTFILES_REPO ~/.dotfiles
fi
}
home_manager_setup() {
# Check if home-manager is installed
if command -v home-manager > /dev/null; then
echo "Home-manager is already installed."
else
echo "Installing home-manager..."
nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager
nix-channel --update
nix-shell '<home-manager>' -A install
fi
}
# Check if parameter is help, continue or initial
if [ "$1" == "-i" ] || [ "$1" == "--install" ]; then
# Ubuntu specific, hide login message
hush_login
# Install nix
install_nix
# Add experimental-features to nix.conf
nix_experimental
# Clone dotfiles
clone_dotfiles
# Run initial home-manager setup
home_manager_setup
# Run initial home-manager switch
home-manager switch --flake ~/.dotfiles
else
help
fi