Add Ansible configuration and remove NixOS
All checks were successful
Nix Format Check / check-format (pull_request) Successful in 39s

This commit is contained in:
2025-01-20 09:18:58 +01:00
parent 4e28e5cbb0
commit e1aa41d1d3
107 changed files with 368 additions and 2902 deletions

View File

@ -1,229 +1,80 @@
#!/usr/bin/env bash
source $HOME/dotfiles/bin/helpers/functions.sh
# check if --verbose was passed
if [ "$2" = "--verbose" ]; then
export verbose=true
printfe "%s\n" "yellow" "Verbose mode enabled"
help() {
printfe "%s\n" "green" "Usage: upgrade.sh [options]"
printfe "%s\n" "green" "Options:"
printfe "%s\n" "green" " --ha, -H Upgrade Home Manager packages."
printfe "%s\n" "green" " --nix, -X Upgrade NixOS packages."
printfe "%s\n" "green" " --full-speed, -F Upgrade packages and use all available cores for compilation. (Default: 8 cores)"
printfe "%s\n" "green" " --help, -h Display this help message."
exit 0
}
while [[ "$#" -gt 0 ]]; do
case $1 in
--ha|-H) RUN_HA=true ;;
--nix|-X) RUN_NIX=true ;;
--ansible|-A) RUN_ANSIBLE=true ;;
--full-speed|-F) FULL_SPEED=true ;;
--help|-h) help ;;
*) echo "Unknown parameter passed: $1";
help ;;
esac
shift
done
if [[ -z "$RUN_HA" && -z "$RUN_NIX" && -z "$RUN_ANSIBLE" ]]; then
RUN_HA=true
RUN_NIX=true
RUN_ANSIBLE=true
fi
# Check if --full-speed flag is passed, otherwise use --cores 8 -j 1
if [[ "$FULL_SPEED" == true ]]; then
CORES=$(nproc)
JOBS=$(nproc)
else
export verbose=false
CORES=8
JOBS=1
fi
# Check if we have shyaml since that's required for the script to function
if [ ! -x "$(command -v shyaml)" ]; then
printfe "%s\n" "red" "shyaml is not installed, installing it..."
pipx install shyaml
fi
printfe "%s\n" "cyan" "Limiting to $CORES cores with $JOBS jobs."
ensure_symlink() {
local source
local target
# Fetch target from YAML
target=$(shyaml get-value "config.symlinks.$1.target" < "$HOME/dotfiles/config/config.yaml" 2>/dev/null)
# Fetch source from YAML based on OS
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Check for WSL2
if [[ $(uname -a) == *"microsoft-standard-WSL2"* ]]; then
source=$(shyaml get-value "config.symlinks.$1.sources.wsl" < "$HOME/dotfiles/config/config.yaml" 2>/dev/null)
else
source=$(shyaml get-value "config.symlinks.$1.sources.linux" < "$HOME/dotfiles/config/config.yaml" 2>/dev/null)
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
source=$(shyaml get-value "config.symlinks.$1.sources.macos" < "$HOME/dotfiles/config/config.yaml" 2>/dev/null)
fi
# Fall back to generic source if OS-specific source is empty
if [ -z "$source" ]; then
source=$(shyaml get-value "config.symlinks.$1.source" < "$HOME/dotfiles/config/config.yaml" 2>/dev/null)
fi
# Attempt to use the hostname of the machine if source is still empty
if [ -z "$source" ]; then
source=$(shyaml get-value "config.symlinks.$1.sources.$(hostname)" < "$HOME/dotfiles/config/config.yaml" 2>/dev/null)
fi
# Error out if source is still empty
if [ -z "$source" ]; then
printfe "%s\n" "red" " - No valid source defined for $1"
return
fi
# Expand ~ with $HOME
source="${source/#\~/$HOME}"
target="${target/#\~/$HOME}"
# Call the function to check or make the symlink
check_or_make_symlink "$source" "$target"
# Check if there is a chmod defined for the target file
desired_chmod=$(shyaml get-value "config.symlinks.$1.chmod" < "$HOME/dotfiles/config/config.yaml" 2>/dev/null)
if [ -n "$desired_chmod" ]; then
# Resolve the target if it is a symlink
resolved_target=$(readlink -f "$target")
# If readlink fails, fall back to the original target
if [ -z "$resolved_target" ]; then
resolved_target="$target"
fi
current_chmod=$(stat -c %a "$resolved_target" 2>/dev/null)
if [ "$current_chmod" != "$desired_chmod" ]; then
printfe "%s\n" "yellow" " - Changing chmod of $resolved_target to $desired_chmod"
chmod "$desired_chmod" "$resolved_target"
fi
fi
}
symlinks() {
####################################################################################################
# Update symlinks
####################################################################################################
# Load symlinks from config file
symlinks=($(cat $HOME/dotfiles/config/config.yaml | shyaml keys config.symlinks))
printfe "%s\n" "cyan" "Updating symlinks..."
for symlink in "${symlinks[@]}"; do
ensure_symlink $symlink
done
}
####################################################################################################
# Update packages
####################################################################################################
sys_packages() {
if [[ "$OSTYPE" == "darwin"* ]]; then
printfe "%s\n" "cyan" "Updating brew packages..."
brew update
brew upgrade
brew cleanup
else
if [ -x "$(command -v nixos-version)" ]; then
printfe "%s\n" "cyan" "Updating nix channels..."
printfe "%s" "cyan" "System channels: "
sudo -i nix-channel --update
printfe "%s" "cyan" "User channels: "
nix-channel --update
printfe "%s\n" "cyan" "Updating nixos flake..."
cd $HOME/dotfiles/config/nixos && nix --extra-experimental-features nix-command --extra-experimental-features flakes flake update
# Exit if this failed
if [ $? -ne 0 ]; then
exit $?
fi
return
fi
if [ -x "$(command -v apt)" ]; then
printfe "%s\n" "cyan" "Updating apt packages..."
sudo nala upgrade -y
sudo nala autoremove -y --purge
fi
if [ -x "$(command -v yum)" ]; then
printfe "%s\n" "cyan" "Updating yum packages..."
sudo yum update -y
fi
fi
}
cargopkgs() {
printfe "%s\n" "cyan" "Ensuring Cargo packages are installed..."
source $HOME/dotfiles/bin/helpers/cargo_packages.sh
ensure_cargo_packages_installed
}
pipxpkgs() {
if [ ! -x "$(command -v pipx)" ]; then
printfe "%s\n" "yellow" "pipx is not available, skipping pipx packages."
return
fi
printfe "%s\n" "cyan" "Ensuring pipx packages are installed..."
source $HOME/dotfiles/bin/helpers/pipx_packages.sh
ensure_pipx_packages_installed
}
flatpakpkgs() {
if [ ! -x "$(command -v flatpak)" ]; then
printfe "%s\n" "yellow" "Flatpak is not available, skipping Flatpak."
return
fi
if is_wsl; then
printfe "%s\n" "yellow" "Running in WSL, skipping Flatpak."
return
fi
printfe "%s\n" "cyan" "Ensuring Flatpak packages are installed..."
source $HOME/dotfiles/bin/helpers/flatpak_packages.sh
ensure_flatpak_packages_installed
}
homemanager() {
if [[ "$RUN_NIX" == true ]]; then
printfe "%s\n" "cyan" "Updating Home Manager flake..."
cd $HOME/dotfiles/config/home-manager && nix --extra-experimental-features nix-command --extra-experimental-features flakes flake update
}
####################################################################################################
# Parse arguments
####################################################################################################
if [ "$#" -eq 0 ]; then
printfe "%s\n" "yellow" "No options passed, running full update..."
symlinks
sys_packages
homemanager
cargopkgs
pipxpkgs
flatpakpkgs
dotf secrets encrypt
else
for arg in "$@"; do
case $arg in
--nixos|nixos|nix|nixos-rebuild)
sys_packages
;;
--home-manager|--homemanager|ha|hm|home)
homemanager
;;
--nix)
sys_packages
homemanager
;;
--symlinks)
symlinks
;;
--packages)
sys_packages
cargopkgs
pipxpkgs
flatpakpkgs
;;
--pipx)
pipxpkgs
;;
--cargo)
cargopkgs
;;
--flatpak)
flatpakpkgs
;;
*)
printfe "%s\n" "red" "Unknown option: $arg"
;;
esac
done
fi
echo ""
printfe "%s\n" "blue" "Done!"
if [[ "$RUN_HA" == true ]]; then
if command -v home-manager &> /dev/null; then
printfe "%s\n" "cyan" "Cleaning old backup files..."
rm -rf $HOME/.config/mimeapps.list.backup
printfe "%s\n" "cyan" "Upgrading Home Manager packages..."
cd $HOME/dotfiles/config/home-manager && NIXPKGS_ALLOW_UNFREE=1 home-manager --extra-experimental-features nix-command --extra-experimental-features flakes switch -b backup --flake .#$DOTF_HOSTNAME --impure --cores $CORES -j $JOBS
if [[ $? -ne 0 ]]; then
printfe "%s\n" "red" "Failed to upgrade Home Manager packages."
exit 1
fi
else
printfe "%s\n" "red" "Home Manager is not installed."
exit 1
fi
fi
if [[ "$RUN_ANSIBLE" == true ]]; then
if command -v ansible-playbook &> /dev/null; then
printfe "%s\n" "cyan" "Upgrading Ansible packages..."
cd $HOME/dotfiles/config/ansible && ansible-playbook -i $HOME/dotfiles/config/ansible/inventory.ini $HOME/dotfiles/config/ansible/main.yml --extra-vars "hostname=$DOTF_HOSTNAME" --limit $DOTF_HOSTNAME
if [[ $? -ne 0 ]]; then
printfe "%s\n" "red" "Failed to upgrade Ansible packages."
exit 1
fi
else
printfe "%s\n" "red" "Ansible is not installed."
exit 1
fi
fi

View File

@ -1,72 +0,0 @@
#!/usr/bin/env bash
source $HOME/dotfiles/bin/helpers/functions.sh
help() {
printfe "%s\n" "green" "Usage: upgrade.sh [options]"
printfe "%s\n" "green" "Options:"
printfe "%s\n" "green" " --ha, -H Upgrade Home Manager packages."
printfe "%s\n" "green" " --nix, -X Upgrade NixOS packages."
printfe "%s\n" "green" " --full-speed, -F Upgrade packages and use all available cores for compilation. (Default: 8 cores)"
printfe "%s\n" "green" " --help, -h Display this help message."
exit 0
}
while [[ "$#" -gt 0 ]]; do
case $1 in
--ha|-H) RUN_HA=true ;;
--nix|-X) RUN_NIX=true ;;
--full-speed|-F) FULL_SPEED=true ;;
--help|-h) help ;;
*) echo "Unknown parameter passed: $1";
help ;;
esac
shift
done
if [[ -z "$RUN_HA" && -z "$RUN_NIX" ]]; then
RUN_HA=true
RUN_NIX=true
fi
# Check if --full-speed flag is passed, otherwise use --cores 8 -j 1
if [[ "$FULL_SPEED" == true ]]; then
CORES=$(nproc)
JOBS=$(nproc)
else
CORES=8
JOBS=1
fi
printfe "%s\n" "cyan" "Limiting to $CORES cores with $JOBS jobs."
if [[ "$RUN_NIX" == true ]]; then
if command -v nixos-rebuild &> /dev/null; then
printfe "%s\n" "cyan" "Upgrading NixOS packages..."
cd $HOME/dotfiles/config/nixos && sudo nixos-rebuild switch --upgrade --flake .#$DOTF_HOSTNAME --impure --cores $CORES -j $JOBS
if [[ $? -ne 0 ]]; then
printfe "%s\n" "red" "Failed to upgrade NixOS packages."
exit 1
fi
else
printfe "%s\n" "red" "Skipping nixos-rebuild, NixOS is not installed."
fi
fi
if [[ "$RUN_HA" == true ]]; then
if command -v home-manager &> /dev/null; then
printfe "%s\n" "cyan" "Cleaning old backup files..."
rm -rf $HOME/.config/mimeapps.list.backup
printfe "%s\n" "cyan" "Upgrading Home Manager packages..."
cd $HOME/dotfiles/config/home-manager && NIXPKGS_ALLOW_UNFREE=1 home-manager --extra-experimental-features nix-command --extra-experimental-features flakes switch -b backup --flake .#$DOTF_HOSTNAME --impure --cores $CORES -j $JOBS
if [[ $? -ne 0 ]]; then
printfe "%s\n" "red" "Failed to upgrade Home Manager packages."
exit 1
fi
else
printfe "%s\n" "red" "Home Manager is not installed."
exit 1
fi
fi

View File

@ -7,7 +7,6 @@ IFS=$'\n\t'
# Script constants
readonly DOTFILES_ROOT="$HOME/dotfiles"
readonly DOTFILES_BIN="$DOTFILES_ROOT/bin"
readonly DOTFILES_CONFIG="$DOTFILES_ROOT/config/config.yaml"
# Source helper functions
if [[ ! -f "$DOTFILES_BIN/helpers/functions.sh" ]]; then
@ -16,8 +15,6 @@ if [[ ! -f "$DOTFILES_BIN/helpers/functions.sh" ]]; then
fi
source "$DOTFILES_BIN/helpers/functions.sh"
export DOTFILES_CONFIG
# Command functions
update() {
local update_script="$DOTFILES_BIN/actions/update.sh"
@ -28,15 +25,6 @@ update() {
"$update_script" $@
}
upgrade() {
local upgrade_script="$DOTFILES_BIN/actions/upgrade.sh"
if [[ ! -x "$upgrade_script" ]]; then
printfe "%s\n" "red" "Error: Upgrade script not found or not executable"
return 1
fi
"$upgrade_script" $@
}
hello() {
local term_script="$DOTFILES_BIN/actions/hello.sh"
if [[ ! -x "$term_script" ]]; then
@ -123,7 +111,6 @@ main() {
# Parse commands
case "${1:-help}" in
update) shift; update "$@" ;;
upgrade) shift; upgrade "$@" ;;
help) shift; help "$@" ;;
hello) shift; hello "$@" ;;
secrets) shift; secrets "$@" ;;

View File

@ -1,72 +0,0 @@
#!/usr/bin/env bash
source $HOME/dotfiles/bin/helpers/functions.sh
ensure_cargo_packages_installed() {
cargo_packages=($(cat $DOTFILES_CONFIG | shyaml keys config.packages.cargo))
for package in "${cargo_packages[@]}"; do
printfe "%s" "cyan" " - Checking $package..."
echo -en '\r'
# Some entries have a git_url and binary, we need to load these in if they exist
pkg_status=$(cargo install --list | grep -E "^${package}\sv[0-9.]+:$")
package_url=$(cat $DOTFILES_CONFIG | shyaml get-value config.packages.cargo.$package.git_url 2>/dev/null)
binary=$(cat $DOTFILES_CONFIG | shyaml get-value config.packages.cargo.$package.binary 2>/dev/null)
# If pkg_status is `installed` then we don't need to install the package, otherwise if it's empty then the package is not installed
if [ -z "$pkg_status" ]; then
ensure_sudo_privileges "In order to install $package, please provide your password:"
printfe "%s" "yellow" " - Compiling/Installing $package... (This may take a while)"
clear_line
# If package_url is defined we should install via git
if [ -n "$package_url" ]; then
command="cargo install --git $package_url $binary"
else
command="cargo install $package"
fi
# Execute the command
result=$(eval $command 2>&1)
if [ $? -ne 0 ]; then
printfe "%s\n" "red" " - Failed to install $package"
printfe "%s\n" "red" " Command: $command"
printfe "%s\n" "red" " Output: $result"
exit 1
fi
printfe "%s\n" "green" " - Installed $package"
else
printfe "%s\n" "green" " - $package is already installed"
fi
done
}
print_cargo_status() {
printfe "%s" "cyan" "Checking Cargo packages..."
clear_line
cargo_packages=($(cat $DOTFILES_CONFIG | shyaml keys config.packages.cargo))
count=$(echo $cargo_packages | wc -w)
installed=0
for package in "${cargo_packages[@]}"; do
pkg_status=$(cargo install --list | grep -E "^${package}\sv[0-9.]+:$")
if [ -z $pkg_status ]; then
if [ "$verbose" = true ]; then
printfe "%s\n" "red" "$package is not installed"
fi
else
installed=$((installed + 1))
fi
done
printfe "%s" "cyan" "Cargo"
if [ $installed -eq $count ]; then
printfe "%s" "green" " $installed/$count "
else
printfe "%s" "red" " $installed/$count "
fi
printfe "%s\n" "cyan" "packages installed"
}

View File

@ -1,55 +0,0 @@
#!/usr/bin/env bash
source $HOME/dotfiles/bin/helpers/functions.sh
ensure_pipx_packages_installed() {
pipx_packages=($(cat $DOTFILES_CONFIG | shyaml get-values config.packages.pipx))
for i in "${pipx_packages[@]}";
do
printfe "%s" "cyan" " - Fetching package details for $i"
echo -en '\r'
if pipx list | grep --quiet ${i}; then
printfe "%s\n" "green" " - $i is already installed."
continue
fi
printfe "%s" "cyan" " - Installing $i..."
echo -en '\r'
pipx install $i
if [ $? -ne 0 ]; then
printfe "%s\n" "red" " - Failed to install $i"
continue
fi
printfe "%s\n" "green" " - $i installed."
done
}
print_pipx_status() {
printfe "%s" "cyan" "Checking pipx packages..."
clear_line
pipx_packages=($(cat $DOTFILES_CONFIG | shyaml get-values config.packages.pipx))
count=$(echo $pipx_packages | wc -w)
installed=0
for package in "${pipx_packages[@]}"; do
if pipx list | grep -q $package; then
installed=$((installed + 1))
else
if [ "$verbose" = true ]; then
printfe "%s\n" "red" "$package is not installed"
fi
fi
done
printfe "%s" "cyan" "pipx"
if [ $installed -eq $count ]; then
printfe "%s" "green" " $installed/$count "
else
printfe "%s" "red" " $installed/$count "
fi
printfe "%s\n" "cyan" "packages installed"
}

View File

@ -1,9 +1,7 @@
Usage: dotf [OPTIONS] [ARGS]
update: Pull latest changes, and update symlinks and configurations
Also pulls latest nix channels and updates flakes to latest versions.
upgrade: Runs switch, flake variants for nix switch with upgrade and home-manager.
update: Update everything in the dotfiles repository.
secrets: Encrypt and decrypt secrets.
auto-start: Start a set of pre-defined applications.
hello: Shows the welcome message for the terminal.