Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·270 lines (253 loc) · 9.97 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·270 lines (253 loc) · 9.97 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/sh
# Exit on error
set -e
# Determine OS using uname
OS_NAME=$(uname)
case "$OS_NAME" in
Darwin)
OS="macos"
;;
Linux)
OS="linux"
;;
*)
echo "Unsupported operating system: $OS_NAME"
exit 1
;;
esac
# Beskar's declared profile and fleet runtime belong to root. Refuse a
# mismatched account before checkout, package installation or activation.
INSTALL_HOME=$HOME
_install_host=$(printf '%s' "${HOST:-$(hostname)}" | tr '[:upper:]' '[:lower:]')
_tailnet_host=
if [ "$OS" = linux ] && command -v tailscale >/dev/null 2>&1 && command -v jq >/dev/null 2>&1; then
_tailnet_host=$(timeout 10 tailscale status --json 2>/dev/null | jq -r '.Self.DNSName // empty' | cut -d. -f1 | tr '[:upper:]' '[:lower:]') || _tailnet_host=
fi
if [ "$OS" = linux ] && { [ "${_install_host%%.*}" = beskar ] || [ "$_tailnet_host" = beskar ]; }; then
if [ "$(id -u)" != 0 ]; then
echo "Beskar installation requires root." >&2
exit 1
fi
INSTALL_HOME=$(getent passwd root | cut -d: -f6)
if [ -z "$INSTALL_HOME" ] || [ ! -d "$INSTALL_HOME" ]; then
echo "Beskar's root account must have an existing home directory." >&2
exit 1
fi
HOME=$INSTALL_HOME
HOST=beskar
USER=root
LOGNAME=root
export HOME HOST USER LOGNAME
fi
# Named Kamino machines share one generated Linux/root profile family.
KAMINO_INSTALL=false
_normalized_host=$(printf '%s' "${HOST:-}" | tr '[:upper:]' '[:lower:]')
case "$_normalized_host" in
kamino*)
if ! printf '%s\n' "$_normalized_host" | grep -Eq '^kamino([1-9][0-9]*)?$'; then
echo "Invalid Kamino name: use kamino or kamino1, kamino2, ..." >&2
exit 1
fi
if [ "$OS" != linux ] || [ "$(uname -m)" != x86_64 ] || [ "$(id -u)" != 0 ]; then
echo "Kamino installation requires root on x86_64 Linux." >&2
exit 1
fi
if [ "$(cat /proc/1/comm 2>/dev/null)" != systemd ]; then
echo "Kamino services require systemd (VPS or systemd-enabled container)." >&2
exit 1
fi
if [ -f "$HOME/.config/kamino/name" ] && [ "$(cat "$HOME/.config/kamino/name")" != "$_normalized_host" ]; then
echo "Refusing to change this machine's installed Kamino identity." >&2
exit 1
fi
HOST=$_normalized_host
USER=root
export HOST USER
KAMINO_INSTALL=true
;;
esac
run_root() {
if [ "$(id -u)" = 0 ]; then
"$@"
else
sudo "$@"
fi
}
if [ "$KAMINO_INSTALL" = true ] && [ ! -f /usr/lib/systemd/user/dbus.socket ]; then
if command -v apt-get >/dev/null 2>&1; then
run_root apt-get update -qq
run_root apt-get install -y -qq dbus-user-session
else
echo "Install the systemd user D-Bus service before installing Kamino." >&2
exit 1
fi
fi
# Initialize
NIX_EFFECTIVE_BIN_PATH=""
# Ensure USER variable is set and exported
if [ -z "$USER" ]; then
CURRENT_USER=$(id -un)
if [ -z "$CURRENT_USER" ]; then
echo "Warning: Could not determine current user using 'id -un'. Defaulting USER to OS_NAME."
USER="$OS_NAME"
else
USER="$CURRENT_USER"
fi
export USER
echo "USER variable was not set or could not be determined. Setting and exporting USER to: $USER"
else
# Ensure USER is exported if it was already set
export USER
echo "USER variable is already set to: $USER. Ensuring it is exported."
fi
# Install Nix if not already installed
if ! command -v nix >/dev/null 2>&1; then
echo "Installing Nix..."
if [ "$OS" = "macos" ]; then
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm
# For macOS, source the Nix profile immediately to update PATH in CI.
# shellcheck source=/dev/null
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
NIX_EFFECTIVE_BIN_PATH="/nix/var/nix/profiles/default/bin"
else # Linux
# Auto-detect container environment (Docker, Podman, RunPod, etc.)
# Use /proc/1/comm instead of pidof - pidof can fail on minimal installs even when systemd is PID 1
_init_name=$(cat /proc/1/comm 2>/dev/null || echo "unknown")
if [ "$IN_DOCKER" = "true" ] || [ -f /.dockerenv ] || [ -f /run/.containerenv ] || [ "$_init_name" != "systemd" ]; then
echo "Performing Determinate Nix installation (Docker environment)..."
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux --init none --no-confirm
# Source the Nix profile script to add Nix to PATH for the current shell
if [ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then
# shellcheck source=/dev/null
. "$HOME/.nix-profile/etc/profile.d/nix.sh"
echo "Sourced Nix profile for Determinate Nix (Docker) setup."
else
echo "Warning: Nix profile script ($HOME/.nix-profile/etc/profile.d/nix.sh) not found after installation."
# Fallback PATH export for the current shell
export PATH="$HOME/.nix-profile/bin:$PATH"
fi
NIX_EFFECTIVE_BIN_PATH="$HOME/.nix-profile/bin"
else # Linux multi-user
echo "Performing Determinate Nix multi-user installation..."
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux --no-confirm
# Start the Nix daemon so nix commands work in this session
if command -v systemctl >/dev/null 2>&1; then
run_root systemctl start nix-daemon.service 2>/dev/null || true
fi
# Source the Nix profile to add nix to PATH
if [ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]; then
# shellcheck source=/dev/null
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
fi
export PATH="/nix/var/nix/profiles/default/bin:$PATH"
NIX_EFFECTIVE_BIN_PATH="/nix/var/nix/profiles/default/bin"
fi
fi
else
echo "Nix is already installed."
# Determine NIX_EFFECTIVE_BIN_PATH from existing Nix installation
_nix_executable_path=$(command -v nix)
if [ -n "$_nix_executable_path" ]; then
NIX_EFFECTIVE_BIN_PATH=$(dirname "$_nix_executable_path")
echo "Determined existing NIX_EFFECTIVE_BIN_PATH as: $NIX_EFFECTIVE_BIN_PATH"
else
echo "Warning: Nix is installed but 'command -v nix' failed to find it. PATH issues might persist."
# Fallback for safety, though 'command -v nix' in the outer 'if' should have caught this.
if [ "$OS" = "macos" ]; then
NIX_EFFECTIVE_BIN_PATH="/nix/var/nix/profiles/default/bin"
elif [ "$IN_DOCKER" = "true" ] || [ -f /.dockerenv ] || [ -f /run/.containerenv ] || ! pidof systemd >/dev/null 2>&1; then
NIX_EFFECTIVE_BIN_PATH="$HOME/.nix-profile/bin"
else
NIX_EFFECTIVE_BIN_PATH="/nix/var/nix/profiles/default/bin"
fi
echo "Using fallback NIX_EFFECTIVE_BIN_PATH: $NIX_EFFECTIVE_BIN_PATH"
fi
fi
# Ensure git is available (fresh Ubuntu minimal installs may not have it)
if ! command -v git >/dev/null 2>&1; then
echo "git is not installed. Installing git..."
if command -v apt-get >/dev/null 2>&1; then
run_root apt-get update -qq && run_root apt-get install -y -qq git
elif command -v dnf >/dev/null 2>&1; then
run_root dnf install -y git
elif command -v yum >/dev/null 2>&1; then
run_root yum install -y git
elif command -v pacman >/dev/null 2>&1; then
run_root pacman -Sy --noconfirm git
else
echo "Error: Could not install git. No supported package manager found."
exit 1
fi
fi
# Clone into the profile owner's home; leave any previous root checkout alone.
DOTFILES_DIR="$INSTALL_HOME/dotfiles"
cd "$INSTALL_HOME"
echo "Fetching the dotfiles repository..."
# If GITHUB_PR is set, handle checkout for the PR branch
if [ -n "$GITHUB_PR" ]; then
echo "Detected PR environment variable GITHUB_PR=$GITHUB_PR"
PR_REF="refs/pull/${GITHUB_PR}/head"
echo "Fetching pull request ref: $PR_REF"
if [ -d "$DOTFILES_DIR" ]; then
echo "Dotfiles repository already exists. Using git fetch to retrieve PR ref..."
cd "$DOTFILES_DIR"
git fetch origin "$PR_REF":pr-"$GITHUB_PR"
git checkout pr-"$GITHUB_PR"
git pull
else
echo "Cloning dotfiles repository..."
git clone https://github.com/shunkakinoki/dotfiles.git "$DOTFILES_DIR"
cd "$DOTFILES_DIR"
git fetch origin "$PR_REF":pr-"$GITHUB_PR"
git checkout pr-"$GITHUB_PR"
fi
else
if [ -d "$DOTFILES_DIR" ]; then
echo "Dotfiles repository already exists. Fetching latest changes..."
cd "$DOTFILES_DIR"
git fetch origin
git pull
else
echo "Cloning dotfiles repository into $DOTFILES_DIR..."
git clone https://github.com/shunkakinoki/dotfiles.git "$DOTFILES_DIR"
cd "$DOTFILES_DIR"
fi
fi
# Use make from nixpkgs when the base system does not provide it. This avoids
# invoking the non-setuid sudo package binary on fresh NixOS machines.
if [ "$KAMINO_INSTALL" = true ]; then
if ! nix --extra-experimental-features "nix-command flakes" eval --raw \
".#homeConfigurations.$HOST.config.home.username" >/dev/null; then
echo "HOST=$HOST is not available in this checkout's generated Kamino fleet." >&2
exit 1
fi
fi
if ! command -v make >/dev/null 2>&1; then
echo "make is not installed; it will be provided temporarily by nixpkgs."
fi
run_make_install() {
if command -v make >/dev/null 2>&1; then
HOST="$HOST" make install
else
HOST="$HOST" nix --extra-experimental-features "nix-command flakes" \
shell nixpkgs#gnumake --command make install
fi
}
# Install Nix packages
echo "Running installation commands..."
if [ -n "$HOST" ]; then
echo "Forwarding HOST=$HOST to make install (overrides hostname auto-detection)."
fi
if [ -n "$NIX_EFFECTIVE_BIN_PATH" ] && [ -d "$NIX_EFFECTIVE_BIN_PATH" ]; then
echo "Prepending $NIX_EFFECTIVE_BIN_PATH to PATH for 'make install' command."
echo "Ensuring USER=$USER and CI=$CI are passed to make install."
PATH="$NIX_EFFECTIVE_BIN_PATH:$PATH"
export PATH USER CI HOST
run_make_install
else
echo "Warning: NIX_EFFECTIVE_BIN_PATH ('$NIX_EFFECTIVE_BIN_PATH') is not set or not a directory."
echo "Running 'make install' with potentially incomplete PATH. Current PATH: $PATH"
echo "Attempting to find nix via 'command -v nix': $(command -v nix || echo 'nix not found in current PATH')"
echo "USER=$USER will be available to make install (exported)."
run_make_install
fi