From 7198af72e3490015cb229f75abaf0e1b9a3108f0 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 14 Nov 2026 04:18:14 +0000 Subject: populate: populate shell, running shell code on a target populate shell [OPTION]... TARGET SCRIPT runs SCRIPT with /bin/sh on TARGET, reached like populate reaches it, deciding whether it's local when it runs, with local or remote as $1, so SCRIPT can tell. It takes populate's target options but --path, and --tty, giving SCRIPT a terminal on a remote target, and --local-only, failing before connecting unless the target is local. It replaces itself with the command running SCRIPT, so the exit status, the terminal, and signals are SCRIPT's. krops can run nixos-rebuild with it, which needs whether the target is local, unless it decides that during evaluation, which pure evaluation can't. The command line's parsing is shared between populate and populate shell. --- pkgs/populate/src/shell.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 pkgs/populate/src/shell.rs (limited to 'pkgs/populate/src/shell.rs') diff --git a/pkgs/populate/src/shell.rs b/pkgs/populate/src/shell.rs new file mode 100644 index 0000000..befd4aa --- /dev/null +++ b/pkgs/populate/src/shell.rs @@ -0,0 +1,72 @@ +//! populate shell: runs shell code on a target, reached like populate +//! reaches it. + +use crate::cli::ShellOptions; +use crate::run::{self, Origin}; +use std::os::unix::process::CommandExt; +use std::process::{Command, ExitCode}; + +/// Runs the script as the options say, replacing populate, so the script's +/// exit status, terminal, and signals are populate's. Returns only if +/// that fails. +pub fn shell(options: ShellOptions) -> ExitCode { + let origin = Origin::from_env(); + let target = options + .target + .complete(&origin, options.sudo, options.ssh_args); + let local = options.local.unwrap_or_else(|| origin.is_local(&target)); + if options.local_only && !local { + let user = target + .user + .as_deref() + .map(|user| format!("{user}@")) + .unwrap_or_default(); + eprintln!( + "populate: shell: {user}{} is not the machine populate runs on", + target.host + ); + return ExitCode::FAILURE; + } + let mut words: Vec = Vec::new(); + if target.sudo { + words.push("sudo".to_string()); + } + words.extend([ + "/bin/sh".to_string(), + "-c".to_string(), + options.script, + "sh".to_string(), + if local { "local" } else { "remote" }.to_string(), + ]); + let mut command = if local { + let mut command = Command::new(&words[0]); + command.args(&words[1..]); + command + } else { + let mut command = Command::new("ssh"); + if let Some(user) = &target.user { + command.args(["-l", user]); + } + if let Some(port) = &target.port { + command.args(["-p", port]); + } + command + .arg(if options.tty { "-t" } else { "-T" }) + .args(&target.extra_options) + .arg(&target.host) + .arg( + words + .iter() + .map(|word| run::quote(word)) + .collect::>() + .join(" "), + ); + command + }; + let error = command.exec(); + eprintln!( + "populate: shell: can't run {}: {error}", + command.get_program().to_string_lossy() + ); + ExitCode::from(127) +} -- cgit v1.3.1