Phase 3c: nixpkgs-unstable for claude-code; harness calls claude --print, falls back to echo

This commit is contained in:
müde 2026-05-14 22:26:14 +02:00
parent 2fe9e91005
commit 6e7fd2e897
6 changed files with 106 additions and 45 deletions

View file

@ -37,33 +37,10 @@ pub async fn spawn(name: &str, agent_flake: &str, agent_dir: &Path) -> Result<()
validate(name)?;
let container = container_name(name);
run(&["create", &container, "--flake", agent_flake]).await?;
set_bind_flag(&container, agent_dir)?;
set_nspawn_flags(&container, agent_dir)?;
run(&["start", &container]).await
}
/// `nixos-container` doesn't expose `--bind` on the CLI, but its start script
/// expands `$EXTRA_NSPAWN_FLAGS` (from `/etc/nixos-containers/<name>.conf`)
/// unquoted into the `systemd-nspawn` invocation. Idempotently replace the
/// `EXTRA_NSPAWN_FLAGS` line with the bind we want.
fn set_bind_flag(container: &str, agent_dir: &Path) -> Result<()> {
let path = format!("/etc/nixos-containers/{container}.conf");
let original = std::fs::read_to_string(&path).with_context(|| format!("read {path}"))?;
let mut lines: Vec<String> = original
.lines()
.filter(|line| !line.trim_start().starts_with("EXTRA_NSPAWN_FLAGS="))
.map(str::to_owned)
.collect();
lines.push(format!(
"EXTRA_NSPAWN_FLAGS=\"--bind={}:{CONTAINER_RUNTIME_MOUNT}\"",
agent_dir.display()
));
let mut content = lines.join("\n");
content.push('\n');
std::fs::write(&path, content).with_context(|| format!("write {path}"))?;
tracing::info!(%path, "set EXTRA_NSPAWN_FLAGS for bind mount");
Ok(())
}
pub async fn kill(name: &str) -> Result<()> {
validate(name)?;
let container = container_name(name);
@ -73,7 +50,7 @@ pub async fn kill(name: &str) -> Result<()> {
pub async fn rebuild(name: &str, agent_flake: &str, agent_dir: &Path) -> Result<()> {
validate(name)?;
let container = container_name(name);
set_bind_flag(&container, agent_dir)?;
set_nspawn_flags(&container, agent_dir)?;
run(&["update", &container, "--flake", agent_flake]).await?;
// Restart so any nspawn-level changes (bind mounts, networking, etc.) apply.
run(&["stop", &container]).await?;
@ -101,6 +78,29 @@ pub async fn list() -> Result<Vec<String>> {
.collect())
}
/// Idempotently rewrite the `EXTRA_NSPAWN_FLAGS` line in
/// `/etc/nixos-containers/<container>.conf`. The start script expands this
/// variable unquoted into the `systemd-nspawn` command.
fn set_nspawn_flags(container: &str, agent_dir: &Path) -> Result<()> {
let path = format!("/etc/nixos-containers/{container}.conf");
let original = std::fs::read_to_string(&path).with_context(|| format!("read {path}"))?;
let flag = format!(
"EXTRA_NSPAWN_FLAGS=\"--bind={}:{CONTAINER_RUNTIME_MOUNT}\"",
agent_dir.display()
);
let mut lines: Vec<String> = original
.lines()
.filter(|line| !line.trim_start().starts_with("EXTRA_NSPAWN_FLAGS="))
.map(str::to_owned)
.collect();
lines.push(flag);
let mut content = lines.join("\n");
content.push('\n');
std::fs::write(&path, content).with_context(|| format!("write {path}"))?;
tracing::info!(%path, "set EXTRA_NSPAWN_FLAGS");
Ok(())
}
async fn run(args: &[&str]) -> Result<()> {
let out = Command::new("nixos-container")
.args(args)