-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·84 lines (73 loc) · 2.52 KB
/
install.sh
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
#!/bin/bash
set -e
USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}"
MULTIUSER="${MULTIUSER:-"true"}"
FLAKEURI="${FLAKEURI:-"none"}"
FEATURE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi
# Determine the appropriate non-root user
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
if id -u "${CURRENT_USER}" >/dev/null 2>&1; then
USERNAME="${CURRENT_USER}"
break
fi
done
if [ "${USERNAME}" = "" ]; then
USERNAME=root
fi
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} >/dev/null 2>&1; then
USERNAME=root
fi
# Fix permissions
if [ "${USERNAME}" = "root" ]; then
user_home="/root"
else
# Find user home directory
user_home="/home/${USERNAME}"
if [ ! -d "${user_home}" ]; then
mkdir -p "${user_home}"
chown "${USERNAME}:${group_name}" "${user_home}"
fi
# Create per-user profile as it may not have been created by default in
# container environment
user_nix_profile="/nix/var/nix/profiles/per-user/${USERNAME}"
if [[ ! -d "${user_nix_profile}" ]]; then
echo "Creating per-user profile..."
mkdir -p "${user_nix_profile}"
chown "${USERNAME}:${group_name}" "${user_nix_profile}"
fi
fi
# Create hook to install Home if specified
if [ "${FLAKEURI}" != "none" ]; then
install_script="$(
cat <<-EOF
#!/bin/bash
set -e
# Container run without USER env variable set so we need to set it manually
# in oder to make home-manager work properly
export USER="\${USER:-\$(whoami)}"
# Install Nix flake in profile if specified
echo "Installing flake ${FLAKEURI} in profile..."
nix run home-manager -- switch --flake "${FLAKEURI}" -b backup-before-nix --refresh
EOF
)"
if [ ! -e "/usr/local/share/catbox-install-home.sh" ]; then
echo "(*) Setting up entrypoint..."
echo "${install_script}" >/usr/local/share/catbox-install-home.sh
chmod +x /usr/local/share/catbox-install-home.sh
fi
fi
# Create cache folders with correct privs in case a volume is mounted here
cache_folders=(".cache" ".cache/pip" ".cache/npm" ".cache/mix" ".cache/nix" ".cache/huggingface")
for folder in "${cache_folders[@]}"; do
mkdir -p "${user_home}/${folder}"
chown -R "${USERNAME}:${USERNAME}" "${user_home}/${folder}"
chmod -R u+wrx "${user_home}/${folder}"
done
echo "Done!"