-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitial_setup.sh
executable file
·243 lines (217 loc) · 5.79 KB
/
initial_setup.sh
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
#! /usr/bin/env bash
set -euo pipefail
export PATH=$PATH:~/bin
OS=$(uname -a)
IS_MAC=
IS_NIX=
if [[ "$OS" == *"NixOS"* ]]; then
IS_NIX=true
fi
if [[ "$OS" == *"Darwin"* ]]; then
IS_MAC=true
fi
YELLOW="\033[1;33m"
RESET="\033[0m"
confirm() {
echo -n "${1:-Continue?} [y/N] "
read -r CONFIRM
if [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "yes" ]; then
return 1
fi
}
set_hostname() {
echo -n "New hostname: "
read -r NEW_HOST
if confirm "Set hostname to $NEW_HOST?"; then
if [[ "$IS_MAC" ]]; then
sudo scutil --set ComputerName "$NEW_HOST"
sudo scutil --set HostName "$NEW_HOST"
elif [[ "$IS_NIX" ]]; then
echo "put in /etc/nixos/configuration.nix: "
echo "networking.hostName = \"$NEW_HOST\";"
echo "sudo nixos-rebuild switch"
echo "sudo reboot"
fi
else
return 1
fi
}
if [[ "sheeley" != $(whoami) ]]; then
if confirm "username is $(whoami), change to sheeley?"; then
sudo useradd -m -G wheel -s /run/current-system/sw/bin/bash
sudo passwd sheeley
sudo su sheeley
cd "$HOME"
fi
fi
EMAIL=""
# generate ssh key
if [ ! -f ~/.ssh/id_ed25519 ]; then
if [ "$EMAIL" == "" ]; then
echo "Enter email to use for ssh key"
read -r EMAIL
fi
echo "At this point, XCode doesn't like ed25519. Maybe generate more than one? Hrm."
set -x
ssh-keygen -t ed25519 -C "$EMAIL"
eval "$(ssh-agent -s)"
if ! ssh-add -l -E sha256 | grep ED25519; then
if [[ "$IS_MAC" ]]; then
ssh-add -K ~/.ssh/id_ed25519
else
ssh-add ~/.ssh/id_ed25519
fi
fi
set +x
else
EMAIL=$(cut -d' ' -f3 <~/.ssh/id_ed25519.pub)
fi
if [[ "$IS_NIX" ]]; then
# git to clone the repo, ripgrep to run ./apply
if ! command -v git &>/dev/null; then
nix-env -iA nixos.git
fi
if ! command -v rg &>/dev/null; then
nix-env -iA nixos.ripgrep
fi
fi
HOST=$(hostname)
if [[ "$IS_MAC" ]]; then
# nix-darwin can't install brew.
if ! command -v brew &>/dev/null; then
echo "brew is required - if you don't have it and don't want it installed this way, install it your way now."
if confirm "Install brew "; then
echo "installing brew"
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
eval "\$(/opt/homebrew/bin/brew shellenv)"
brew install --cask 1password
fi
fi
if ! xcode-select -p; then
echo "installing xcode"
xcode-select --install
confirm "Hit enter when install finished" || exit 1
fi
if ! command -v nix &>/dev/null; then
echo "installing nix"
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
# shellcheck disable=SC1091
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
# create /run
if [ ! -f /run ]; then
echo "creating /run"
set +x
printf 'run\tprivate/var/run\n' | sudo tee -a /etc/synthetic.conf
/System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -t
set -x
fi
fi
if command -v scutil &>/dev/null; then
HOST=$(scutil --get HostName)
fi
if confirm "Change from current hostname: $HOST?"; then
while true; do
set_hostname && break
done
fi
fi
if [ ! -f ~/.gh_done ]; then
# store key in github
echo -e "$YELLOW"
cat ~/.ssh/id_ed25519.pub
echo 'Input SSH Key: https://github.com/settings/keys'
echo -e "$RESET"
if confirm "Copy public key to clipboard and open browser ^?"; then
if command -v pbcopy &>/dev/null; then
pbcopy <~/.ssh/id_ed25519.pub
fi
open https://github.com/settings/keys
fi
touch ~/.gh_done
fi
set +u
if [ "$HOMEBREW_GITHUB_API_TOKEN" == "" ]; then
if [ -f ~/.gh_token ]; then
# store token for repeat runs
HOMEBREW_GITHUB_API_TOKEN=$(cat ~/.gh_token)
fi
if [ "$HOMEBREW_GITHUB_API_TOKEN" == "" ]; then
echo 'Generate a token at https://github.com/settings/tokens/new?scopes=gist,public_repo,workflow&description=Homebrew'
if confirm "open a browser to go to github to create a token for homebrew?"; then
set -x
open 'https://github.com/settings/tokens/new?scopes=gist,public_repo,workflow&description=Homebrew'
set +x
fi
echo "Enter Github token"
read -r HOMEBREW_GITHUB_API_TOKEN
export HOMEBREW_GITHUB_API_TOKEN
echo "$HOMEBREW_GITHUB_API_TOKEN" >~/.gh_token
fi
fi
set -u
if [ -d ~/dotfiles ]; then
echo "pulling latest dotfiles"
(
cd ~/dotfiles
git pull
)
else
git clone [email protected]:sheeley/dotfiles.git ~/dotfiles
fi
if [ ! -f ~/.nix-private/private.nix ]; then
mkdir -p ~/.nix-private
cp ~/dotfiles/private.nix ~/.nix-private/private.nix
echo "set values in ~/.nix-private/private.nix"
if [[ "$IS_MAC" ]]; then
sed -i "" "s/EMAIL_HERE/$EMAIL/g" ~/.nix-private/private.nix
sed -i "" "s/EMAIL_HERE/$EMAIL/g" ~/.nix-private/private.nix
else
sed -i "s/GH_TOKEN_HERE/$HOMEBREW_GITHUB_API_TOKEN/g" ~/.nix-private/private.nix
sed -i "s/EMAIL_HERE/$EMAIL/g" ~/.nix-private/private.nix
fi
echo "BLAH" >~/.gh_token
fi
if [[ "$IS_MAC" ]]; then
confirm "Log in to App Store then hit enter"
fi
if [[ "$IS_NIX" && ! -d "$HOME/dotfiles/$(hostname)" ]]; then
if confirm "Change from current hostname: $HOST?"; then
while true; do
set_hostname && break
done
fi
if confirm "Make a new directory for this host?"; then
echo "mkdir $(hostname)"
echo "cp /etc/nixos/*.nix ./$(hostname)/"
echo ""
echo "ensure configuration.nix has:"
echo "networking.hostName = \"$(hostname)\";"
echo ""
if confirm "apply changes and reboot?"; then
sudo nixos-rebuild switch
sudo reboot
else
echo "sudo nixos-rebuild switch"
echo "sudo reboot"
exit 1
fi
fi
fi
echo ""
if confirm "Customize private.nix"; then
$EDITOR ~/.nix-private/private.nix
fi
(
cd ~/dotfiles || exit
./apply
)
if [[ "$IS_MAC" ]]; then
if [[ "$SHELL" != "/run/current-system/sw/bin/fish" ]]; then
echo "Shell isn't fish - may want to chsh!"
# if cat /etc/shells | grep /run/current-system/sw/bin/fish; then
# chsh -s /run/current-system/sw/bin/fish
# else
# echo "couldn't set shell to fish"
# fi
fi
fi