garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

cartopolis-agent · 2026-11-21T20:36:59Z

feat(setup): setup installs the binary it runs from, and says what comes next

Setting up a node took five commands and a musl target before the one
that mattered: the guide had the owner build static, `install -D` the
binary under /var/lib/midas/bin and call it by its full path. `sudo
target/release/midas setup box-1 --hub …` now copies itself there (as
`hub setup` already did to /usr/local/bin) and goes on from there, so
the guide is build, then setup. The hub guide loses the install step
the same way; musl stays as the aside it is (for nodes whose containers
have an older C library than the hub). `hub setup` ends by naming the
first node to add when there is none, beside the deploy key, callers
and agent keys it already asked for.
docs/hub.md     | 26 +++++++++++++++++++-------
 docs/node.md    | 22 +++++++++-------------
 src/hubsetup.rs |  5 +++++
 src/setup.rs    | 29 +++++++++++++++++++++++++----
 4 files changed, 58 insertions(+), 24 deletions(-)

diff --git a/docs/hub.md b/docs/hub.md
index 404324c..2748bba 100644
--- a/docs/hub.md
+++ b/docs/hub.md
@@ -5,22 +5,30 @@ keeps the workspace's state, answers everybody over HTTP, mirrors the git
 repository for the nodes (nodes never get git credentials of their own) and
 keeps free nodes warm. Agents usually run on the same machine.
 
-## 1. Build and install midas
+## 1. Build midas
 
-From a commit you have read; musl, so the binary runs anywhere:
+From a commit you have read, with Rust and [Bun](https://bun.sh) installed:
 
 ```bash
 git clone <the midas repository> && cd midas
-rustup target add x86_64-unknown-linux-musl
-(cd web && bun install && bun run build)   # the web page, which the binary carries
-cargo build --release --target x86_64-unknown-linux-musl
-sudo install -D -m 755 target/x86_64-unknown-linux-musl/release/midas /var/lib/midas/bin/midas
+(cd web && bun install --frozen-lockfile && bun run build)   # the web page, which the binary carries
+cargo build --release
 ```
 
+Nodes on other machines take this build from the hub when it runs there too
+(`midas node update`), so when their containers may have an older C library
+than this machine, build it static instead: `rustup target add
+x86_64-unknown-linux-musl` and `cargo build --release --target
+x86_64-unknown-linux-musl` (the binary is then under
+`target/x86_64-unknown-linux-musl/release/`).
+
 ## 2. Set it up
 
+Setup installs the binary it runs from as `/usr/local/bin/midas`, so it is run
+straight from the build:
+
 ```bash
-sudo /var/lib/midas/bin/midas hub setup \
+sudo target/release/midas hub setup \
     --listen 10.44.0.1:4791 \
     --forgejo ssh://git@git.example.org:2222 \
     --forgejo-api https://git.example.org/api/v1 \
@@ -28,6 +36,10 @@ sudo /var/lib/midas/bin/midas hub setup \
     --agent-key "ssh-ed25519 AAAA… agents"
 ```
 
+It ends by saying what is still missing — the deploy key to add, the callers
+and agent keys to make, the first node to add — so the rest of this guide can
+be read as needed.
+
 | option | what |
 |---|---|
 | `--listen` | this machine's WireGuard address, and a port for the hub |
diff --git a/docs/node.md b/docs/node.md
index 51581b7..a6eef9d 100644
--- a/docs/node.md
+++ b/docs/node.md
@@ -76,35 +76,31 @@ code that ever runs on your machine:
 
 ```bash
 git clone <the midas repository> && cd midas
-rustup target add x86_64-unknown-linux-musl
-cargo build --release --target x86_64-unknown-linux-musl
-sudo install -D -m 755 target/x86_64-unknown-linux-musl/release/midas /var/lib/midas/bin/midas
+cargo build --release
 ```
 
-It goes where only root can change it, because root runs it. Until setup has
-run, call it by that full path; setup then makes `midas` a command (step 2).
-
-**On SteamOS**, build inside a distrobox and install outside it (the distrobox
-shares your home; the system it does not):
+The page the hub serves is not needed here; a build without Bun is fine.
+**On SteamOS**, build inside a distrobox (the distrobox shares your home) and
+run setup outside it, where sudo reaches the system:
 
 ```bash
 distrobox enter arch
 sudo pacman -S --needed rustup git && rustup default stable
-rustup target add x86_64-unknown-linux-musl
-cd ~/midas && cargo build --release --target x86_64-unknown-linux-musl
+cd ~/midas && cargo build --release
 exit
-sudo install -D -m 755 ~/midas/target/x86_64-unknown-linux-musl/release/midas /var/lib/midas/bin/midas
 ```
 
 ## 2. Set it up
 
 ```bash
-sudo /var/lib/midas/bin/midas setup box-1 --hub 10.44.0.1:4791
+sudo target/release/midas setup box-1 --hub 10.44.0.1:4791
 ```
 
 `box-1` stands for your node's name as the hub knows it, and `10.44.0.1:4791`
 is the hub, never your own machine's address. The hub's address is needed the
-first time only.
+first time only. Setup copies the binary it runs from to
+`/var/lib/midas/bin/midas` — where only root can change it, because root runs
+it; writable on SteamOS, whose `/usr` is not — and runs from there on.
 
 Setup also makes `midas` a command: `/etc/profile.d/midas.sh` puts
 `/var/lib/midas/bin` on every login shell's PATH (log in again, or
diff --git a/src/hubsetup.rs b/src/hubsetup.rs
index e6e57a4..39678e2 100644
--- a/src/hubsetup.rs
+++ b/src/hubsetup.rs
@@ -298,6 +298,11 @@ pub fn setup(mut a: Args) -> Result<i32> {
     if c.agent_keys.is_empty() {
         println!("\nNo agent keys yet: nodes will only let the hub in. Add the agents' public key with\n  sudo midas hub setup --agent-key \"ssh-ed25519 AAAA…\"");
     }
+    if crate::registry::nodes().is_empty() {
+        println!("\nNo build nodes yet. Name the first one here, then set it up on its machine (docs/node.md):\n  \
+                  midas nodes add box-1 --machine box --address <its WireGuard address>\n  \
+                  sudo midas setup box-1 --hub {}      # on that machine, by its owner", c.listen);
+    }
     Ok(0)
 }
 
diff --git a/src/setup.rs b/src/setup.rs
index 183c79b..89fdbd1 100644
--- a/src/setup.rs
+++ b/src/setup.rs
@@ -559,13 +559,34 @@ fn profile_snippet() -> String {
              case \":$PATH:\" in\n    *:{d}:*) ;;\n    *) PATH=\"{d}:$PATH\"; export PATH ;;\nesac\n", d = INSTALLED_DIR)
 }
 
+/// The binary this runs as, at /var/lib/midas/bin/midas: a setup run from a
+/// build directory (`sudo target/release/midas setup …`) installs itself, so
+/// the owner never copies it by hand. Byte-equal already: nothing to do.
+fn install_self() -> Result<()> {
+    let me = std::env::current_exe()?;
+    if me == Path::new(INSTALLED) {
+        return Ok(());
+    }
+    let bytes = fs::read(&me)?;
+    if fs::read(INSTALLED).is_ok_and(|b| b == bytes) {
+        return Ok(());
+    }
+    fs::create_dir_all(INSTALLED_DIR)?;
+    let tmp = format!("{INSTALLED}.new");
+    fs::write(&tmp, &bytes)?;
+    fs::set_permissions(&tmp, fs::Permissions::from_mode(0o755))?;
+    fs::rename(&tmp, INSTALLED)?;
+    println!("installed {INSTALLED}");
+    Ok(())
+}
+
 /// Make `midas` a command for the machine's owner: in every login shell's PATH
 /// and, where /usr/local is writable, for sudo too, whose secure_path never
-/// holds /var/lib/midas/bin. Only for the installed binary — a setup run from
-/// a build directory or a temporary copy (deploy.sh does that) links nothing,
-/// and a real file at /usr/local/bin/midas (the hub's own) is left alone.
+/// holds /var/lib/midas/bin. A real file at /usr/local/bin/midas (the hub's
+/// own) is left alone.
 fn put_on_path() {
-    if std::env::current_exe().ok().as_deref() != Some(Path::new(INSTALLED)) {
+    if let Err(e) = install_self() {
+        println!("note: could not install {INSTALLED} ({e:#}); midas stays where you built it");
         return;
     }
     let macos = cfg!(target_os = "macos");