-
Notifications
You must be signed in to change notification settings - Fork 17
/
install
252 lines (212 loc) · 6.46 KB
/
install
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env bash
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -Eueo pipefail
SUDO_USER=$(whoami)
# Intell by default because my personal machine
ARCH="i386"
if [[ $(arch) == 'arm64' ]]; then
ARCH="$(arch)"
fi
# =======================================================================
# = Helpers & setting some variables
# =======================================================================
DOTFILES_DIR="$HOME/.dotfiles"
tput sgr0
RED=$(tput setaf 1)
ORANGE=$(tput setaf 3)
GREEN=$(tput setaf 2)
PURPLE=$(tput setaf 5)
CYAN=$(tput setaf 4)
BLUE=$(tput setaf 6)
WHITE=$(tput setaf 7)
BOLD=$(tput bold)
RESET=$(tput sgr0)
log() {
local LABEL="[$1]"
local COLOR="$2"
shift
shift
local MSG=("$@")
printf "${COLOR}${LABEL}%*s${RESET}" $(($(tput cols) - ${#LABEL})) | tr ' ' '='
for M in "${MSG[@]}"; do
let COL=$(tput cols)-2-${#M}
printf "%s%${COL}s${RESET}" "$COLOR* $M"
done
printf "${COLOR}%*s${RESET}\n\n\n" $(tput cols) | tr ' ' '='
}
log_error() {
log "FAIL" "$RED" "$@"
exit 1
}
log_info() {
log "INFO" "$ORANGE" "$@"
}
log_success() {
log "OK" "$GREEN" "$@"
}
print_main_banner() {
cd "$HOME"
command cat <<EOF
$GREEN
_ _ __ _ _
| | | | / _(_) |
__| | ___ | |_| |_ _| | ___ ___
/ _. |/ _ \| __| _| | |/ _ \/ __|
| (_| | (_) | |_| | | | | __/\__ \ $()
(_)__,_|\___/ \__|_| |_|_|\___||___/
bootstrapping an $ARCH machine
$RESET
EOF
if [ -d "$DOTFILES_DIR/.git" ]; then
command cat <<EOF
$BLUE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$(git --git-dir "$DOTFILES_DIR/.git" --work-tree "$DOTFILES_DIR" log -n 1 --pretty=format:'%C(yellow)commit: %h')
$(git --git-dir "$DOTFILES_DIR/.git" --work-tree "$DOTFILES_DIR" log -n 1 --pretty=format:'%C(red)date: %ad' --date=short)
$(git --git-dir "$DOTFILES_DIR/.git" --work-tree "$DOTFILES_DIR" log -n 1 --pretty=format:'%C(cyan)author: %an')
$(git --git-dir "$DOTFILES_DIR/.git" --work-tree "$DOTFILES_DIR" log -n 1 --pretty=format:'%C(green)message: %s')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$RESET
EOF
fi
}
print_prompt() {
echo "What do you want to do?"
PS3="Enter your choice (must be a number): "
options=("All" "Install package manager" "Clone dotfiles" "Backup old files" "Symlink files" "Install Apps" "Change shell" "Quit")
select opt in "${options[@]}"; do
case $opt in
"All")
all
break
;;
"Install package manager")
package_manager
break
;;
"Clone dotfiles")
clone_dotfiles
break
;;
"Install")
install
break
;;
"Quit")
break
;;
*)
echo "Invalid option"
break
;;
esac
done
}
# =======================================================================
# = Main functions
# =======================================================================
install_xcode() {
if command -v xcode-select >/dev/null; then
log_info "Xcode already installed"
else
log_success "Installing Xcode"
xcode-select --install
if [[ "$ARCH" == 'arm64' ]]; then
log_success "Installing Rosetta"
sudo -u "$SUDO_USER" softwareupdate --install-rosetta --agree-to-license
fi
fi
}
install_nix() {
if command -v nix >/dev/null; then
log_info "Nix already installed"
else
log_success "Installing Nix"
true | sh <(curl -L https://nixos.org/nix/install)
fi
}
package_manager() {
if [ "$(uname)" == "Darwin" ]; then
install_xcode
install_nix
fi
}
decrypt_repo() {
cd "$DOTFILES_DIR"
echo "decrypting dotfiles..."
echo -n "transcrypt cipher: "
read -r cipher
echo -n "transcrypt password: "
read -sr password
echo
transcrypt -c "$cipher" -p "$password"
}
clone_dotfiles() {
if [[ ! -d $DOTFILES_DIR ]]; then
log_success "Cloning dotfiles..."
git clone --recursive https://github.com/ahmedelgabri/dotfiles.git "$DOTFILES_DIR"
# Setup repo origin & mirrors
# This is stupid, because we don't have SSH keys at this point
# cd "$DOTFILES_DIR" &&
# git remote set-url origin [email protected]:ahmedelgabri/dotfiles.git &&
# git remote add --mirror=push bitbucket [email protected]:ahmedelgabri/dotfiles.git &&
# git remote add --mirror=push gitlab [email protected]:ahmedelgabri/dotfiles.git
decrypt_repo
else
cd "$DOTFILES_DIR"
git stash -u || true
git checkout main
git reset --hard origin/main
git submodule update --init --recursive
git checkout -
git stash pop || true
fi
}
install() {
if command -v nix >/dev/null; then
if [ "$(uname)" == "Darwin" ]; then
log_info "Building flakes..."
cd "$DOTFILES_DIR"
nix --experimental-features 'nix-command flakes' build "./#$(hostname -s)"
# Needed for nix-darwin to work, copied verbatim from the install script, since we build with flakes we have to do this manually
# https://github.com/abathur/nix-darwin/blob/44da835ac40dab5fd231298b59d83487382d2fab/pkgs/darwin-installer/configuration.nix#L45-L51
if ! grep -q '^run\b' /etc/synthetic.conf 2>/dev/null; then
echo "setting up /etc/synthetic.conf..."
echo -e "run\tprivate/var/run" | sudo tee -a /etc/synthetic.conf >/dev/null
/System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -B &>/dev/null ||
/System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -t &>/dev/null ||
echo "warning: failed to execute apfs.util"
fi
# Needed otherwise nix-darwin will complain
sudo -u "$SUDO_USER" mv /etc/shells /etc/shells.old
sudo -u "$SUDO_USER" mv /etc/zshenv /etc/zshenv.old
sudo -u "$SUDO_USER" mv /etc/zshrc /etc/zshrc.old
sudo -u "$SUDO_USER" mv /etc/zprofile /etc/zprofile.old
sudo -u "$SUDO_USER" mv /etc/nix/nix.conf /etc/nix/nix.conf.old
"$DOTFILES_DIR"/result/sw/bin/darwin-rebuild switch --flake "./#$(hostname -s)"
else
sudo -u "$SUDO_USER" nixos-rebuild switch --flake "./#$(hostname -s)"
fi
fi
}
all() {
package_manager
clone_dotfiles
install
FAILED_COMMAND=$(fc -ln -1)
if [ $? -eq 0 ]; then
log_success "Done."
log_info "Don't forget to generate SSH keys & import gpg keys"
# command vim -u NONE $XDG_DATA_HOME/$(hostname)/{gitconfig,zshrc}
else
log_error "Something went wrong, [ Failed on: $FAILED_COMMAND ]"
fi
}
run() {
print_main_banner
print_prompt
}
# =======================================================================
# = Run!
# =======================================================================
run