Rewirtiung, copiued the wrong stuff <blush>.
Rewrite with endpoints
This commit is contained in:
+133
-72
@@ -1,121 +1,182 @@
|
|||||||
Create for me a script that does the following.
|
#!/usr/bin/env bash
|
||||||
The script should run on any nixos machine.
|
set -euo pipefail
|
||||||
|
|
||||||
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
|
### Configuration
|
||||||
|
### ─────────────────────────────
|
||||||
|
REPO_URL="https://gitea.data-pro.nu/henrov/nixos.git"
|
||||||
|
ZIP_URL="https://gitea.data-pro.nu/henrov/nixos/archive/main.zip"
|
||||||
|
|
||||||
This script is really important, my job depends on it
|
TARGET_DIR="$HOME/Droidnix"
|
||||||
|
|
||||||
|
### ─────────────────────────────
|
||||||
|
### Step 1 — Identity
|
||||||
|
### ─────────────────────────────
|
||||||
|
USER_NAME="$(whoami)"
|
||||||
|
HOSTNAME="$(hostname)"
|
||||||
|
|
||||||
|
echo "User: $USER_NAME"
|
||||||
|
echo "Hostname: $HOSTNAME"
|
||||||
|
|
||||||
|
### ─────────────────────────────
|
||||||
|
### Step 2 — Create target directory
|
||||||
|
### ─────────────────────────────
|
||||||
|
mkdir -p "$TARGET_DIR"
|
||||||
|
|
||||||
|
### ─────────────────────────────
|
||||||
|
### Step 3 — Download repository
|
||||||
|
### ─────────────────────────────
|
||||||
|
TMP_DIR="$(mktemp -d)"
|
||||||
|
|
||||||
# DroidNix Installation — Step Specification
|
cleanup() {
|
||||||
|
rm -rf "$TMP_DIR"
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
## Step 1 — Collect system identity
|
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/"
|
||||||
|
|
||||||
Store the current Unix username in a variable: `USER=$(whoami)`
|
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/"
|
||||||
|
|
||||||
Store the current hostname in a variable: `HOSTNAME=$(hostname)`
|
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/"
|
||||||
|
|
||||||
## Step 2 — Create target directory
|
else
|
||||||
|
echo "Error: need git, wget, or curl"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
Create the directory `~/Droidnix` if it does not already exist.
|
### ─────────────────────────────
|
||||||
|
### Step 4 — Rename files (deepest first)
|
||||||
|
### ─────────────────────────────
|
||||||
|
echo "Renaming files..."
|
||||||
|
|
||||||
## Step 3 — Download the repository
|
find "$TARGET_DIR" -depth | while read -r path; do
|
||||||
|
dir=$(dirname "$path")
|
||||||
|
base=$(basename "$path")
|
||||||
|
|
||||||
Download the contents of `https://gitea.data-pro.nu/henrov/nixos/src/branch/main/Droidnix` into `~/Droidnix` using the following priority:
|
newbase="$base"
|
||||||
|
|
||||||
- If `git` is available: `git clone <url> ~/Droidnix`
|
newbase="${newbase//traveldroid/$HOSTNAME}"
|
||||||
|
newbase="${newbase//henrov/$USER_NAME}"
|
||||||
|
|
||||||
- Otherwise if `wget` is available: download the zip archive and extract it into `~/Droidnix`
|
if [[ "$base" != "$newbase" ]]; then
|
||||||
|
mv "$path" "$dir/$newbase"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
- Otherwise: use `curl` to download the zip archive and extract it into `~/Droidnix`
|
### ─────────────────────────────
|
||||||
|
### Step 5 — Replace file contents
|
||||||
|
### ─────────────────────────────
|
||||||
|
|
||||||
## Step 4 — Rename files and folders
|
echo "Replacing content..."
|
||||||
|
|
||||||
Perform the following renames recursively under `~/Droidnix`, processing deepest paths first to avoid conflicts:
|
# 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
|
||||||
|
|
||||||
- **4.1** Rename any file or folder whose name contains `traveldroid`, replacing it with `$HOSTNAME`
|
# 5.3 path replacement (skip binaries loosely)
|
||||||
|
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
|
||||||
|
|
||||||
- **4.2** Rename any file or folder whose name contains `henrov`, replacing it with `$USER`
|
# 5.4 traveldroid → hostname everywhere
|
||||||
|
find "$TARGET_DIR" -type f -exec grep -Iq . {} \; -print | while read -r file; do
|
||||||
|
sed -i "s/traveldroid/$HOSTNAME/g" "$file" || true
|
||||||
|
done
|
||||||
|
|
||||||
## Step 5 — Replace strings in file contents
|
# 5.2 & 5.6 README.org block replacement
|
||||||
|
README="$TARGET_DIR/README.org"
|
||||||
|
|
||||||
Perform the following text substitutions:
|
if [ -f "$README" ]; then
|
||||||
|
awk -v user="$USER_NAME" -v host="$HOSTNAME" '
|
||||||
|
BEGIN {inblock=0}
|
||||||
|
|
||||||
- **5.1** In all files under `~/Droidnix/generated/`, replace every occurrence of `henrov` with `$USER`
|
{
|
||||||
|
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
|
||||||
|
|
||||||
- **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 6 — Hardware config
|
||||||
|
### ─────────────────────────────
|
||||||
|
HOST_DIR="$TARGET_DIR/generated/hosts/$HOSTNAME"
|
||||||
|
mkdir -p "$HOST_DIR"
|
||||||
|
|
||||||
- **5.3** In all files under `~/Droidnix/` (recursively, excluding binary files), replace every occurrence of `~/Repos/nixos/Droidnix` with `~/Droidnix`
|
echo "Generating hardware configuration..."
|
||||||
|
sudo nixos-generate-config --show-hardware-config > \
|
||||||
|
"$HOST_DIR/hardware-configuration.nix"
|
||||||
|
|
||||||
- **5.4.** In all files under `~/Droidnix`, replace every occurrence of `traveldroid` with `$HOSTNAME`
|
### ─────────────────────────────
|
||||||
|
### Step 7 — Inject hardware into README.org
|
||||||
|
### ─────────────────────────────
|
||||||
|
|
||||||
- **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`
|
if [ -f "$README" ]; then
|
||||||
|
awk -v file="$HOST_DIR/hardware-configuration.nix" '
|
||||||
|
BEGIN {inblock=0}
|
||||||
|
|
||||||
## Step 6 — Generate hardware configuration
|
{
|
||||||
|
if ($0 ~ /^#\+BEGIN_SRC nix :tangle generated\/hosts\/\$/) {
|
||||||
|
inblock=1
|
||||||
|
print $0
|
||||||
|
|
||||||
Create the directory `~/Droidnix/generated/hosts/$HOSTNAME/` if it does not already exist.
|
while ((getline line < file) > 0) {
|
||||||
|
print line
|
||||||
|
}
|
||||||
|
close(file)
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
Run the following command and write its output to `~/Droidnix/generated/hosts/$HOSTNAME/hardware-configuration.nix`:
|
if (inblock == 1 && $0 ~ /^#\+END_SRC/) {
|
||||||
|
inblock=0
|
||||||
|
}
|
||||||
|
|
||||||
```bash
|
if (inblock == 0) print $0
|
||||||
|
}' "$README" > "$README.tmp" && mv "$README.tmp" "$README"
|
||||||
|
fi
|
||||||
|
|
||||||
sudo nixos-generate-config --show-hardware-config
|
### ─────────────────────────────
|
||||||
|
### Step 8 — Build system
|
||||||
|
### ─────────────────────────────
|
||||||
|
cd "$TARGET_DIR"
|
||||||
|
sudo nixos-rebuild switch --flake ".#$HOSTNAME"
|
||||||
|
|
||||||
```
|
### ─────────────────────────────
|
||||||
|
### Step 9 — Done
|
||||||
## Step 7 — Update [README.org](http://README.org) with generated hardware configuration
|
### ─────────────────────────────
|
||||||
|
cat <<EOF
|
||||||
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.
|
Installation complete. Please reboot your system.
|
||||||
|
|
||||||
After rebooting, any changes to your NixOS configuration must be made in
|
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:
|
~/Droidnix/README.org. Before rebuilding, first tangle the org file using:
|
||||||
|
|
||||||
emacs --batch --no-init-file ~/Droidnix/[README.org](http://README.org) \
|
|
||||||
|
|
||||||
|
emacs --batch --no-init-file ~/Droidnix/README.org \
|
||||||
--eval "(require 'org)" \
|
--eval "(require 'org)" \
|
||||||
|
|
||||||
--eval "(require 'ob-tangle)" \
|
--eval "(require 'ob-tangle)" \
|
||||||
|
--eval "(find-file \"README.org\")" \
|
||||||
--eval "(find-file \"[README.org](http://README.org)\")" \
|
|
||||||
|
|
||||||
--eval "(with-temp-message \"\" (org-babel-tangle))" \
|
--eval "(with-temp-message \"\" (org-babel-tangle))" \
|
||||||
|
|
||||||
--eval "(with-temp-message \"\" (org-html-export-to-html))"
|
--eval "(with-temp-message \"\" (org-html-export-to-html))"
|
||||||
|
|
||||||
Then rebuild with:
|
Then rebuild with:
|
||||||
|
|
||||||
cd ~/Droidnix && sudo nixos-rebuild switch --flake .#$(hostname)
|
cd ~/Droidnix && sudo nixos-rebuild switch --flake .#$(hostname)
|
||||||
|
|
||||||
```
|
EOF
|
||||||
|
|||||||
Reference in New Issue
Block a user