forked from egen/disruptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.sh
More file actions
executable file
·202 lines (178 loc) · 5.77 KB
/
shell.sh
File metadata and controls
executable file
·202 lines (178 loc) · 5.77 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
#!/usr/bin/env sh
# Assumption #1: non-GNU, non-BSD linux distros are out-of-scope
# Otherwise we at least need to get rid of 'local' keyword
# Assumption #2: coreutils are present in $PATH
# Assumption #3: there is internet access (assumption to be removed)
# '-o pipefail' is not POSIX compliant, will fail in some sh's
set -eu
realpath () {
case "$(uname)" in
Linux)
readlink -f -- "$1"
;;
Darwin)
greadlink -f -- "$1"
;;
esac
}
# Find current script location
{
__FILE__=$(dirname "$(realpath $0)")
} || {
echo "Unable to determine current script location.."
echo "Will assume '.'"
__FILE__="."
}
fail() {
echo "$0:" "$@" >&2
exit 1
}
# Nicked from https://releases.nixos.org/nix/nix-2.5.1/install
require_util() {
command -v "$1" > /dev/null 2>&1 ||
fail "you do not have '$1' installed, which I need to $2"
}
readonly USER_HOME="${HOME:-"HOME variable has not been set"}"
readonly CACHE_ROOT="${__FILE__}/../.cache"
readonly NIX_USER_CHROOT_VERSION="1.2.2"
readonly NIX_USER_CHROOT_DIR="${CACHE_ROOT}/nix-user-chroot"
readonly NIX_USER_CHROOT_BIN="${NIX_USER_CHROOT_DIR}/nix-user-chroot"
readonly NIX_STORE="${CACHE_ROOT}/nix-store"
readonly NIX_VERSION="2.5.1"
readonly NIX_INSTALL_SCRIPT="${CACHE_ROOT}/nix-${NIX_VERSION}-install.sh"
readonly NIX_EXTRA_CONF_PATH="${__FILE__}/nix.conf"
readonly NIX_DIRENV_CONF_PATH="${CACHE_ROOT}/direnv.toml"
IS_NIX_INSTALLED=false
# Check if running on NixOS
if [ -e "/etc/os-release" ]; then
case "$(cat /etc/os-release)" in
*NixOS*)
readonly IS_NIXOS=true;
;;
*)
readonly IS_NIXOS=false;
;;
esac
else
readonly IS_NIXOS=false;
fi
preflightCheck() {
require_util curl "download dependencies"
require_util tar "decompress nix installation package"
case "$(uname)" in
Darwin)
require_util nix "make the magic happen. You are running on OSX, please make sure Nix is installed by running the following: sh <(curl -L https://nixos.org/nix/install)."
;;
Linux)
require_util unshare "verify kernel support for user namespaces"
;;
*)
echo "Platform not yet supported"
;;
esac
}
setup_nix_user_chroot() {
# Verify that user namespaces for unprivileged users are enabled
local readonly are_user_ns_enabled="$(unshare\
--user --pid echo -n YES \
)"
if [ "$are_user_ns_enabled" != "YES" ]; then
# TODO: Implement fallback to proot
fail "Current kernel settings do not allow\
running user namespaces for unprivileged users."
fi
local readonly arch=$(uname -m)
case "$arch" in
"aarch64")
local readonly download_url="https://github.com/nix-community\
/nix-user-chroot/releases/download/${NIX_USER_CHROOT_VERSION}\
/nix-user-chroot-bin-${NIX_USER_CHROOT_VERSION}-aarch64-unknown-linux-musl"
;;
"x86_64")
local readonly download_url="https://github.com/nix-community\
/nix-user-chroot/releases/download/${NIX_USER_CHROOT_VERSION}\
/nix-user-chroot-bin-${NIX_USER_CHROOT_VERSION}-x86_64-unknown-linux-musl"
;;
*)
fail "Arch '${arch}' is not supported at the moment."
;;
esac
mkdir -p "${NIX_USER_CHROOT_DIR}" || {
fail "Unable to create '${NIX_USER_CHROOT_DIR}'"
}
curl -L -o "${NIX_USER_CHROOT_BIN}" "${download_url}" || {
fail "Unable to download nix-user-chroot (url: ${download_url})"
}
chmod +x "${NIX_USER_CHROOT_BIN}"
}
setup_nix() {
mkdir -p "${NIX_STORE}" || {
fail "Unable to create '${NIX_STORE}'"
}
$NIX_USER_CHROOT_BIN "${NIX_STORE}" sh -c\
"curl -L https://releases.nixos.org/nix/nix-${NIX_VERSION}/install > ${NIX_INSTALL_SCRIPT} && sh ${NIX_INSTALL_SCRIPT}\
--no-daemon\
--no-modify-profile\
--nix-extra-conf-file ${NIX_EXTRA_CONF_PATH}"
}
ensure_direnv_is_configured() {
local readonly project_root=$(dirname $(realpath ${__FILE__}/../docs))
mkdir -p "${CACHE_ROOT}"
echo "[whitelist]" > ${NIX_DIRENV_CONF_PATH}
echo "prefix = [ \"${project_root}\" ]" >> ${NIX_DIRENV_CONF_PATH}
}
ensure_nix_is_present() {
if $IS_NIXOS; then
# On nixos, nix is installed by default
return
fi
if command -v "nix" >/dev/null 2>&1; then
IS_NIX_INSTALLED=true;
else
IS_NIX_INSTALLED=false;
fi
# We need to distinguish between single-user and multi-user installs.
# This is difficult because there's no official way to do this.
# Details: https://github.com/lilyball/nix-env.fish/blob/00c6cc762427efe08ac0bd0d1b1d12048d3ca727/conf.d/nix-env.fish
# stat is not portable. Splitting the output of ls -nd is reliable on most platforms.
if $IS_NIX_INSTALLED; then
local readonly nix_store_owner=$(ls -nd /nix/store | cut -d' ' -f3)
if [ "${nix_store_owner}" -eq 0 ]; then
local readonly is_nix_multiuser_install=true;
else
local readonly is_nix_multiuser_install=false;
fi
else
local readonly is_nix_multiuser_install=false;
fi
# Global, single-user installation
if $IS_NIX_INSTALLED && ! $is_nix_multiuser_install; then
return
fi
if $IS_NIX_INSTALLED && $is_nix_multiuser_install; then
# TODO: Find a solution
fail "Daemon-based nix installation is not supported"
fi
if [ -d "${NIX_STORE}" ]; then
# Nix has already been set up
echo "Detected previous setup in ${CACHE_ROOT}. Will attempt to use it."
return
fi
# No nix installed or nix is multi-user installation
setup_nix_user_chroot
setup_nix
}
preflightCheck
ensure_nix_is_present
ensure_direnv_is_configured
if $IS_NIXOS || $IS_NIX_INSTALLED; then
NIX_USER_CONF_FILES=${NIX_EXTRA_CONF_PATH}\
nix-shell --pure "${__FILE__}/shell.nix"
else
# Explicitly source nix profile in bash invocation
# In future: remove dependency on user HOME
$NIX_USER_CHROOT_BIN "${NIX_STORE}" bash -c\
". ${HOME}/.nix-profile/etc/profile.d/nix.sh\
&& NIX_USER_CONF_FILES=${NIX_EXTRA_CONF_PATH}\
nix-shell --pure '${__FILE__}/shell.nix'"
fi