-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.sh
More file actions
executable file
·312 lines (263 loc) · 11.5 KB
/
bootstrap.sh
File metadata and controls
executable file
·312 lines (263 loc) · 11.5 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env bash
set -euo pipefail
echo "=================================================="
echo " macOS Development Environment Bootstrap"
echo "=================================================="
echo
# Check if we're in the right directory
if [ ! -f "flake.nix" ]; then
echo "Error: Must run this script from the starterpack directory"
exit 1
fi
REPO_DIR="$(pwd)"
echo "This script will configure machine-specific settings:"
echo " - Homebrew"
echo " - Git user configuration"
echo " - SSH key for GitHub"
echo " - Claude API key"
echo
# ============================================================================
# Homebrew
# ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Homebrew"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
if command -v brew > /dev/null 2>&1; then
echo "Homebrew already installed, skipping"
else
echo "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo "✓ Homebrew installed"
fi
echo
# ============================================================================
# /etc File Cleanup for nix-darwin
# ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "/etc Preparation for nix-darwin"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "nix-darwin needs to manage /etc/zshrc and /etc/bashrc."
echo "Any existing files will be backed up."
echo
for f in /etc/zshrc /etc/bashrc; do
if [ -f "$f" ] && [ ! -f "${f}.before-nix-darwin" ]; then
echo "Backing up $f..."
sudo mv "$f" "${f}.before-nix-darwin"
echo "✓ Moved $f -> ${f}.before-nix-darwin"
elif [ -f "${f}.before-nix-darwin" ]; then
echo "$f already backed up, skipping"
else
echo "$f does not exist, skipping"
fi
done
echo
# ============================================================================
# Git Configuration
# ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Git Configuration"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
CURRENT_GIT_NAME=$(git config --global user.name 2>/dev/null || true)
CURRENT_GIT_EMAIL=$(git config --global user.email 2>/dev/null || true)
if [ -n "$CURRENT_GIT_NAME" ] || [ -n "$CURRENT_GIT_EMAIL" ]; then
echo "Git already configured:"
echo " Name: $CURRENT_GIT_NAME"
echo " Email: $CURRENT_GIT_EMAIL"
read -p "Update git configuration? (y/N): " UPDATE_GIT
if [[ ! "$UPDATE_GIT" =~ ^[Yy]$ ]]; then
echo "Keeping existing git configuration"
GIT_NAME="$CURRENT_GIT_NAME"
GIT_EMAIL="$CURRENT_GIT_EMAIL"
SKIP_GIT=true
fi
fi
if [ "${SKIP_GIT:-false}" != "true" ]; then
read -p "Enter your full name for git${CURRENT_GIT_NAME:+ [$CURRENT_GIT_NAME]}: " GIT_NAME_INPUT
read -p "Enter your email for git${CURRENT_GIT_EMAIL:+ [$CURRENT_GIT_EMAIL]}: " GIT_EMAIL_INPUT
GIT_NAME="${GIT_NAME_INPUT:-$CURRENT_GIT_NAME}"
GIT_EMAIL="${GIT_EMAIL_INPUT:-$CURRENT_GIT_EMAIL}"
if [ -n "$GIT_NAME" ] && [ -n "$GIT_EMAIL" ]; then
git config --global user.name "$GIT_NAME"
git config --global user.email "$GIT_EMAIL"
echo "✓ Git configured with:"
echo " Name: $GIT_NAME"
echo " Email: $GIT_EMAIL"
else
echo "Skipping git configuration (name or email empty)"
fi
fi
echo
# ============================================================================
# SSH Key Generation
# ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "SSH Key for GitHub"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
SSH_KEY="$HOME/.ssh/id_ed25519"
if [ -f "$SSH_KEY" ]; then
echo "SSH key already exists at $SSH_KEY, skipping generation"
else
echo "Generating SSH key..."
ssh-keygen -t ed25519 -C "$GIT_EMAIL" -f "$SSH_KEY" -N ""
echo "✓ SSH key generated at $SSH_KEY"
fi
echo
if ssh-add -L 2>/dev/null | grep -qF "$(awk '{print $2}' "$SSH_KEY.pub")"; then
echo "SSH key already loaded in agent, skipping"
else
agent_exit=$(ssh-add -l > /dev/null 2>&1; echo $?)
[ "$agent_exit" = "2" ] && eval "$(ssh-agent -s)"
ssh-add "$SSH_KEY"
echo "✓ SSH key added to agent"
fi
echo
echo "Testing GitHub SSH connection..."
GITHUB_SSH_RESULT=$(ssh -T [email protected] 2>&1 || true)
echo "$GITHUB_SSH_RESULT"
if echo "$GITHUB_SSH_RESULT" | grep -q "successfully authenticated"; then
echo "✓ GitHub SSH already configured"
else
echo
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📋 Add this SSH key to GitHub:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
cat "$SSH_KEY.pub"
echo
echo "Steps:"
echo " 1. Copy the SSH key above"
echo " 2. Go to https://github.com/settings/ssh/new"
echo " 3. Paste the key and give it a title (e.g., 'MacBook Pro')"
echo " 4. Click 'Add SSH key'"
echo
read -p "Press Enter once you've added the SSH key to GitHub..."
echo
echo "Testing GitHub SSH connection..."
ssh -T [email protected] 2>&1 | head -2 || true
echo
fi
echo
# ============================================================================
# Claude API Key
# ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Claude API Key Configuration"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
CLAUDE_DIR="$HOME/.claude"
CLAUDE_CONFIG="$CLAUDE_DIR/config.json"
mkdir -p "$CLAUDE_DIR"
if [ -f "$CLAUDE_CONFIG" ] && grep -q "apiKey" "$CLAUDE_CONFIG" 2>/dev/null; then
echo "Claude API key already configured in $CLAUDE_CONFIG, skipping"
SKIP_CLAUDE=true
fi
if [ "${SKIP_CLAUDE:-false}" != "true" ]; then
echo "Get your API key from: https://console.anthropic.com/settings/keys"
echo
read -p "Enter your Claude API key (or press Enter to skip): " CLAUDE_API_KEY
if [ -n "$CLAUDE_API_KEY" ]; then
cat > "$CLAUDE_CONFIG" <<EOF
{
"apiKey": "$CLAUDE_API_KEY"
}
EOF
chmod 600 "$CLAUDE_CONFIG"
echo "✓ Claude API key configured at $CLAUDE_CONFIG"
else
echo "Skipping Claude API key configuration"
fi
fi
echo
# ============================================================================
# Gemini API Key
# ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Gemini API Key Configuration"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
SECRETS_FILE="$HOME/.config/zsh/.zshrc.d/secrets.zsh"
if grep -q "GEMINI_API_KEY" "$SECRETS_FILE" 2>/dev/null; then
echo "Gemini API key already configured in $SECRETS_FILE, skipping"
SKIP_GEMINI=true
fi
if [ "${SKIP_GEMINI:-false}" != "true" ]; then
echo "Get your API key from: https://aistudio.google.com/apikey"
echo
read -p "Enter your Gemini API key (or press Enter to skip): " GEMINI_API_KEY_INPUT
if [ -n "$GEMINI_API_KEY_INPUT" ]; then
mkdir -p "$(dirname "$SECRETS_FILE")"
touch "$SECRETS_FILE"
if grep -q "GEMINI_API_KEY" "$SECRETS_FILE" 2>/dev/null; then
sed -i '' "s|^export GEMINI_API_KEY=.*|export GEMINI_API_KEY=\"$GEMINI_API_KEY_INPUT\"|" "$SECRETS_FILE"
else
echo "export GEMINI_API_KEY=\"$GEMINI_API_KEY_INPUT\"" >> "$SECRETS_FILE"
fi
chmod 600 "$SECRETS_FILE"
echo "✓ Gemini API key configured at $SECRETS_FILE"
else
echo "Skipping Gemini API key configuration"
fi
fi
echo
# ============================================================================
# Global Environment Keys
# ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Global Environment Keys"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Some tools (e.g. Claude Code) run in non-interactive shells"
echo "and require keys to be set in ~/.zshenv rather than ~/.zshrc."
echo
ZSHENV_LOCAL="$HOME/.config/zsh/.zshenv.local"
for KEY in GEMINI_API_KEY; do
if grep -q "^export $KEY=" "$SECRETS_FILE" 2>/dev/null; then
if grep -q "^export $KEY=" "$ZSHENV_LOCAL" 2>/dev/null; then
echo "$KEY already in ~/.config/zsh/.zshenv.local, skipping"
else
read -p "Export $KEY to global environment (~/.config/zsh/.zshenv.local)? (y/N): " EXPORT_KEY
if [[ "$EXPORT_KEY" =~ ^[Yy]$ ]]; then
VALUE=$(grep "^export $KEY=" "$SECRETS_FILE" | cut -d'=' -f2-)
echo "export $KEY=$VALUE" >> "$ZSHENV_LOCAL"
chmod 600 "$ZSHENV_LOCAL"
echo "✓ $KEY added to ~/.config/zsh/.zshenv.local"
fi
fi
fi
done
echo
# ============================================================================
# Summary
# ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✓ Bootstrap Complete!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
echo "Configuration summary:"
echo " ✓ Git: $GIT_NAME <$GIT_EMAIL>"
echo " ✓ SSH key: $SSH_KEY"
echo " $([ -f "$CLAUDE_CONFIG" ] && echo "✓" || echo "⚠") Claude API key"
echo " $(grep -q "GEMINI_API_KEY" "$SECRETS_FILE" 2>/dev/null && echo "✓" || echo "⚠") Gemini API key"
echo
echo "Next steps:"
echo
echo " 1. Install Nix (if not already installed):"
echo " curl -L https://nixos.org/nix/install | sh -s -- --daemon"
echo
echo " 2. Move this directory to ~/.config/starterpack:"
echo " mv \"$REPO_DIR\" ~/.config/starterpack"
echo " cd ~/.config/starterpack"
echo
echo " 3. Generate flake.lock:"
echo " nix flake update"
echo
echo " 4. Apply the configuration:"
echo " nix run nix-darwin -- switch --flake ~/.config/starterpack"
echo
echo " 5. After first run, use:"
echo " darwin-rebuild switch --flake ~/.config/starterpack"
echo
echo "See README.md for more details."
echo