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

195 lines
6.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
### ─────────────────────────────
### Configuration
### ─────────────────────────────
REPO_URL="https://gitea.data-pro.nu/henrov/nixos.git"
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
### ─────────────────────────────
TMP_DIR="$(mktemp -d)"
cleanup() {
rm -rf "$TMP_DIR"
}
trap cleanup EXIT
if command -v git >/dev/null 2>&1; then
echo "Using git clone..."
git clone "$REPO_URL" "$TMP_DIR/repo"
cp -r "$TMP_DIR/repo/Droidnix/"* "$TARGET_DIR/"
elif command -v wget >/dev/null 2>&1; then
echo "Using wget..."
wget -O "$TMP_DIR/main.zip" "$ZIP_URL"
unzip -q "$TMP_DIR/main.zip" -d "$TMP_DIR/unzipped"
cp -r "$TMP_DIR/unzipped"/*/Droidnix/* "$TARGET_DIR/"
elif command -v curl >/dev/null 2>&1; then
echo "Using curl..."
curl -L "$ZIP_URL" -o "$TMP_DIR/main.zip"
unzip -q "$TMP_DIR/main.zip" -d "$TMP_DIR/unzipped"
cp -r "$TMP_DIR/unzipped"/*/Droidnix/* "$TARGET_DIR/"
else
echo "Error: need git, wget, or curl"
exit 1
fi
### ─────────────────────────────
### 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
echo "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 (skip binaries loosely)
echo "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 everywhere
echo "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
# 5.2 & 5.6 README.org block replacement
echo "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"
echo "$HOST_DIR/hardware-configuration.nix"
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
### ─────────────────────────────
echo "$HOST_DIR/hardware-configuration.nix"
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 "sudo nixos-rebuild switch --flake .#$HOSTNAME in $TARGET_DIR"
cd "$TARGET_DIR"
git init main
git -C "/home/$USER/Droidnix" add "flake.nix"
sudo nixos-rebuild switch --flake ".#$HOSTNAME"
### ─────────────────────────────
### Step 9 — Done
### ─────────────────────────────
cat <<EOF
Installation complete. Please reboot your system.
After rebooting, any changes to your NixOS configuration must be made in
~/Droidnix/README.org. Before rebuilding, first tangle the org file using:
emacs --batch --no-init-file ~/Droidnix/README.org \
--eval "(require 'org)" \
--eval "(require 'ob-tangle)" \
--eval "(find-file \"README.org\")" \
--eval "(with-temp-message \"\" (org-babel-tangle))" \
--eval "(with-temp-message \"\" (org-html-export-to-html))"
Then rebuild with:
cd ~/Droidnix && sudo nixos-rebuild switch --flake .#$(hostname)
EOF