-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·245 lines (224 loc) · 7.46 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·245 lines (224 loc) · 7.46 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
#!/bin/bash
# @describe Setup development environment with Nix
# @flag --no-root Use nix-portable instead of root installation
# @flag --sudo-mount Use sudo with nsenter/mount for /nix access
# @arg nix_args~ Additional arguments to pass to nix build
# ARGC-BUILD {
# This block was generated by argc (https://github.com/sigoden/argc).
# Modifying it manually is not recommended
_argc_run() {
if [[ "${1:-}" == "___internal___" ]]; then
_argc_die "error: unsupported ___internal___ command"
fi
if [[ "${OS:-}" == "Windows_NT" ]] && [[ -n "${MSYSTEM:-}" ]]; then
set -o igncr
fi
argc__args=("$(basename "$0" .sh)" "$@")
argc__positionals=()
_argc_index=1
_argc_len="${#argc__args[@]}"
_argc_tools=()
_argc_parse
if [ -n "${argc__fn:-}" ]; then
$argc__fn "${argc__positionals[@]}"
fi
}
_argc_usage() {
cat <<-'EOF'
Setup development environment with Nix
USAGE: setup.sh [OPTIONS] [NIX-ARGS]...
ARGS:
[NIX-ARGS]... Additional arguments to pass to nix build
OPTIONS:
--no-root Use nix-portable instead of root installation
--sudo-mount Use sudo with nsenter/mount for /nix access
-h, --help Print help
-V, --version Print version
EOF
exit
}
_argc_version() {
echo setup.sh 0.0.0
exit
}
_argc_parse() {
local _argc_key _argc_action
local _argc_subcmds=""
while [[ $_argc_index -lt $_argc_len ]]; do
_argc_item="${argc__args[_argc_index]}"
_argc_key="${_argc_item%%=*}"
case "$_argc_key" in
--help | -help | -h)
_argc_usage
;;
--version | -version | -V)
_argc_version
;;
--)
_argc_dash="${#argc__positionals[@]}"
argc__positionals+=("${argc__args[@]:$((_argc_index + 1))}")
_argc_index=$_argc_len
break
;;
--no-root)
if [[ "$_argc_item" == *=* ]]; then
_argc_die "error: flag \`--no-root\` don't accept any value"
fi
_argc_index=$((_argc_index + 1))
if [[ -n "${argc_no_root:-}" ]]; then
_argc_die "error: the argument \`--no-root\` cannot be used multiple times"
else
argc_no_root=1
fi
;;
--sudo-mount)
if [[ "$_argc_item" == *=* ]]; then
_argc_die "error: flag \`--sudo-mount\` don't accept any value"
fi
_argc_index=$((_argc_index + 1))
if [[ -n "${argc_sudo_mount:-}" ]]; then
_argc_die "error: the argument \`--sudo-mount\` cannot be used multiple times"
else
argc_sudo_mount=1
fi
;;
*)
if _argc_maybe_flag_option "-" "$_argc_item"; then
_argc_die "error: unexpected argument \`$_argc_key\` found"
fi
argc__positionals+=("$_argc_item")
_argc_index=$((_argc_index + 1))
if [[ "${#argc__positionals[@]}" -ge 0 ]]; then
argc__positionals+=("${argc__args[@]:_argc_index}")
_argc_index=$_argc_len
fi
;;
esac
done
if [[ -n "${_argc_action:-}" ]]; then
$_argc_action
else
if [[ "${argc__positionals[0]:-}" == "help" ]] && [[ "${#argc__positionals[@]}" -eq 1 ]]; then
_argc_usage
fi
_argc_match_positionals 1
local values_index values_size
IFS=: read -r values_index values_size <<<"${_argc_match_positionals_values[0]:-}"
if [[ -n "$values_index" ]]; then
argc_nix_args=("${argc__positionals[@]:values_index:values_size}")
fi
fi
}
_argc_match_positionals() {
_argc_match_positionals_values=()
_argc_match_positionals_len=0
local params=("$@")
local args_len="${#argc__positionals[@]}"
if [[ $args_len -eq 0 ]]; then
return
fi
local params_len=$# arg_index=0 param_index=0
while [[ $param_index -lt $params_len && $arg_index -lt $args_len ]]; do
local takes=0
if [[ "${params[param_index]}" -eq 1 ]]; then
if [[ $param_index -eq 0 ]] &&
[[ ${_argc_dash:-} -gt 0 ]] &&
[[ $params_len -eq 2 ]] &&
[[ "${params[$((param_index + 1))]}" -eq 1 ]] \
; then
takes=${_argc_dash:-}
else
local arg_diff=$((args_len - arg_index)) param_diff=$((params_len - param_index))
if [[ $arg_diff -gt $param_diff ]]; then
takes=$((arg_diff - param_diff + 1))
else
takes=1
fi
fi
else
takes=1
fi
_argc_match_positionals_values+=("$arg_index:$takes")
arg_index=$((arg_index + takes))
param_index=$((param_index + 1))
done
if [[ $arg_index -lt $args_len ]]; then
_argc_match_positionals_values+=("$arg_index:$((args_len - arg_index))")
fi
_argc_match_positionals_len=${#_argc_match_positionals_values[@]}
if [[ $params_len -gt 0 ]] && [[ $_argc_match_positionals_len -gt $params_len ]]; then
local index="${_argc_match_positionals_values[params_len]%%:*}"
_argc_die "error: unexpected argument \`${argc__positionals[index]}\` found"
fi
}
_argc_maybe_flag_option() {
local signs="$1" arg="$2"
if [[ -z "$signs" ]]; then
return 1
fi
local cond=false
if [[ "$signs" == *"+"* ]]; then
if [[ "$arg" =~ ^\+[^+].* ]]; then
cond=true
fi
elif [[ "$arg" == -* ]]; then
if (( ${#arg} < 3 )) || [[ ! "$arg" =~ ^---.* ]]; then
cond=true
fi
fi
if [[ "$cond" == "false" ]]; then
return 1
fi
local value="${arg%%=*}"
if [[ "$value" =~ [[:space:]] ]]; then
return 1
fi
return 0
}
_argc_die() {
if [[ $# -eq 0 ]]; then
cat
else
echo "$*" >&2
fi
exit 1
}
_argc_run "$@"
# ARGC-BUILD }
set -eu
if [[ "$OSTYPE" == "darwin"* ]] && [ "${argc_no_root:-0}" -eq 1 ]; then
echo "Error: --no-root is not supported on macOS"
exit 1
fi
# Create config directory if it doesn't exist
mkdir -p config
# Save sudo mount preference to config file
if [ "${argc_sudo_mount:-0}" -eq 1 ]; then
echo "SUDO_MOUNT_ENABLED=1" > config/shelffiles.conf
echo "Sudo mount support enabled for containers"
else
echo "SUDO_MOUNT_ENABLED=0" > config/shelffiles.conf
fi
# Use nix-portable when --no-root or --sudo-mount is specified
if [ "${argc_no_root:-0}" -eq 1 ] || [ "${argc_sudo_mount:-0}" -eq 1 ]; then
if [ ! -e nix-portable ]; then
echo "Installing nix-portable..."
curl -L https://github.com/DavHau/nix-portable/releases/download/v012/nix-portable-"$(arch)" -o nix-portable
chmod +x nix-portable
fi
else
if [ -e "$HOME"/.nix-profile/etc/profile.d/nix.sh ]; then
# shellcheck disable=SC1091
. "$HOME"/.nix-profile/etc/profile.d/nix.sh
fi
if ! command -v nix &> /dev/null; then
echo "Installing Nix..."
sh <(curl -L https://nixos.org/nix/install)
fi
fi
echo "Building..."
if [ "${argc_no_root:-0}" -eq 1 ] || [ "${argc_sudo_mount:-0}" -eq 1 ]; then
./nix-portable nix build --extra-experimental-features nix-command --extra-experimental-features flakes --store "$(pwd)" "${argc_nix_args[@]}"
else
nix build --extra-experimental-features nix-command --extra-experimental-features flakes "${argc_nix_args[@]}"
fi