Totally renewd, different endpoints for git and curl
This commit is contained in:
+89
-129
@@ -1,161 +1,121 @@
|
|||||||
#!/usr/bin/env bash
|
Create for me a script that does the following.
|
||||||
set -euo pipefail
|
The script should run on any nixos machine.
|
||||||
|
|
||||||
### ─────────────────────────────
|
For git use REPO_URL="https://gitea.data-pro.nu/henrov/nixos.git"
|
||||||
### Step 0 — Sudo preflight
|
For html use https://gitea.data-pro.nu/henrov/nixos/archive/main.zip
|
||||||
### ─────────────────────────────
|
|
||||||
command -v sudo >/dev/null 2>&1 || { echo "sudo required"; exit 1; }
|
|
||||||
|
|
||||||
echo "Requesting sudo..."
|
This script is really important, my job depends on it
|
||||||
sudo -v
|
|
||||||
|
|
||||||
( 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"
|
# DroidNix Installation — Step Specification
|
||||||
REPO_URL="https://gitea.data-pro.nu/henrov/nixos.git"
|
|
||||||
|
|
||||||
### ─────────────────────────────
|
## Step 1 — Collect system identity
|
||||||
### Step 2 — Prepare directory
|
|
||||||
### ─────────────────────────────
|
|
||||||
rm -rf "$TARGET_DIR"
|
|
||||||
mkdir -p "$TARGET_DIR"
|
|
||||||
|
|
||||||
### ─────────────────────────────
|
Store the current Unix username in a variable: `USER=$(whoami)`
|
||||||
### 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 hostname in a variable: `HOSTNAME=$(hostname)`
|
||||||
### Step 4 — Rename safely
|
|
||||||
### ─────────────────────────────
|
|
||||||
echo "Renaming files..."
|
|
||||||
|
|
||||||
find "$TARGET_DIR" -depth -name '*traveldroid*' -print0 | while IFS= read -r -d '' path; do
|
## Step 2 — Create target directory
|
||||||
mv "$path" "${path//traveldroid/$HOSTNAME_NAME}"
|
|
||||||
done
|
|
||||||
|
|
||||||
find "$TARGET_DIR" -depth -name '*henrov*' -print0 | while IFS= read -r -d '' path; do
|
Create the directory `~/Droidnix` if it does not already exist.
|
||||||
mv "$path" "${path//henrov/$USER_NAME}"
|
|
||||||
done
|
|
||||||
|
|
||||||
### ─────────────────────────────
|
## Step 3 — Download the repository
|
||||||
### Step 5 — Content replacement
|
|
||||||
### ─────────────────────────────
|
|
||||||
echo "Processing files..."
|
|
||||||
|
|
||||||
is_text() {
|
Download the contents of `https://gitea.data-pro.nu/henrov/nixos/src/branch/main/Droidnix` into `~/Droidnix` using the following priority:
|
||||||
file --mime "$1" | grep -q text
|
|
||||||
}
|
|
||||||
|
|
||||||
export USER_NAME HOSTNAME_NAME TARGET_DIR
|
- If `git` is available: `git clone <url> ~/Droidnix`
|
||||||
|
|
||||||
# generated directory
|
- Otherwise if `wget` is available: download the zip archive and extract it into `~/Droidnix`
|
||||||
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
|
|
||||||
|
|
||||||
README="$TARGET_DIR/README.org"
|
- Otherwise: use `curl` to download the zip archive and extract it into `~/Droidnix`
|
||||||
|
|
||||||
# org block replacement
|
## Step 4 — Rename files and folders
|
||||||
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
|
|
||||||
|
|
||||||
# global replacements
|
Perform the following renames recursively under `~/Droidnix`, processing deepest paths first to avoid conflicts:
|
||||||
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
|
|
||||||
|
|
||||||
### ─────────────────────────────
|
- **4.1** Rename any file or folder whose name contains `traveldroid`, replacing it with `$HOSTNAME`
|
||||||
### Step 6 — Hardware config
|
|
||||||
### ─────────────────────────────
|
|
||||||
HOST_DIR="$TARGET_DIR/generated/hosts/$HOSTNAME_NAME"
|
|
||||||
mkdir -p "$HOST_DIR"
|
|
||||||
|
|
||||||
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 \
|
## Step 5 — Replace strings in file contents
|
||||||
> "$HOST_DIR/hardware-configuration.nix"
|
|
||||||
|
|
||||||
### ─────────────────────────────
|
Perform the following text substitutions:
|
||||||
### Step 7 — Inject into README
|
|
||||||
### ─────────────────────────────
|
|
||||||
if [ -f "$README" ]; then
|
|
||||||
echo "Updating README.org..."
|
|
||||||
|
|
||||||
awk -v host="$HOSTNAME_NAME" -v file="$HOST_DIR/hardware-configuration.nix" '
|
- **5.1** In all files under `~/Droidnix/generated/`, replace every occurrence of `henrov` with `$USER`
|
||||||
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.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.
|
||||||
### Step 8 — Build system
|
|
||||||
### ─────────────────────────────
|
|
||||||
echo "Rebuilding NixOS..."
|
|
||||||
|
|
||||||
cd "$TARGET_DIR"
|
- **5.3** In all files under `~/Droidnix/` (recursively, excluding binary files), replace every occurrence of `~/Repos/nixos/Droidnix` with `~/Droidnix`
|
||||||
sudo nixos-rebuild switch --flake ".#$HOSTNAME_NAME"
|
|
||||||
|
|
||||||
### ─────────────────────────────
|
- **5.4.** In all files under `~/Droidnix`, replace every occurrence of `traveldroid` with `$HOSTNAME`
|
||||||
### Step 9 — Done
|
|
||||||
### ─────────────────────────────
|
|
||||||
cat <<EOF
|
|
||||||
|
|
||||||
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:
|
## Step 6 — Generate hardware configuration
|
||||||
~/Droidnix/README.org
|
|
||||||
|
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)
|
cd ~/Droidnix && sudo nixos-rebuild switch --flake .#$(hostname)
|
||||||
|
|
||||||
EOF
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user