aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-20 05:19:21 +0000
committertv <tv@krebsco.de>2026-11-20 05:19:21 +0000
commite81592e76f48c850a0ef35b8048260c21306a4b2 (patch)
tree91ed172c077c7532ee39757578ec8630f6833600 /pkgs
parent2aa54d9cb0399371a72a768cb5657a2bfebfde9e (diff)
populate: get what the target lacks from its <nixpkgs>krops-2
populate runs findutils, flock, rsync, patch, and Git on the target, as the sources need them. check looks for those first, and if any isn't there, it warns, and gets their packages from the target's <nixpkgs> with one nix-build, whose bin directories commands on the target, and rsync's remote end, find commands in before those in their PATH, for the rest of the run. If the target is the machine populate runs on, that goes for populate as well.
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/populate/README.md8
-rw-r--r--pkgs/populate/src/populate.rs37
-rw-r--r--pkgs/populate/src/rsync.rs12
-rw-r--r--pkgs/populate/src/run.rs35
-rw-r--r--pkgs/populate/src/sh.rs40
5 files changed, 115 insertions, 17 deletions
diff --git a/pkgs/populate/README.md b/pkgs/populate/README.md
index b42d987..3a154b6 100644
--- a/pkgs/populate/README.md
+++ b/pkgs/populate/README.md
@@ -28,10 +28,12 @@ fetches the program's dependencies from [crates.io](https://crates.io).
populate runs rsync, ssh, Git, Nix, pass, and passage as the sources need
them, found in `PATH`, as well as coreutils and findutils, and a POSIX shell
as `/bin/sh`.
-The target needs a POSIX shell as `/bin/sh`, coreutils, findutils, `flock`, rsync,
-`patch` for sources with `patches`, Git for `git` sources, and Nix, and, if
+The target needs a POSIX shell as `/bin/sh`, coreutils, Nix, and, if
remote, ssh access as root, or, with `--sudo`, as a user who may use
-`sudo`.
+`sudo`. populate also runs findutils, `flock`, rsync, `patch` for sources
+with `patches`, and Git for `git` sources there. If the target lacks any
+of those, populate warns, and gets them from `<nixpkgs>` there with
+`nix-build`, once for each run.
NixOS has all of them but Git, and rsync as long as
`environment.defaultPackages` contains it. Features that say so need Nix
with flake support (Nix 2.4 or later) on the machine that uses them.
diff --git a/pkgs/populate/src/populate.rs b/pkgs/populate/src/populate.rs
index 0366c53..322708a 100644
--- a/pkgs/populate/src/populate.rs
+++ b/pkgs/populate/src/populate.rs
@@ -12,7 +12,31 @@ use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
-/// Checks the sentinel, takes the lock on the target directory, makes a
+/// The commands populate runs on the target to deliver spec's sources, each
+/// followed by the attribute name in nixpkgs of the package that has it.
+fn tools(spec: &Spec) -> Vec<&'static str> {
+ let sources = || spec.source.values();
+ let mut tools = vec!["find", "findutils", "flock", "util-linux"];
+ if sources().any(|s| !matches!(s.kind.as_str(), "derivation" | "git" | "symlink")) {
+ tools.extend(["rsync", "rsync"]);
+ }
+ if sources().any(|s| s.kind == "git") {
+ tools.extend(["git", "git"]);
+ }
+ let patched = |s: &&Source| {
+ s.settings
+ .get("patches")
+ .and_then(Value::as_array)
+ .is_some_and(|patches| !patches.is_empty())
+ };
+ if sources().any(|s| patched(&s)) {
+ tools.extend(["patch", "gnupatch"]);
+ }
+ tools
+}
+
+/// Checks the sentinel, gets what the target lacks of the commands
+/// populate runs there from nixpkgs, takes the lock on the target directory, makes a
/// backup if asked, delivers each source, prunes if asked, and writes the
/// record of the run, see the record module.
pub fn populate(context: &Context, spec: &Spec) -> Result<()> {
@@ -20,10 +44,13 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> {
refuse_copying_with_sudo(context, spec)?;
let sentinel = format!("{dir}/.populate");
let force = if spec.force { "1" } else { "" };
- context.run(
- sh::CHECK,
- &[&sentinel, &context.name(&sentinel), dir, force],
- )?;
+ let name = context.name(&sentinel);
+ let mut args = vec![sentinel.as_str(), &name, dir, force];
+ args.extend(tools(spec));
+ let output = context.output(sh::CHECK, &args)?;
+ if let Some(path) = output.lines().find_map(|line| line.strip_prefix("path ")) {
+ context.add_path(path);
+ }
let started = record::now();
let (previous, last) = context.lock(&sentinel, json!({ "run": started }))?;
if spec.backup {
diff --git a/pkgs/populate/src/rsync.rs b/pkgs/populate/src/rsync.rs
index 05eb126..f943d0e 100644
--- a/pkgs/populate/src/rsync.rs
+++ b/pkgs/populate/src/rsync.rs
@@ -106,9 +106,15 @@ pub fn transfer(
if config.use_checksum {
command.arg("--checksum");
}
- if context.target.sudo {
- command.arg("--rsync-path=sudo rsync");
- }
+ // The remote shell runs it as a command.
+ let sudo = if context.target.sudo { "sudo " } else { "" };
+ match context.path() {
+ Some(path) => command.arg(format!(
+ "--rsync-path={sudo}env PATH={path}:\"$PATH\" rsync"
+ )),
+ None if context.target.sudo => command.arg("--rsync-path=sudo rsync"),
+ None => &mut command,
+ };
command.arg("-e").arg(&context.ssh).arg("--protect-args");
if insecure_links {
command.arg("--insecure-links");
diff --git a/pkgs/populate/src/run.rs b/pkgs/populate/src/run.rs
index 113bcf9..cf6364d 100644
--- a/pkgs/populate/src/run.rs
+++ b/pkgs/populate/src/run.rs
@@ -123,6 +123,9 @@ pub struct Context {
/// The command holding the lock on the target directory, see lock, and
/// the lock file's name in messages.
lock: std::cell::RefCell<Option<(Child, String)>>,
+ /// Directories on the target that commands there find commands in
+ /// before those in their PATH, see add_path.
+ path: std::cell::RefCell<Option<String>>,
}
impl Context {
@@ -151,6 +154,7 @@ impl Context {
git_checked: Default::default(),
journal: Default::default(),
entries: Default::default(),
+ path: Default::default(),
lock: Default::default(),
};
context.write_ssh(&context.ssh, &context.ssh_options(true))?;
@@ -249,10 +253,14 @@ impl Context {
*flushed = entries.len();
}
}
+ let path = match &*self.path.borrow() {
+ Some(path) => format!("PATH={path}:$PATH\nexport PATH\n"),
+ None => String::new(),
+ };
let script = if lines.is_empty() {
- format!("set -efu\n{script}")
+ format!("set -efu\n{path}{script}")
} else {
- format!("set -efu\n{}{script}", crate::sh::JOURNAL)
+ format!("set -efu\n{path}{}{script}", crate::sh::JOURNAL)
};
let args: Vec<&str> = lines
.iter()
@@ -281,6 +289,29 @@ impl Context {
}
}
+ /// Has commands on the target find commands in path, a list of
+ /// directories like PATH, before those in their PATH. If the target
+ /// is the machine populate runs on, that goes for populate as well.
+ pub fn add_path(&self, path: &str) {
+ if self.local {
+ let old = std::env::var_os("PATH").unwrap_or_default();
+ let mut new = std::ffi::OsString::from(path);
+ if !old.is_empty() {
+ new.push(":");
+ new.push(old);
+ }
+ std::env::set_var("PATH", new);
+ } else {
+ *self.path.borrow_mut() = Some(path.to_string());
+ }
+ }
+
+ /// The directories add_path added for commands on the target, unless
+ /// the target is the machine populate runs on.
+ pub fn path(&self) -> Option<String> {
+ self.path.borrow().clone()
+ }
+
/// Takes the lock on the target directory, whose sentinel is sentinel,
/// adding entry to the journal, which entries go to from now on, until
/// unlock. Returns the lines that runs that didn't finish left in the
diff --git a/pkgs/populate/src/sh.rs b/pkgs/populate/src/sh.rs
index 7a61d92..71f5ebf 100644
--- a/pkgs/populate/src/sh.rs
+++ b/pkgs/populate/src/sh.rs
@@ -125,12 +125,44 @@ remove_others() {
}
"#;
-/// check SENTINEL NAME DIR FORCE: fails unless the sentinel exists, creating
-/// it and the target directory DIR first if FORCE isn't empty. A sentinel
-/// file becomes a directory, which populate keeps its state in. NAME is
-/// the sentinel's name in messages.
+/// check SENTINEL NAME DIR FORCE [COMMAND ATTR]...: fails unless the
+/// sentinel exists, creating it and the target directory DIR first if FORCE
+/// isn't empty. A sentinel file becomes a directory, which populate keeps
+/// its state in. NAME is the sentinel's name in messages. If any COMMAND
+/// isn't there, it warns, gets the packages with the attribute names ATTR
+/// of those from <nixpkgs> with nix-build, and prints path and where their
+/// commands are, like PATH.
pub const CHECK: &str = r#"
sentinel=$1 name=$2 dir=$3 force=$4
+shift 4
+# The commands that aren't there, of those that follow, each followed by
+# the attribute name of its package in nixpkgs, which nix-build gets, once,
+# for all of them.
+missing= attrs=
+while test $# -gt 0; do
+ if ! command -v "$1" >/dev/null; then
+ missing=$missing${missing:+, }$1
+ attrs="$attrs \"$2\""
+ fi
+ shift 2
+done
+if test -n "$missing"; then
+ >&2 printf 'warning: the target lacks %s, which populate gets from <nixpkgs> for this run\n' \
+ "$missing"
+ if ! out=$(nix-build --no-out-link --arg attrs "[$attrs ]" -E '
+ { attrs }: let pkgs = import <nixpkgs> {}; in
+ map (attr: pkgs.lib.getBin pkgs.${attr}) attrs
+ '); then
+ >&2 printf 'error: the target lacks %s, and nix-build cannot get it from <nixpkgs>\n' \
+ "$missing"
+ exit 1
+ fi
+ path=
+ for out in $out; do
+ path=$path${path:+:}$out/bin
+ done
+ printf 'path %s\n' "$path"
+fi
# Before anything touches it, which would touch what it points to.
if test -L "$sentinel"; then
>&2 printf 'error: sentinel file is a symlink: %s\n' "$name"