Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
reflex-frp
GitHub Repository: reflex-frp/reflex-platform
Path: blob/develop/scripts/common-setup.sh
1 views
1
# This file should not be run directly; it exists to share setup code between the various scripts in this repository
2
# Before running this script, DIR should be defined equal to the directory containing this script
3
4
REPO="https://github.com/reflex-frp/reflex-platform"
5
6
NIXOPTS="--option extra-binary-caches https://nixcache.reflex-frp.org"
7
8
NIX_CONF="/etc/nix/nix.conf"
9
10
if [ -e "$NIX_CONF" ] && grep -q 'https://ryantrinkle\.com:5443' "$NIX_CONF"; then
11
>&2 echo "Warning: The reflex-platform cache server has moved from https://ryantrinkle.com:5443 to https://nixcache.reflex-frp.org. Please update your /etc/nixos/configuration/nix or /etc/nix/nix.conf accordingly"
12
if ! grep -q 'https://nixcache\.reflex-frp\.org' ; then
13
NIXOPTS+=" --option extra-binary-caches https://ryantrinkle.com:5443"
14
fi
15
fi
16
17
LOGFILE="$0.log"
18
19
trap "echo 'It looks like a problem occurred. Please submit an issue at $REPO/issues - include $LOGFILE to provide more information'; exit 1" ERR
20
21
echo "Command: " "$0" "$@" >"$LOGFILE"
22
exec 3>&1
23
exec 4>&2
24
exec > >(tee -ia "$LOGFILE")
25
exec 2> >(tee -ia "$LOGFILE" >&2)
26
27
terminate_logging() {
28
exec 1>&3
29
exec 2>&4
30
exec 3>&-
31
exec 4>&-
32
}
33
34
# Exit because the user caused an error, with the given error code and message
35
user_error() {
36
>&2 echo "$2"
37
trap - ERR
38
exit "$1"
39
}
40
41
on_darwin_host() { [ "$(uname -s)" == 'Darwin' ]; }
42
43
reset_daemon() {
44
if on_darwin_host; then
45
sudo launchctl stop org.nixos.nix-daemon
46
sudo launchctl start org.nixos.nix-daemon
47
fi
48
}
49
50
installing_nix=false
51
user_prefs="$HOME/.local/share/reflex-platform"
52
skip_cache_setup="$user_prefs/skip_cache_setup"
53
nixconf_dir="/etc/nix"
54
nixconf="$nixconf_dir/nix.conf"
55
our_cache="https://nixcache.reflex-frp.org"
56
our_key="JJiAKaRv9mWgpVAz8dwewnZe0AzzEAzPkagE9SP5NWI="
57
58
nixconf_exists() {
59
if [ -e "$nixconf" ]; then return 0; else return 1; fi;
60
}
61
62
nixconf_has_cache_settings() {
63
if nixconf_exists && grep -q '^binary-caches\|^binary-cache-public-keys\|^binary-caches-parallel-connections' "$nixconf" ; then return 0; else return 1; fi;
64
}
65
66
nixconf_has_reflex_cache() {
67
if nixconf_has_cache_settings && grep -q "$our_cache" "$nixconf"; then return 0; else return 1; fi;
68
}
69
70
nixconf_has_reflex_key() {
71
if nixconf_has_cache_settings && grep -q "$our_key" "$nixconf"; then return 0; else return 1; fi;
72
}
73
74
enable_cache() {
75
if [ -e "$skip_cache_setup" ]; then return 0; fi;
76
77
if nixconf_has_reflex_cache && nixconf_has_reflex_key; then return 0; fi;
78
79
if uname -v | grep -i "\bnixos\b"; then
80
echo "Please enable reflex's binary cache by following the instructions at https://github.com/reflex-frp/reflex-platform/blob/develop/notes/NixOS.md"
81
return 0;
82
fi
83
84
mkdir -p "$user_prefs"
85
if [ "$installing_nix" = false ]; then
86
echo "Add binary caches for reflex to $nixconf ?"
87
select yn in "Yes" "No" "Ask again next time"; do
88
case $yn in
89
"Yes" )
90
break;;
91
"No" )
92
touch "$skip_cache_setup"
93
echo "To re-enable this prompt do: "
94
echo "rm $skip_cache_setup"
95
echo ""
96
return 0;;
97
"Ask again next time" )
98
return 0;;
99
esac
100
done
101
fi
102
103
sudo_msg="This requires root access."
104
backup="$nixconf.$(date -u +"%FT%TZ").bak"
105
if nixconf_exists; then
106
echo "$nixconf already exists: creating backup - $sudo_msg"
107
sudo cp "$nixconf" "$backup"
108
echo "backup saved at $backup"
109
fi
110
111
caches_line="binary-caches = https://cache.nixos.org $our_cache"
112
keys_line="binary-cache-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= ryantrinkle.com-1:$our_key"
113
if ! nixconf_has_cache_settings; then
114
if ! nixconf_exists; then
115
echo "Creating $nixconf - $sudo_msg";
116
else
117
echo "Adding cache settings to $nixconf - $sudo_msg";
118
fi
119
sudo mkdir -p "$nixconf_dir"
120
sudo tee -a "$nixconf" > /dev/null <<EOF
121
$caches_line
122
$keys_line
123
binary-caches-parallel-connections = 40
124
EOF
125
reset_daemon
126
else
127
echo "Adding cache settings to $nixconf - $sudo_msg"
128
if ! nixconf_has_reflex_cache; then
129
sudo sed -i.bak 's|^\(binary-caches[ =].*\)$|\1 '"$our_cache"'|' "$nixconf"
130
fi
131
if ! nixconf_has_reflex_key; then
132
sudo sed -i.bak 's|^\(binary-cache-public-keys[ =].*\)$|\1 ryantrinkle.com-1:'"$our_key"'|' "$nixconf"
133
fi
134
reset_daemon
135
fi
136
}
137
138
if on_darwin_host; then
139
if (sw_vers | grep "ProductVersion" | grep "\(10.13.0\|10.13.1\)$" || false) &> /dev/null; then
140
allow_broken_macos="$HOME/.local/share/reflex-platform/allow-broken-macos"
141
if [ ! -d "$allow_broken_macos" ]; then
142
echo "Running nix on High Sierra versions prior to 10.13.2 is likely to cause system crashes"
143
echo "See https://github.com/NixOS/nix/issues/1583"
144
echo "Please update your system to continue safely"
145
echo "If you still want to try, do:"
146
echo "mkdir -p $allow_broken_macos"
147
exit 1
148
fi
149
fi
150
fi
151
152
>&2 echo "If you have any trouble with this script, please submit an issue at $REPO/issues"
153
154
(
155
156
cd "$DIR"
157
158
if [ ! -d /nix ]; then
159
installing_nix=true
160
if ! type -P curl >/dev/null ; then
161
echo "Please make sure that 'curl' is installed and can be run from this shell"
162
exit 1
163
fi
164
165
echo "In order to continue, $0 must install the Nix package manager. This requires root access, so you will be prompted for your password. If you do not wish to continue, just hit Ctrl-C at the password prompt."
166
./scripts/installNix.sh
167
fi
168
169
)
170
171
# The command to source the nix script. This should be a line of valid bash code.
172
if [ -O /nix/store ]; then
173
SOURCE_NIX_SCRIPT=". $HOME/.nix-profile/etc/profile.d/nix.sh"
174
else
175
SOURCE_NIX_SCRIPT=". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh"
176
fi
177
178
# Whether the nix script needed to be sourced - i.e. nix commands are not available without doing so, from the user's basic prompt.
179
NEEDED_TO_SOURCE_NIX_SCRIPT=0
180
181
if ! type -P nix-shell >/dev/null; then
182
set +eu
183
$SOURCE_NIX_SCRIPT
184
set -eu
185
NEEDED_TO_SOURCE_NIX_SCRIPT=1
186
if ! type -P nix-shell >/dev/null; then
187
echo "It looks like Nix isn't working. Please make sure you can run nix-shell, then retry the $0, or submit an issue at $REPO/issues"
188
exit 1
189
fi
190
fi
191
192
# The minimum required version of Nix to run this script.
193
MIN_REQUIRED_NIX_VERSION="1.8"
194
195
if [ "$(nix-instantiate --eval --expr "builtins.compareVersions builtins.nixVersion \"$MIN_REQUIRED_NIX_VERSION\" >= 0")" != "true" ]; then
196
echo "It looks like your version of Nix, $(nix-instantiate --eval --expr "builtins.nixVersion"), is older than the minimum version required by the Reflex Platform, \"$MIN_REQUIRED_NIX_VERSION\". You'll need to upgrade Nix to continue. On non-NixOS platforms, that can usually be done like this:"
197
if [ "$NEEDED_TO_SOURCE_NIX_SCRIPT" -ne 0 ]; then
198
echo "$SOURCE_NIX_SCRIPT"
199
fi
200
echo "nix-env --upgrade"
201
echo "If you're on NixOS, you may need to upgrade your OS to a later version. See https://nixos.org/nixos/manual/sec-upgrading.html"
202
exit 1
203
fi
204
205
enable_cache
206
207
git_thunk() {
208
case "$1" in
209
git) echo "import ((import <nixpkgs> {}).fetchgit (builtins.fromJSON (builtins.readFile ./git.json)))" ;;
210
github) echo "import ((import <nixpkgs> {}).fetchFromGitHub (builtins.fromJSON (builtins.readFile ./github.json)))" ;;
211
esac
212
}
213
214
# NOTE: Returns the manifest type in OUTPUT_GIT_MANIFEST_TYPE and the manifest contents in OUTPUT_GIT_MANIFEST
215
get_git_manifest() {
216
local NIX_PREFETCH_SCRIPTS="$(nix-build --no-out-link -E "(import \"$DIR/nixpkgs\" {}).nix-prefetch-scripts")"
217
local NIX="$(nix-build --no-out-link -E "(import \"$DIR/nixpkgs\" {}).nix")"
218
local REPO="$(echo "$1" | sed 's/\.git$//')"
219
220
local URL="$(git -C "$REPO" config --get remote.origin.url | sed 's_^[email protected]:_git://github.com/_')" # Don't use [email protected] origins, since these can't be accessed by nix
221
local REV="$(git -C "$REPO" rev-parse HEAD)"
222
223
local GITHUB_PATTERN="^git://github.com/\([^/]*\)/\([^/]*\)$"
224
local GITHUB_ARCHIVE_URL="$(echo "$URL" | sed -n "s_${GITHUB_PATTERN}_https://github.com/\1/\2/archive/$REV.tar.gz_p")"
225
if [ -n "$GITHUB_ARCHIVE_URL" -a "$(curl -o /dev/null --silent --head --write-out '%{http_code}' "$GITHUB_ARCHIVE_URL")" -ne 404 ] ; then
226
OUTPUT_GIT_MANIFEST_TYPE=github
227
local GITHUB_OWNER="$(echo "$URL" | sed "s_${GITHUB_PATTERN}_\1_")"
228
local GITHUB_REPO="$(echo "$URL" | sed "s_${GITHUB_PATTERN}_\2_")"
229
local SHA256="$($NIX/bin/nix-prefetch-url --unpack --type sha256 "$GITHUB_ARCHIVE_URL")"
230
OUTPUT_GIT_MANIFEST="$(cat <<EOF
231
{
232
"owner": "$GITHUB_OWNER",
233
"repo": "$GITHUB_REPO",
234
"rev": "$REV",
235
"sha256": "$SHA256"
236
}
237
EOF
238
)"
239
else
240
OUTPUT_GIT_MANIFEST_TYPE=git
241
OUTPUT_GIT_MANIFEST="$("$NIX_PREFETCH_SCRIPTS"/bin/nix-prefetch-git "$PWD/$REPO" "$REV" 2>/dev/null | sed -e '/^ *"date":/d' -e "s|$(echo "$PWD/$REPO" | sed 's/|/\\|/g')|$(echo "$URL" | sed 's/|/\\|/g')|" 2>/dev/null)"
242
fi
243
}
244
245
# Clean up a path so it can be injected into a nix expression
246
cleanup_nix_path() {
247
echo "$1" | sed 's@/*$@@'
248
}
249
250
prebuild_try_reflex_shell() {
251
nix-instantiate "$DIR/shell.nix" --indirect --add-root "$DIR/gc-roots/shell.drv" $NIXOPTS >/dev/null
252
nix-store --realize "$DIR/gc-roots/shell.drv" --indirect --add-root "$DIR/gc-roots/shell.out" >/dev/null
253
}
254
255
try_reflex_shell() {
256
prebuild_try_reflex_shell
257
nix-shell -E '{path}: import path' --arg path "$(readlink "$DIR/gc-roots/shell.drv")" $NIXOPTS "$@"
258
}
259
260
# For a given effective platform, turn a string representing a package, which
261
# may be a package name or a path, into a nix invocation representing the
262
# package.
263
package_invocation() {
264
local EFFECTIVE_PLATFORM="$1"
265
local PACKAGE="$2"
266
267
if echo "$PACKAGE" | grep -q / ; then
268
# Package name includes a slash
269
local PACKAGE_PATH="$(cleanup_nix_path "$PACKAGE")"
270
if ( echo "$PACKAGE_PATH" | grep -q '\.nix$' && [ -f "$PACKAGE_PATH" ] ) || [ -f "$PACKAGE_PATH/default.nix" ] ; then
271
echo -n "($EFFECTIVE_PLATFORM.callPackage $PACKAGE_PATH {})"
272
elif test -n "$(shopt -s nullglob ; echo "$PACKAGE_PATH"/*.cabal)" ; then
273
echo -n "($EFFECTIVE_PLATFORM.callCabal2nix \"$(basename "$PACKAGE_PATH"/*.cabal | sed 's/\.cabal$//')\" $PACKAGE_PATH {})"
274
else
275
user_error 1 "Error: The given path must be a nix expression, a directory with a default.nix, or a directory with a cabal file"
276
fi
277
else
278
echo -n "$EFFECTIVE_PLATFORM.$PACKAGE"
279
fi
280
}
281
282