Totally renewd, different endpoints for git and curl

This commit is contained in:
2026-05-04 17:49:17 +02:00
parent 78ee510d83
commit 063972a5a8
+89 -129
View File
@@ -1,161 +1,121 @@
#!/usr/bin/env bash
set -euo pipefail
Create for me a script that does the following.
The script should run on any nixos machine.
### ─────────────────────────────
### Step 0 — Sudo preflight
### ─────────────────────────────
command -v sudo >/dev/null 2>&1 || { echo "sudo required"; exit 1; }
For git use REPO_URL="https://gitea.data-pro.nu/henrov/nixos.git"
For html use https://gitea.data-pro.nu/henrov/nixos/archive/main.zip
echo "Requesting sudo..."
sudo -v
This script is really important, my job depends on it
( while true; do sudo -n true; sleep 60; done ) &
SUDO_KEEPALIVE_PID=$!
trap 'kill $SUDO_KEEPALIVE_PID >/dev/null 2>&1 || true' EXIT
### ─────────────────────────────
### Step 1 — Identity
### ─────────────────────────────
USER_NAME="$(whoami)"
HOSTNAME_NAME="$(hostname)"
echo "User: $USER_NAME"
echo "Hostname: $HOSTNAME_NAME"
TARGET_DIR="$HOME/Droidnix"
REPO_URL="https://gitea.data-pro.nu/henrov/nixos.git"
# DroidNix Installation — Step Specification
### ─────────────────────────────
### Step 2 — Prepare directory
### ─────────────────────────────
rm -rf "$TARGET_DIR"
mkdir -p "$TARGET_DIR"
## Step 1 — Collect system identity
### ─────────────────────────────
### Step 3 — Clone repository (FIXED)
### ─────────────────────────────
if command -v git >/dev/null 2>&1; then
echo "Cloning via git..."
git clone "$REPO_URL" "$TARGET_DIR"
else
echo "ERROR: git is required (ZIP fallback removed because it was broken)"
exit 1
fi
Store the current Unix username in a variable: `USER=$(whoami)`
### ─────────────────────────────
### Step 4 — Rename safely
### ─────────────────────────────
echo "Renaming files..."
Store the current hostname in a variable: `HOSTNAME=$(hostname)`
find "$TARGET_DIR" -depth -name '*traveldroid*' -print0 | while IFS= read -r -d '' path; do
mv "$path" "${path//traveldroid/$HOSTNAME_NAME}"
done
## Step 2 — Create target directory
find "$TARGET_DIR" -depth -name '*henrov*' -print0 | while IFS= read -r -d '' path; do
mv "$path" "${path//henrov/$USER_NAME}"
done
Create the directory `~/Droidnix` if it does not already exist.
### ─────────────────────────────
### Step 5 — Content replacement
### ─────────────────────────────
echo "Processing files..."
## Step 3 — Download the repository
is_text() {
file --mime "$1" | grep -q text
}
Download the contents of `https://gitea.data-pro.nu/henrov/nixos/src/branch/main/Droidnix` into `~/Droidnix` using the following priority:
export USER_NAME HOSTNAME_NAME TARGET_DIR
- If `git` is available: `git clone <url> ~/Droidnix`
# generated directory
if [ -d "$TARGET_DIR/generated" ]; then
find "$TARGET_DIR/generated" -type f -print0 | while IFS= read -r -d '' f; do
is_text "$f" && sed -i "s/henrov/$USER_NAME/g" "$f"
done
fi
- Otherwise if `wget` is available: download the zip archive and extract it into `~/Droidnix`
README="$TARGET_DIR/README.org"
- Otherwise: use `curl` to download the zip archive and extract it into `~/Droidnix`
# org block replacement
if [ -f "$README" ]; then
awk -v user="$USER_NAME" -v host="$HOSTNAME_NAME" '
BEGIN { inblock=0 }
/^\*\* =/ { inblock=1 }
/^\#\+END_SRC/ { inblock=0 }
{
if (inblock) {
gsub(/henrov/, user)
gsub(/traveldroid/, host)
}
print
}' "$README" > "$README.tmp" && mv "$README.tmp" "$README"
fi
## Step 4 — Rename files and folders
# global replacements
find "$TARGET_DIR" -type f -print0 | while IFS= read -r -d '' f; do
is_text "$f" || continue
sed -i \
-e "s|~/Repos/nixos/Droidnix|~/Droidnix|g" \
-e "s/traveldroid/$HOSTNAME_NAME/g" \
"$f"
done
Perform the following renames recursively under `~/Droidnix`, processing deepest paths first to avoid conflicts:
### ─────────────────────────────
### Step 6 — Hardware config
### ─────────────────────────────
HOST_DIR="$TARGET_DIR/generated/hosts/$HOSTNAME_NAME"
mkdir -p "$HOST_DIR"
- **4.1** Rename any file or folder whose name contains `traveldroid`, replacing it with `$HOSTNAME`
echo "Generating hardware config..."
- **4.2** Rename any file or folder whose name contains `henrov`, replacing it with `$USER`
sudo nixos-generate-config --show-hardware-config \
> "$HOST_DIR/hardware-configuration.nix"
## Step 5 — Replace strings in file contents
### ─────────────────────────────
### Step 7 — Inject into README
### ─────────────────────────────
if [ -f "$README" ]; then
echo "Updating README.org..."
Perform the following text substitutions:
awk -v host="$HOSTNAME_NAME" -v file="$HOST_DIR/hardware-configuration.nix" '
BEGIN {
target = "#+BEGIN_SRC nix :tangle generated/hosts/" host "/hardware-configuration.nix :noweb yes :mkdirp yes :eval never"
inblock=0
}
$0 == target {
print
while ((getline line < file) > 0) print line
inblock=1
next
}
inblock && $0 == "#+END_SRC" {
print
inblock=0
next
}
!inblock { print }
' "$README" > "$README.tmp" && mv "$README.tmp" "$README"
fi
- **5.1** In all files under `~/Droidnix/generated/`, replace every occurrence of `henrov` with `$USER`
### ─────────────────────────────
### Step 8 — Build system
### ─────────────────────────────
echo "Rebuilding NixOS..."
- **5.2** In `~/Droidnix/[README.org](http://README.org)`, replace every occurrence of `henrov` with `$USER`, but **only within blocks** that begin with the string `** =` and end with a line matching `#+END_SRC`. Leave all text outside these blocks unchanged.
cd "$TARGET_DIR"
sudo nixos-rebuild switch --flake ".#$HOSTNAME_NAME"
- **5.3** In all files under `~/Droidnix/` (recursively, excluding binary files), replace every occurrence of `~/Repos/nixos/Droidnix` with `~/Droidnix`
### ─────────────────────────────
### Step 9 — Done
### ─────────────────────────────
cat <<EOF
- **5.4.** In all files under `~/Droidnix`, replace every occurrence of `traveldroid` with `$HOSTNAME`
Installation complete. Reboot recommended.
- **5.6** In `~/Droidnix/[README.org](http://README.org)`, replace every occurrence of `traveldroid` with `$HOSTNAME`, but **only within blocks** that begin with a line matching `** =` and end with a line matching `#+END_SRC`
Edit config:
~/Droidnix/README.org
## Step 6 — Generate hardware configuration
Create the directory `~/Droidnix/generated/hosts/$HOSTNAME/` if it does not already exist.
Run the following command and write its output to `~/Droidnix/generated/hosts/$HOSTNAME/hardware-configuration.nix`:
```bash
sudo nixos-generate-config --show-hardware-config
```
## Step 7 — Update [README.org](http://README.org) with generated hardware configuration
In `~/Droidnix/[README.org](http://README.org)`, locate the block that begins with exactly this line:
```
#+BEGIN_SRC nix :tangle generated/hosts/$HOSTNAME/hardware-configuration.nix :noweb yes :mkdirp yes :eval never
```
and ends with the next line containing exactly `#+END_SRC`.
Replace everything between (but not including) those two boundary lines with the full contents of `~/Droidnix/generated/hosts/$HOSTNAME/hardware-configuration.nix`.
## Step 8 — Build and activate the NixOS configuration
Change directory to `~/Droidnix` and run:
```bash
sudo nixos-rebuild switch --flake .#$HOSTNAME
```
## Step 9 — Inform the user
Print the following message:
```
Installation complete. Please reboot your system.
After rebooting, any changes to your NixOS configuration must be made in
~/Droidnix/[README.org](http://README.org). Before rebuilding, first tangle the org file using:
emacs --batch --no-init-file ~/Droidnix/[README.org](http://README.org) \
--eval "(require 'org)" \
--eval "(require 'ob-tangle)" \
--eval "(find-file \"[README.org](http://README.org)\")" \
--eval "(with-temp-message \"\" (org-babel-tangle))" \
--eval "(with-temp-message \"\" (org-html-export-to-html))"
Then rebuild with:
Rebuild:
cd ~/Droidnix && sudo nixos-rebuild switch --flake .#$(hostname)
EOF
```