Files
nixos/Droidnix/install_Droidnix.sh
T
2026-05-04 19:12:07 +02:00

180 lines
5.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
### ─────────────────────────────
### Configuration
### ─────────────────────────────
ZIP_URL="https://gitea.data-pro.nu/henrov/nixos/archive/main.zip"
TARGET_DIR="$HOME/Droidnix"
### ─────────────────────────────
### Step 1 — Identity
### ─────────────────────────────
USER_NAME="$(whoami)"
HOSTNAME="$(hostname)"
echo "User: $USER_NAME"
echo "Hostname: $HOSTNAME"
### ─────────────────────────────
### Step 2 — Create target directory
### ─────────────────────────────
cd ~
rm -rf "$TARGET_DIR"
mkdir -p "$TARGET_DIR"
### ─────────────────────────────
### Step 3 — Download repository (ZIP ONLY)
### ─────────────────────────────
TMP_DIR="$(mktemp -d)"
cleanup() {
rm -rf "$TMP_DIR"
}
trap cleanup EXIT
if command -v wget >/dev/null 2>&1; then
echo "Downloading with wget..."
wget -O "$TMP_DIR/main.zip" "$ZIP_URL"
elif command -v curl >/dev/null 2>&1; then
echo "Downloading with curl..."
curl -L "$ZIP_URL" -o "$TMP_DIR/main.zip"
else
echo "Error: need wget or curl"
exit 1
fi
echo "Extracting..."
unzip -q "$TMP_DIR/main.zip" -d "$TMP_DIR/unzipped"
# Gitea zip contains folder like: repo-main/
# Adjusting safely:
cp -r "$TMP_DIR"/unzipped/*/Droidnix/* "$TARGET_DIR/"
### ─────────────────────────────
### Step 4 — Rename files (deepest first)
### ─────────────────────────────
echo "Renaming files..."
find "$TARGET_DIR" -depth | while read -r path; do
dir=$(dirname "$path")
base=$(basename "$path")
newbase="$base"
newbase="${newbase//traveldroid/$HOSTNAME}"
newbase="${newbase//henrov/$USER_NAME}"
if [[ "$base" != "$newbase" ]]; then
mv "$path" "$dir/$newbase"
echo "$path --> $dir/$newbase"
fi
done
### ─────────────────────────────
### Step 5 — Replace file contents
### ─────────────────────────────
echo "Replacing content..."
# 5.1 generated/ henrov → user
if [ -d "$TARGET_DIR/generated" ]; then
find "$TARGET_DIR/generated" -type f -exec sed -i "s/henrov/$USER_NAME/g" {} +
fi
# 5.3 path replacement
find "$TARGET_DIR" -type f -exec grep -Iq . {} \; -print | while read -r file; do
sed -i "s#~/Repos/nixos/Droidnix#~/Droidnix#g" "$file" || true
done
# 5.4 traveldroid → hostname
find "$TARGET_DIR" -type f -exec grep -Iq . {} \; -print | while read -r file; do
sed -i "s/traveldroid/$HOSTNAME/g" "$file" || true
done
# README.org block replacement
README="$TARGET_DIR/README.org"
if [ -f "$README" ]; then
awk -v user="$USER_NAME" -v host="$HOSTNAME" '
BEGIN {inblock=0}
{
if ($0 ~ /^\*\* =/) inblock=1
if (inblock == 1) {
gsub("henrov", user)
gsub("traveldroid", host)
}
print $0
if ($0 ~ /^#\+END_SRC/) inblock=0
}' "$README" > "$README.tmp" && mv "$README.tmp" "$README"
fi
### ─────────────────────────────
### Step 6 — Hardware config
### ─────────────────────────────
HOST_DIR="$TARGET_DIR/generated/hosts/$HOSTNAME"
mkdir -p "$HOST_DIR"
echo "Generating hardware configuration..."
sudo nixos-generate-config --show-hardware-config > \
"$HOST_DIR/hardware-configuration.nix"
### ─────────────────────────────
### Step 7 — Inject hardware into README.org
### ─────────────────────────────
if [ -f "$README" ]; then
awk -v file="$HOST_DIR/hardware-configuration.nix" '
BEGIN {inblock=0}
{
if ($0 ~ /^#\+BEGIN_SRC nix :tangle generated\/hosts\/\$/) {
inblock=1
print $0
while ((getline line < file) > 0) {
print line
}
close(file)
next
}
if (inblock == 1 && $0 ~ /^#\+END_SRC/) {
inblock=0
}
if (inblock == 0) print $0
}' "$README" > "$README.tmp" && mv "$README.tmp" "$README"
fi
### ─────────────────────────────
### Step 8 — Build system
### ─────────────────────────────
echo "Building NixOS configuration..."
cd "$TARGET_DIR"
sudo nixos-rebuild switch --flake ".#$HOSTNAME"
### ─────────────────────────────
### Step 9 — Done
### ─────────────────────────────
cat <<EOF
Installation complete. Please reboot your system.
After rebooting, any changes must be made in:
~/Droidnix/README.org
Then rebuild with:
cd ~/Droidnix && sudo nixos-rebuild switch --flake .#$(hostname)
EOF