-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.zsh
executable file
·108 lines (87 loc) · 3.11 KB
/
bootstrap.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env zsh
# Exit immediately if a command fails and treat unset vars as error
set -eu
# Immediately invoked anonymous function with the script's path as its only argument
# used to contain variables and functions in a local scope
function {
local __dotfiles_scripts_dir="$(dirname "$1")"
local __dotfiles_dir="$(dirname "$__dotfiles_scripts_dir")"
local __flake_system="personal"
function __log_info() { print -P "%F{blue} %f $1" }
function __log_ok() { print -P "%F{green} %f $1" }
function __log_error() { print -P "%F{red} %f $1" }
# Install nix
if [[ $(command -v nix) == "" ]]; then
__log_info "Nix not installed. Attempting install..."
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm
if [[ $? == 0 ]]; then
__log_ok "Nix installed, starting service..."
# Start the nix daemon without restarting the shell
source /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
fi
else
__log_ok "Nix already installed"
fi
# Install nix-darwin
if [[ $(command -v nix) != "" && $(command -v darwin-rebuild) == "" ]]; then
__log_info "nix-darwin not installed, installing..."
nix run nix-darwin -- switch --flake "$__dotfiles_dir#$__flake_system"
if [[ $? == 0 ]]; then
__log_ok "nix-darwin installed"
fi
else
__log_ok "nix-darwin already installed"
fi
# Apply nix-darwin configuration
if [[ $(command -v darwin-rebuild) != "" ]]; then
__log_info "Applying nix-darwin changes..."
darwin-rebuild switch --flake "$__dotfiles_dir#$__flake_system"
if [[ $? == 0 ]]; then
__log_ok "nix-darwin changes applied"
fi
fi
# Symlink dotfiles
if [[ $(command -v stow) != "" ]]; then
__log_info "GNU Stow found, symlinking dotfiles"
# CD to dotfiles dir and then back when done
local __from_dir="$PWD"
if [[ $__from_dir != $__dotfiles_dir ]]; then
echo "- CWD: $PWD"
echo "- Switching to $__dotfiles_dir"
cd "$__dotfiles_dir"
echo "- CWD: $PWD"
fi
stow -vR .
if [[ $PWD != $__from_dir ]]; then
echo "- CWD: $PWD"
echo "- Switching back to $__from_dir"
cd "$__from_dir"
echo "- CWD: $PWD"
fi
else
__log_error "GNU Stow not found"
exit 1
fi
# Update Yazi packages
if [[ $(command -v ya) != "" ]]; then
__log_info "Updating Yazi packages..."
ya pack -u
fi
# TODO: Reactivate?
# Update NPM packages
# if [[ $(command -v npm) != "" ]]; then
# echo "\n- Install + update NPM packages..."
# npm install -g npm@latest neovim
# fi
# From https://github.com/ohmyzsh/ohmyzsh/blob/d82669199b5d900b50fd06dd3518c277f0def869/lib/cli.zsh#L668-L676
function __reload {
# Delete current completion cache
(command rm -f $_comp_dumpfile $ZSH_COMPDUMP) 2> /dev/null
# Old zsh versions don't have ZSH_ARGZERO
local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
# Check whether to run a login shell
[[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
}
__log_ok "Update complete, reloading shell..."
__reload
} $(realpath $0)