diff --git a/docs/reference/builtins.md b/docs/reference/builtins.md index 07b6a39b8..9a625d004 100644 --- a/docs/reference/builtins.md +++ b/docs/reference/builtins.md @@ -25,6 +25,10 @@ These hold for every builtin: When a note mentions a difference, `docs/reference/divergences.md` has the details. To add a builtin, see `docs/how-to/add-a-builtin.md`. +`builtins.resolveGoPackages` is an optional extension, not a standard Nix +builtin. It is available only when the host supplies `IETS_GO2NIX_RESOLVER`; +see [Optional Go package resolver](settings.md#optional-go-package-resolver). + ## Status Every builtin that is not in this table works as it does in Nix. diff --git a/docs/reference/settings.md b/docs/reference/settings.md index 32ae20e6f..7caa4f0f6 100644 --- a/docs/reference/settings.md +++ b/docs/reference/settings.md @@ -61,6 +61,35 @@ prints cover every worker. ties the caches to the binary. `ARCHITECTURE.md`, under "Benchmarks", lists the benchmark scripts that read the statistics knobs. +### Optional Go package resolver + +`IETS_GO2NIX_RESOLVER` is a host-only, absolute executable path. When set, +iets exposes `builtins.resolveGoPackages`. It is not read from `iets.toml` +or a flake, and is disabled by default. `plugin-files` still does not load +native Nix plugins. + +The executable reads go2nix's input object as JSON from stdin and writes +its result object as JSON to stdout. A nonzero exit fails the evaluation +with the helper's stderr. The host supplies the resolver and Go toolchain; +iets does not implement a second Go dependency resolver. A helper can wrap +go2nix's resolver library or call only the builtin through a plugin-enabled +Nix executable, without reevaluating the flake. + +iets materializes the exact filtered source tree before invoking the helper, +including sources held only in its virtual store. Paths returned by go2nix +must remain source-relative. Derivation-backed arguments are unsupported: +this adapter never builds to discover dependencies. Evaluations with the +adapter enabled do not use the tracing cache, because the resolver may read +the host's Go module cache and environment. + +The flake's `lib.mkGo2nixResolver pkgs { nix = ...; pluginFile = ...; }` +packages a helper using an existing plugin-enabled Nix installation. +`pluginFile` is the absolute `.so` path, and `nix` must match the plugin's +Nix ABI. Set `IETS_GO2NIX_RESOLVER` to the resulting package's +`bin/iets-go2nix-resolver`. This invokes Nix for the resolver call alone, +not for flake discovery or derivation evaluation. It neither installs a +plugin nor enables the adapter by default. + ### fix-hashes - `ask` asks `[y/n/a]` for each fix when stdin and stderr are terminals and diff --git a/flake.nix b/flake.nix index bf149994d..e64d7a490 100644 --- a/flake.nix +++ b/flake.nix @@ -55,6 +55,7 @@ devShells = forAll (scope: { default = scope.devShell; }); nixosModules.default = import ./nix/module.nix self; lib.mkClassic = pkgs: iets: nix: pkgs.callPackage ./nix/classic.nix { inherit iets nix; }; + lib.mkGo2nixResolver = pkgs: args: pkgs.callPackage ./nix/go2nix-resolver.nix args; packages = lib.recursiveUpdate (forAll (scope: onLinux scope (scope.callPackages ./nix/packages.nix { }) diff --git a/nix/go2nix-resolver.nix b/nix/go2nix-resolver.nix new file mode 100644 index 000000000..edb900430 --- /dev/null +++ b/nix/go2nix-resolver.nix @@ -0,0 +1,15 @@ +{ writeShellApplication, coreutils, nix, pluginFile }: +writeShellApplication { + name = "iets-go2nix-resolver"; + runtimeInputs = [ coreutils ]; + text = '' + input=$(mktemp) + trap 'rm -f "$input"' EXIT + cat > "$input" + ${nix}/bin/nix-instantiate \ + --option plugin-files ${pluginFile} \ + --eval --strict --json --impure \ + --expr '{ path }: builtins.resolveGoPackages (builtins.fromJSON (builtins.readFile path))' \ + --argstr path "$input" + ''; +} diff --git a/src/builtins/go2nix.rs b/src/builtins/go2nix.rs new file mode 100644 index 000000000..821765fb9 --- /dev/null +++ b/src/builtins/go2nix.rs @@ -0,0 +1,263 @@ +//! Opt-in adapter for go2nix's JSON resolver, supplied by the host rather +//! than a flake. Native Nix plugins cannot run inside this evaluator. + +use std::io::{Seek, SeekFrom, Write}; +use std::path::Path; +use std::process::{Command, Stdio}; + +use super::Registry; +use super::paths; +use super::store_hooks::{self, Follow}; +use super::text::json; +use crate::eval::{Error, ErrorKind, EvalResult, Evaluator, Pos, bail}; +use crate::print; +use crate::runtime::{Ctx, Word}; +use crate::store::nar; + +pub fn register(r: &mut Registry) { + r.op("resolveGoPackages", 1, resolve); +} + +fn resolve(ev: &mut Evaluator, args: &[Word], pos: Pos) -> EvalResult { + ev.trace().mark_uncacheable(); + let fail = |e| Error::boxed(ErrorKind::Eval, format!("resolveGoPackages: {e}"), pos); + let helper = ev + .session() + .env + .non_empty("IETS_GO2NIX_RESOLVER") + .expect("registered only with a host resolver"); + if !Path::new(&helper).is_absolute() { + return Err(fail( + "IETS_GO2NIX_RESOLVER must be an absolute executable path".to_string(), + )); + } + ev.force_attrs( + args[0], + pos, + "while evaluating the argument to builtins.resolveGoPackages", + )?; + let mut input = Vec::new(); + let mut context = Vec::new(); + print::print_json(ev, args[0], &mut input, &mut context, true, pos)?; + if context + .iter() + .any(|c| matches!(c, Ctx::Built { .. } | Ctx::DrvDeep(_))) + { + bail!( + ErrorKind::Unsupported, + pos, + "resolveGoPackages: derivation-backed arguments require import from derivation" + ); + } + let mut input: serde_json::Value = + serde_json::from_slice(&input).map_err(|e| fail(e.to_string()))?; + let src = input + .get("src") + .and_then(serde_json::Value::as_str) + .ok_or_else(|| fail("'src' must be a path or string".to_string()))?; + let path = ev.mk_string(src.as_bytes()); + let path = paths::realise_path(ev, path, pos)?; + store_hooks::confine(ev, &path, Follow::Full, pos)?; + let source = store_hooks::check(ev, store_hooks::locate(ev, &path), &path, Follow::Full, pos)?; + let dir = tempfile::tempdir().map_err(|e| fail(e.to_string()))?; + let mut archive = tempfile::tempfile().map_err(|e| fail(e.to_string()))?; + source.nar(&mut archive).map_err(|e| fail(e.to_string()))?; + archive.rewind().map_err(|e| fail(e.to_string()))?; + let materialized = dir.path().join("src"); + nar::restore_path(&mut archive, &materialized).map_err(|e| fail(e.to_string()))?; + input["src"] = materialized.to_string_lossy().into_owned().into(); + + // A file avoids pipe backpressure if the helper writes before reading. + let mut request = tempfile::tempfile().map_err(|e| fail(e.to_string()))?; + serde_json::to_writer(&mut request, &input).map_err(|e| fail(e.to_string()))?; + request.flush().map_err(|e| fail(e.to_string()))?; + request + .seek(SeekFrom::Start(0)) + .map_err(|e| fail(e.to_string()))?; + let result = Command::new(helper) + .stdin(Stdio::from(request)) + .output() + .map_err(|e| fail(e.to_string()))?; + if !result.status.success() { + return Err(fail(format!( + "resolver exited with {}: {}", + result.status, + String::from_utf8_lossy(&result.stderr) + ))); + } + let decoded: serde_json::Value = + serde_json::from_slice(&result.stdout).map_err(|e| fail(e.to_string()))?; + if !decoded.is_object() { + return Err(fail("resolver must return a JSON object".to_string())); + } + let output = ev.mk_string(&result.stdout); + json::from_json(ev, &[output], pos) +} + +#[cfg(all(test, unix))] +mod tests { + use std::fs; + use std::os::unix::fs::PermissionsExt; + use std::path::{Path, PathBuf}; + + use crate::env::Env; + use crate::eval::{EvalConfig, Evaluator, Pos}; + use crate::runtime::Word; + use crate::session::Session; + use crate::sys; + + fn evaluator(helper: Option<&Path>) -> Evaluator { + let env = match helper { + Some(path) => Env::isolated().with([("IETS_GO2NIX_RESOLVER", path.as_os_str())]), + None => Env::isolated(), + }; + EvalConfig::new() + .session(Session::with_env(env)) + .trace(true) + .build() + } + + fn helper(dir: &Path, body: &str) -> PathBuf { + let path = dir.join("resolver"); + fs::write(&path, format!("#!/bin/sh\nset -eu\n{body}\n")).unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap(); + path + } + + #[test] + fn absent_without_host_opt_in() { + let mut ev = evaluator(None); + let v = ev + .eval_string("builtins ? resolveGoPackages", b"/") + .unwrap(); + assert_eq!(ev.force(v).unwrap(), Word::FALSE); + assert!(ev.trace().is_cacheable()); + } + + #[test] + fn opt_in_disables_the_tracing_cache_before_any_lookup() { + let ev = evaluator(Some(Path::new("/unused"))); + assert!(!ev.trace().is_cacheable()); + } + + #[test] + fn helper_must_be_an_absolute_path() { + let mut ev = evaluator(Some(Path::new("untrusted-path-lookup"))); + let error = ev + .eval_string("builtins.resolveGoPackages {}", b"/") + .and_then(|v| ev.force(v)) + .unwrap_err(); + assert!( + error.msg.contains("absolute executable path"), + "{}", + error.msg + ); + } + + #[test] + fn helper_reads_only_the_filtered_virtual_source() { + let dir = tempfile::tempdir().unwrap(); + fs::write(dir.path().join("hidden"), "not in the source").unwrap(); + fs::write( + dir.path().join("result.json"), + r#"{"modulePath":"example.test/fixture","packages":{}}"#, + ) + .unwrap(); + let helper = helper( + dir.path(), + r#"input=$(cat) +src=$(printf '%s' "$input" | sed 's/.*"src":"\([^"]*\)".*/\1/') +test ! -e "$src/hidden" +cat "$src/result.json""#, + ); + let mut ev = evaluator(Some(&helper)); + let src = format!( + r#"(builtins.resolveGoPackages {{ + src = builtins.path {{ + path = {}; + filter = p: type: type == "directory" || builtins.baseNameOf p == "result.json"; + }}; + }}).modulePath"#, + sys::internal_string(dir.path()) + ); + let v = ev.eval_string(&src, b"/").unwrap(); + assert_eq!( + ev.force_string_no_ctx(v, Pos::NONE, "").unwrap(), + b"example.test/fixture" + ); + } + + #[test] + fn rejects_derivation_arguments_before_running_helper() { + let dir = tempfile::tempdir().unwrap(); + let helper = helper(dir.path(), "echo should-not-run >&2; exit 3"); + let mut ev = evaluator(Some(&helper)); + let v = ev + .eval_string( + r#"builtins.resolveGoPackages { + src = derivation { name = "source"; system = "test"; builder = "/not-run"; }; + }"#, + b"/", + ) + .and_then(|v| ev.force(v)); + let error = v.unwrap_err(); + assert!( + error.msg.contains("derivation-backed arguments"), + "{}", + error.msg + ); + } + + #[test] + fn helper_failures_are_evaluation_errors() { + let dir = tempfile::tempdir().unwrap(); + let helper = helper(dir.path(), "echo resolver-failed >&2; exit 7"); + let mut ev = evaluator(Some(&helper)); + let src = format!( + "builtins.resolveGoPackages {{ src = {}; }}", + sys::internal_string(dir.path()) + ); + let error = ev + .eval_string(&src, b"/") + .and_then(|v| ev.force(v)) + .unwrap_err(); + assert!(error.msg.contains("resolver-failed"), "{}", error.msg); + assert!(error.msg.contains('7'), "{}", error.msg); + } + + #[test] + fn rejects_non_object_results() { + let dir = tempfile::tempdir().unwrap(); + let helper = helper(dir.path(), "printf '[]'"); + let mut ev = evaluator(Some(&helper)); + let src = format!( + "builtins.resolveGoPackages {{ src = {}; }}", + sys::internal_string(dir.path()) + ); + let error = ev + .eval_string(&src, b"/") + .and_then(|v| ev.force(v)) + .unwrap_err(); + assert!(error.msg.contains("JSON object"), "{}", error.msg); + } + + #[test] + fn preserves_pure_source_access_checks() { + let dir = tempfile::tempdir().unwrap(); + let helper = helper(dir.path(), "echo should-not-run >&2; exit 3"); + let env = Env::isolated().with([("IETS_GO2NIX_RESOLVER", helper.as_os_str())]); + let mut ev = EvalConfig::new() + .session(Session::with_env(env)) + .pure(true) + .build(); + let expr = format!( + "builtins.resolveGoPackages {{ src = \"{}\"; }}", + sys::internal_string(dir.path()) + ); + let error = ev + .eval_string(&expr, b"/") + .and_then(|v| ev.force(v)) + .unwrap_err(); + assert!(error.msg.contains("forbidden"), "{}", error.msg); + } +} diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs index 96fcb0186..dd322de76 100644 --- a/src/builtins/mod.rs +++ b/src/builtins/mod.rs @@ -9,6 +9,7 @@ pub mod derivation; pub mod fetch; pub mod fetch_closure; pub mod fetch_git; +mod go2nix; pub mod lists; pub mod math; pub mod paths; @@ -65,6 +66,10 @@ pub fn install(ev: &mut Evaluator) { context::register(&mut r); text::register(&mut r); derivation::register(&mut r); + if ev.session().env.non_empty("IETS_GO2NIX_RESOLVER").is_some() { + ev.trace().mark_uncacheable(); + go2nix::register(&mut r); + } r.constant("true", Word::TRUE); r.constant("false", Word::FALSE);