-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathsetup-doughnut-dev.sh
More file actions
executable file
·172 lines (140 loc) · 4.98 KB
/
setup-doughnut-dev.sh
File metadata and controls
executable file
·172 lines (140 loc) · 4.98 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
#!/usr/bin/env bash
set -eo pipefail
readonly NIX_CONF_DIR="${HOME}/.config/nix"
readonly NIX_CONF="${NIX_CONF_DIR}/nix.conf"
readonly NIXPKGS_CONF_DIR="${HOME}/.config/nixpkgs"
readonly NIXPKGS_CONF="${NIXPKGS_CONF_DIR}/config.nix"
readonly TEMP_DIR="$(mktemp -d)"
# Check bash version (we need 4.0+ for some features)
if ((BASH_VERSINFO[0] < 4)); then
echo "Error: Bash 4.0 or higher is required"
exit 1
fi
# Enable debug mode if DEBUG env var is set
[[ -n "${DEBUG}" ]] && set -x
# Error handling with cleanup
cleanup() {
[[ -d "${TEMP_DIR}" ]] && rm -rf "${TEMP_DIR}"
}
trap 'echo "Error: Script failed on line $LINENO"; cleanup' ERR
trap cleanup EXIT
trap cleanup SIGTERM SIGINT SIGHUP
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
trap cleanup SIGTERM SIGINT SIGHUP EXIT
fi
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*"
}
# Global variables
declare -r unameOut="$(uname -s)"
declare os_type="Unsupported"
get_os_type() {
case "${unameOut}" in
Linux*) os_type="Linux" ;;
Darwin*) os_type="Mac" ;;
*) log "Error: Unknown OS ${unameOut}"; exit 1 ;;
esac
}
check_wsl2() {
if [[ "${os_type}" == "Linux" ]] && grep -qi microsoft /proc/version; then
if ! grep -qi "wsl2" /proc/version; then
log "Error: WSL1 detected. Please upgrade to WSL2 for better performance"
exit 1
fi
log "WSL2 environment detected"
fi
}
configure_nix_flakes() {
local flakes_config="experimental-features = nix-command flakes"
# Check if we have write permission to config directory
if ! mkdir -p "${HOME}/.config/nix" 2>/dev/null; then
log "Error: Cannot create directory ${HOME}/.config/nix"
exit 1
fi
touch "${NIX_CONF}"
if ! grep -Fxq "${flakes_config}" "${NIX_CONF}"; then
log "Configuring nix flakes"
echo "${flakes_config}" > "${NIX_CONF}"
fi
}
allow_nix_unfree() {
mkdir -p "${NIXPKGS_CONF_DIR}"
log "Enabling unfree packages"
echo '{ allowUnfree = true; }' > "${NIXPKGS_CONF}"
}
install_nixpkg_manager() {
get_os_type
if command -v nix >/dev/null 2>&1; then
log "Nix package manager already installed"
return 0
fi
touch "${HOME}/.bash_profile"
if ! command -v curl >/dev/null 2>&1; then
log "Error: curl is required but not installed"
exit 1
fi
# Check internet connectivity
if ! curl -s --head https://install.determinate.systems >/dev/null; then
log "Error: No internet connection or install.determinate.systems is unreachable"
exit 1
fi
if [[ "${os_type}" == "Mac" || "${os_type}" == "Linux" ]]; then
local max_attempts=3
local attempt=1
local installed=false
while (( attempt <= max_attempts )); do
log "Installing nix using Determinate Systems installer (Attempt $attempt/$max_attempts)..."
# Adding --retry and --retry-connrefused to curl for better network resilience
if curl --proto '=https' --tlsv1.2 -sSf -L --retry 3 --retry-connrefused --connect-timeout 10 https://install.determinate.systems/nix | sh -s -- install --determinate --no-confirm; then
installed=true
break
else
log "Installation command failed (Attempt $attempt/$max_attempts)."
if (( attempt < max_attempts )); then
log "Retrying in 3 seconds..."
sleep 3
fi
((attempt++))
fi
done
if [[ "$installed" == "false" ]]; then
log "Error: Nix installation failed after $max_attempts attempts."
return 1
fi
else
log "Error: Unsupported OS Platform for Nix development environment"
return 1
fi
# Source nix in current shell
if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then
. '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'
fi
allow_nix_unfree
configure_nix_flakes
log "Nix installation completed successfully"
}
main() {
local rc=0
# Prevent running as root
if [[ $EUID -eq 0 ]]; then
log "Error: This script should not be run as root"
exit 1
fi
log "Starting doughnut development environment setup"
check_wsl2
install_nixpkg_manager || rc=$?
if [[ $rc -eq 0 ]]; then
log "------------------------------------------ CONGRATS !!! ----------------------------------------------------"
log " doughnut basic nix development environment tooling setup completed successfully."
log " Please exit this shell terminal and start a new one in doughnut root directory then execute 'nix develop'."
log " To uninstall Nix in the future, you can run: /nix/nix-installer uninstall"
log "------------------------------------------ END ----------------------------------------------------"
else
log "------------------------------------------ WARNING !!! ----------------------------------------------------"
log " Installation FAILED with warnings or errors. Please check the logs above."
log " You may need to manually verify the installation or try again."
log "------------------------------------------ END ----------------------------------------------------"
fi
exit $rc
}
main "$@"