Skip to main content

byob_exec/env/
nix.rs

1//! Nix: the workspace's own dev shell, packages installed on request, and a `nix` CLI that
2//! works inside the sandbox.
3//!
4//! What a sandboxed `nix` needs (verified on NixOS with Nix 2.34):
5//! - read `/nix/store`, `/nix/var/nix` and `/etc/nix`, and connect to the daemon socket.
6//!   The daemon downloads and builds outside the sandbox, so a run without network can
7//!   still get packages from the binary cache — but the client sees no network, warns
8//!   "you don't have Internet access" and turns substitution off; `substitute = true` in
9//!   `NIX_CONFIG` turns it back on.
10//! - the flake registry: the global one is downloaded by the *client* (fails offline), so
11//!   byob points `flake-registry` at its own file pinning `nixpkgs` (seeded from
12//!   `/etc/nix/registry.json`, else from the first install). Flake inputs that aren't in
13//!   the store yet are fetched by the client too, so loading a dev shell runs with network.
14//! - a writable cache: `NIX_CACHE_HOME` is `<data>/nix/cache`, shared by all runs, so
15//!   evaluation and fetcher caches survive the per-run temp home.
16//! - `nix-command flakes` enabled even if only the user's own `nix.conf` (unreadable from
17//!   the sandbox) enables them.
18
19use std::path::{Path, PathBuf};
20use std::time::Duration;
21
22use serde::{Deserialize, Serialize};
23use serde_json::{Value, json};
24
25use super::{Installed, Layer, Project, Runner, keep_var, write_atomic};
26use crate::Job;
27
28pub const DAEMON_SOCKET: &str = "/nix/var/nix/daemon-socket/socket";
29/// What `nix` says when the sandbox has no network (left out of tool output: with
30/// `substitute = true` the daemon still downloads).
31pub const OFFLINE_WARNING: &str = "warning: you don't have Internet access; disabling some network-dependent features\n";
32const LOAD_TIMEOUT: Duration = Duration::from_secs(900);
33const EVAL_TIMEOUT: Duration = Duration::from_secs(300);
34const HOOK_TIMEOUT: Duration = Duration::from_secs(300);
35/// Largest dev environment JSON read back from the profile.
36const MAX_ENV_JSON: u64 = 64 << 20;
37/// Most packages per workspace.
38const MAX_INSTALLED: usize = 100;
39
40/// Nix on this machine.
41#[derive(Debug, Clone)]
42pub struct Nix {
43    bin: PathBuf,
44    /// The daemon's socket (multi-user install); `None` for a single-user install.
45    socket: Option<PathBuf>,
46    /// `<data>/nix`: shared cache, registry pin.
47    state: Option<PathBuf>,
48}
49
50impl Nix {
51    /// `nix` on `PATH` and a `/nix/store`. Prepares `<data>/nix` (cache folder, registry).
52    pub fn detect(data_dir: Option<&Path>) -> Option<Self> {
53        if !Path::new("/nix/store").is_dir() {
54            return None;
55        }
56        let bin = byob_sandbox::which("nix".as_ref())?;
57        let socket = Path::new(DAEMON_SOCKET).exists().then(|| PathBuf::from(DAEMON_SOCKET));
58        let state = data_dir.map(|d| d.join("nix")).and_then(|s| match std::fs::create_dir_all(s.join("cache")) {
59            Ok(()) => s.canonicalize().ok(),
60            Err(e) => {
61                log::warn!("nix: can't create {}: {e}", s.display());
62                None
63            }
64        });
65        let nix = Self { bin, socket, state };
66        nix.seed_registry();
67        Some(nix)
68    }
69
70    pub fn binary(&self) -> &Path {
71        &self.bin
72    }
73
74    pub fn daemon(&self) -> bool {
75        self.socket.is_some()
76    }
77
78    fn registry_file(&self) -> Option<PathBuf> {
79        self.state.as_ref().map(|s| s.join("registry.json"))
80    }
81
82    fn pin_file(&self) -> Option<PathBuf> {
83        self.state.as_ref().map(|s| s.join("nixpkgs.json"))
84    }
85
86    /// Start byob's registry from the system's `nixpkgs` pin (NixOS sets one), so `nixpkgs#…`
87    /// works offline from the first run.
88    fn seed_registry(&self) {
89        let Some(file) = self.registry_file() else { return };
90        if file.exists() {
91            return;
92        }
93        let Ok(text) = std::fs::read_to_string("/etc/nix/registry.json") else { return };
94        if let Some(entry) = nixpkgs_entry(&text)
95            && let Err(e) = write_atomic(&file, registry_json(entry).as_bytes())
96        {
97            log::warn!("nix: {e}");
98        }
99    }
100
101    /// `NIX_CONFIG` for every run.
102    pub fn config(&self) -> String {
103        let mut c = String::from("extra-experimental-features = nix-command flakes\nsubstitute = true\nwarn-dirty = false\n");
104        if let Some(r) = self.registry_file().filter(|r| r.is_file()) {
105            c.push_str(&format!("flake-registry = {}\n", r.display()));
106        }
107        c
108    }
109
110    /// What every sandboxed run gets so `nix` works inside it.
111    pub fn support(&self) -> Layer {
112        let mut l = Layer { read: vec!["/nix/var/nix".into(), "/etc/nix".into()], ..Default::default() };
113        if let Some(dir) = self.bin.canonicalize().ok().and_then(|p| p.parent().map(Path::to_path_buf)) {
114            l.read.push(dir);
115        }
116        match &self.socket {
117            Some(s) => l.sockets.push(s.clone()),
118            // Single-user: the client writes the store itself.
119            None => l.write.extend([PathBuf::from("/nix/store"), PathBuf::from("/nix/var/nix")]),
120        }
121        l.vars.push(("NIX_CONFIG".into(), self.config()));
122        if let Some(s) = &self.state {
123            let cache = s.join("cache");
124            l.vars.push(("NIX_CACHE_HOME".into(), cache.to_string_lossy().into_owned()));
125            l.write.push(cache);
126            if let Some(r) = self.registry_file() {
127                l.read.push(r);
128            }
129        }
130        // Legacy `nix-shell -p` and `<nixpkgs>` (NixOS sets NIX_PATH=nixpkgs=flake:nixpkgs).
131        for k in ["NIX_PATH", "NIX_REMOTE"] {
132            if let Some(v) = std::env::var_os(k) {
133                l.vars.push((k.into(), v.to_string_lossy().into_owned()));
134            }
135        }
136        l
137    }
138
139    // ---- packages installed with install_packages --------------------------------------
140
141    /// The installed packages of the workspace whose state folder is `dir`.
142    pub fn installed(&self, dir: &Path) -> Option<Layer> {
143        let target = dir.join("tools").canonicalize().ok()?;
144        let bin = target.join("bin");
145        bin.is_dir().then(|| Layer { path: vec![bin], ..Default::default() })
146    }
147
148    /// Install `packages` (nixpkgs attribute names, or Python libraries) for the workspace
149    /// whose state folder is `dir`: pin nixpkgs, check each name, build one environment.
150    pub(crate) fn install(&self, dir: &Path, packages: &[String], run: &dyn Runner) -> Result<Installed, String> {
151        let requests = packages.iter().map(|p| Request::parse(p)).collect::<Result<Vec<_>, _>>()?;
152        std::fs::create_dir_all(dir).map_err(|e| format!("can't create {}: {e}", dir.display()))?;
153        let dir = dir.canonicalize().map_err(|e| format!("{}: {e}", dir.display()))?;
154        let state_file = dir.join("packages.json");
155        let mut state: Tools = std::fs::read_to_string(&state_file).ok().and_then(|s| serde_json::from_str(&s).ok()).unwrap_or_default();
156        if state.nixpkgs.is_empty() {
157            state.nixpkgs = self.pin(&dir, run)?;
158        }
159        let found = self.classify(&dir, &state.nixpkgs, &requests, run)?;
160        let mut added = Vec::new();
161        let mut missing = Vec::new();
162        let mut not_found = Vec::new();
163        for (req, f) in requests.iter().zip(&found) {
164            match f {
165                Found::Package { attr, name } => {
166                    if !state.packages.contains(attr) {
167                        state.packages.push(attr.clone());
168                    }
169                    added.push(name.clone());
170                }
171                Found::Python { attr, name } => {
172                    if !state.python.contains(attr) {
173                        state.python.push(attr.clone());
174                    }
175                    added.push(format!("{name} (Python library)"));
176                }
177                Found::Missing { suggestions } => {
178                    missing.push(missing_text(&req.query, suggestions));
179                    not_found.push(req.query.clone());
180                }
181            }
182        }
183        if state.packages.len() + state.python.len() > MAX_INSTALLED {
184            return Err(format!("a workspace can have at most {MAX_INSTALLED} installed packages"));
185        }
186        let mut text = String::new();
187        if !added.is_empty() {
188            self.build(&dir, &state, run)?;
189            write_atomic(&state_file, serde_json::to_string_pretty(&state).unwrap_or_default().as_bytes())?;
190            text.push_str(&format!(
191                "Installed with Nix for this workspace: {}.\nrun_command and run_python can use them now (they are on PATH; Python libraries are in run_python's python3).",
192                added.join(", ")
193            ));
194        }
195        if !missing.is_empty() {
196            if !text.is_empty() {
197                text.push_str("\n\n");
198            }
199            text.push_str(&format!(
200                "Not found in nixpkgs: {}.\nCall install_packages again with the exact name (search: https://search.nixos.org/packages).",
201                missing.join("; ")
202            ));
203        }
204        Ok(Installed { text, failed: !missing.is_empty(), missing: not_found })
205    }
206
207    /// The locked `nixpkgs` every install uses: byob's pin, else the registry's `nixpkgs`
208    /// resolved now (and pinned, also in byob's registry).
209    fn pin(&self, dir: &Path, run: &dyn Runner) -> Result<String, String> {
210        let Some(pin_file) = self.pin_file() else { return Err("byob has no data folder for Nix".into()) };
211        if let Some(url) =
212            std::fs::read_to_string(&pin_file).ok().and_then(|s| serde_json::from_str::<Value>(&s).ok()).and_then(|v| v["url"].as_str().map(str::to_owned))
213        {
214            return Ok(url);
215        }
216        let mut job = self.job(vec!["flake".into(), "metadata".into(), "nixpkgs".into(), "--json".into()], EVAL_TIMEOUT);
217        job.network = true;
218        let out = in_tmp(run, job, dir)?;
219        if !out.success() {
220            return Err(format!("couldn't resolve nixpkgs: {}", nix_error(&out.stderr.text())));
221        }
222        let meta: Value = serde_json::from_slice(&out.stdout.bytes()).map_err(|e| format!("bad `nix flake metadata` output: {e}"))?;
223        let url = meta["url"].as_str().filter(|u| !u.is_empty()).ok_or("`nix flake metadata nixpkgs` gave no locked URL")?.to_owned();
224        write_atomic(&pin_file, json!({"url": url}).to_string().as_bytes())?;
225        if let Some(reg) = self.registry_file().filter(|r| !r.exists())
226            && let Some(locked) = meta.get("locked").and_then(Value::as_object)
227        {
228            let mut to = locked.clone();
229            to.remove("__final");
230            write_atomic(&reg, registry_json(json!({"exact": true, "from": {"id": "nixpkgs", "type": "indirect"}, "to": to})).as_bytes())?;
231        }
232        Ok(url)
233    }
234
235    fn classify(&self, dir: &Path, nixpkgs: &str, requests: &[Request], run: &dyn Runner) -> Result<Vec<Found>, String> {
236        let mut job = self.job(vec!["eval".into(), "--impure".into(), "--json".into(), "--expr".into(), CLASSIFY.into()], EVAL_TIMEOUT);
237        job.network = true;
238        job.env.push(("BYOB_NIXPKGS".into(), nixpkgs.into()));
239        job.env.push(("BYOB_REQ".into(), serde_json::to_string(requests).unwrap_or_default().into()));
240        let out = in_tmp(run, job, dir)?;
241        if !out.success() {
242            return Err(format!("checking the package names failed: {}", nix_error(&out.stderr.text())));
243        }
244        let found: Vec<Found> = serde_json::from_slice(&out.stdout.bytes()).map_err(|e| format!("bad `nix eval` output: {e}"))?;
245        if found.len() != requests.len() {
246            return Err("bad `nix eval` output: wrong length".into());
247        }
248        Ok(found)
249    }
250
251    fn build(&self, dir: &Path, state: &Tools, run: &dyn Runner) -> Result<(), String> {
252        let link = dir.join("tools");
253        let args = vec!["build".into(), "--impure".into(), "--out-link".into(), link.into_os_string(), "--expr".into(), BUILD.into()];
254        let mut job = self.job(args, LOAD_TIMEOUT);
255        job.network = true;
256        job.layer.write.push(dir.to_path_buf());
257        job.env.push(("BYOB_NIXPKGS".into(), state.nixpkgs.clone().into()));
258        job.env.push(("BYOB_REQ".into(), json!({"packages": state.packages, "python": state.python}).to_string().into()));
259        let out = in_tmp(run, job, dir)?;
260        if out.cancelled {
261            return Err("Stopped by the user.".into());
262        }
263        if !out.success() {
264            return Err(format!("building the packages failed: {}", nix_error(&out.stderr.text())));
265        }
266        Ok(())
267    }
268
269    fn job(&self, args: Vec<std::ffi::OsString>, timeout: Duration) -> Job {
270        Job::new(self.bin.clone().into_os_string(), args, timeout)
271    }
272
273    /// Load the dev shell of `ws`'s `flake.nix` / `shell.nix` with `nix print-dev-env`
274    /// (GC-rooted by a profile in `dir`), then run its `shellHook` once.
275    fn load_shell(&self, ws: &Path, dir: &Path, kind: ShellKind, run: &dyn Runner) -> Result<Layer, String> {
276        let profile = dir.join("devshell-profile");
277        let mut args: Vec<std::ffi::OsString> = vec!["print-dev-env".into(), "--profile".into(), profile.clone().into_os_string(), "--json".into()];
278        match kind {
279            ShellKind::Flake => args.push(".".into()),
280            ShellKind::ShellNix => args.extend(["--impure".into(), "--file".into(), "shell.nix".into()]),
281        }
282        let mut job = self.job(args, LOAD_TIMEOUT);
283        // Flake inputs not yet in the store are fetched by the client.
284        job.network = true;
285        job.layer.write.extend([ws.to_path_buf(), dir.to_path_buf()]);
286        job.layer.read.extend(git_dirs(ws));
287        let tmp = byob_sandbox::TempDir::new("byob-nix-")?;
288        let out = run.run(job, ws, tmp.path())?;
289        if out.cancelled {
290            return Err("Stopped by the user.".into());
291        }
292        if !out.success() {
293            let err = nix_error(&out.stderr.text());
294            let hint = if err.contains("is not tracked by Git") { "\n(Nix only sees files git tracks: `git add flake.nix` and try again.)" } else { "" };
295            return Err(format!("`nix print-dev-env` failed: {err}{hint}"));
296        }
297        let target = profile.canonicalize().map_err(|e| format!("no dev shell profile: {e}"))?;
298        let size = std::fs::metadata(&target).map(|m| m.len()).unwrap_or(0);
299        if size > MAX_ENV_JSON {
300            return Err(format!("the dev shell environment is too large ({size} bytes)"));
301        }
302        let text = std::fs::read_to_string(&target).map_err(|e| format!("can't read {}: {e}", target.display()))?;
303        let env = DevEnv::parse(&text)?;
304        match env.shell_hook.as_deref().filter(|h| !h.trim().is_empty()) {
305            None => Ok(env.layer),
306            Some(hook) => {
307                let bash = env.bash.as_deref().map(PathBuf::from).filter(|b| b.is_file()).or_else(|| byob_sandbox::which("bash".as_ref()));
308                let Some(bash) = bash else { return Ok(env.layer) };
309                let mut layer = run_hook(&bash, hook, &env.layer, ws, run).map_err(|e| format!("its shellHook failed: {e}"))?;
310                layer.stack(&env.layer);
311                Ok(layer)
312            }
313        }
314    }
315}
316
317/// The workspace's `flake.nix` / `shell.nix` dev shell as a `Project`.
318#[derive(Debug)]
319pub struct NixShell(pub Nix);
320
321impl Project for NixShell {
322    fn id(&self) -> &str {
323        "devshell"
324    }
325
326    fn label(&self) -> &str {
327        "Nix dev shell"
328    }
329
330    fn file(&self, ws: &Path) -> Option<String> {
331        ShellKind::find(ws).map(|k| k.file().to_owned())
332    }
333
334    fn inputs(&self) -> Vec<String> {
335        ["flake.nix", "flake.lock", "shell.nix", "default.nix"].map(str::to_owned).to_vec()
336    }
337
338    fn load(&self, ws: &Path, dir: &Path, run: &dyn Runner) -> Result<Layer, String> {
339        let kind = ShellKind::find(ws).ok_or("no flake.nix or shell.nix")?;
340        self.0.load_shell(ws, dir, kind, run)
341    }
342}
343
344/// Run the dev shell's `shellHook` once in its environment and keep what it changed.
345fn run_hook(bash: &Path, hook: &str, env: &Layer, ws: &Path, run: &dyn Runner) -> Result<Layer, String> {
346    let tmp = byob_sandbox::TempDir::new("byob-hook-")?;
347    let dump = tmp.path().join("env.dump");
348    let script = r#"eval "$shellHook" >&2 </dev/null; env -0 > "$1""#;
349    let mut job = Job::new(
350        bash.as_os_str().to_owned(),
351        vec!["--noprofile".into(), "--norc".into(), "-c".into(), script.into(), "bash".into(), dump.clone().into_os_string()],
352        HOOK_TIMEOUT,
353    );
354    job.network = true;
355    job.layer = env.clone();
356    job.layer.write.push(ws.to_path_buf());
357    job.env.push(("shellHook".into(), hook.into()));
358    let out = run.run(job, ws, tmp.path())?;
359    let bytes = std::fs::read(&dump).map_err(|_| format!("exit code {:?}: {}", out.exit_code, nix_error(&out.stderr.text())))?;
360    let mut before = crate::clean_env_vars(tmp.path());
361    before.extend(env.vars.iter().cloned());
362    let host = std::env::var_os("PATH").map(|p| std::env::split_paths(&p).collect::<Vec<_>>()).unwrap_or_default();
363    Ok(hook_changes(&bytes, &before, &env.path, &host))
364}
365
366/// Variables the hook set or changed (an `env -0` dump against what it started with), and
367/// `PATH` entries it added in front.
368pub fn hook_changes(dump: &[u8], before: &[(String, String)], had_path: &[PathBuf], host_path: &[PathBuf]) -> Layer {
369    let mut l = Layer::default();
370    for entry in dump.split(|b| *b == 0) {
371        let entry = String::from_utf8_lossy(entry);
372        let Some((k, v)) = entry.split_once('=') else { continue };
373        if k == "PATH" {
374            // The hook ran in Nix's bash: a Unix PATH whatever the host's rules.
375            for p in v.split(':').filter(|p| p.starts_with('/')).map(PathBuf::from) {
376                if !host_path.contains(&p) && !had_path.contains(&p) && !l.path.contains(&p) {
377                    l.path.push(p);
378                }
379            }
380            continue;
381        }
382        if k.is_empty() || keep_var(k) || before.iter().any(|(bk, bv)| bk == k && bv == v) {
383            continue;
384        }
385        l.vars.push((k.to_owned(), v.to_owned()));
386    }
387    l
388}
389
390/// A dev environment as `nix print-dev-env --json` describes it.
391#[derive(Debug, Clone, PartialEq, Eq)]
392pub struct DevEnv {
393    pub layer: Layer,
394    pub shell_hook: Option<String>,
395    /// The bash the environment was made with (runs the hook).
396    pub bash: Option<String>,
397}
398
399impl DevEnv {
400    /// Exported variables become the layer (`PATH` split into entries), except those the
401    /// sandbox or `nix develop` keep from outside ([`keep_var`]).
402    pub fn parse(text: &str) -> Result<Self, String> {
403        let v: Value = serde_json::from_str(text).map_err(|e| format!("bad dev shell JSON: {e}"))?;
404        let vars = v.get("variables").and_then(Value::as_object).ok_or("dev shell JSON has no variables")?;
405        let mut layer = Layer::default();
406        let str_of = |name: &str| vars.get(name).and_then(|e| e.get("value")).and_then(Value::as_str).map(str::to_owned);
407        for (k, e) in vars {
408            if e.get("type").and_then(Value::as_str) != Some("exported") {
409                continue;
410            }
411            let Some(val) = e.get("value").and_then(Value::as_str) else { continue };
412            if k == "PATH" {
413                layer.path = val.split(':').filter(|p| p.starts_with('/')).map(PathBuf::from).collect();
414            } else if !keep_var(k) && !k.contains('=') && !k.contains('\0') && !val.contains('\0') {
415                layer.vars.push((k.clone(), val.to_owned()));
416            }
417        }
418        Ok(Self { layer, shell_hook: str_of("shellHook"), bash: str_of("BASH") })
419    }
420}
421
422/// Which project file defines the dev shell.
423#[derive(Debug, Clone, Copy, PartialEq, Eq)]
424pub enum ShellKind {
425    Flake,
426    ShellNix,
427}
428
429impl ShellKind {
430    pub fn find(ws: &Path) -> Option<Self> {
431        if ws.join("flake.nix").is_file() {
432            Some(Self::Flake)
433        } else if ws.join("shell.nix").is_file() {
434            Some(Self::ShellNix)
435        } else {
436            None
437        }
438    }
439
440    pub fn file(self) -> &'static str {
441        match self {
442            Self::Flake => "flake.nix",
443            Self::ShellNix => "shell.nix",
444        }
445    }
446}
447
448/// What a workspace has installed (`packages.json`).
449#[derive(Debug, Default, Serialize, Deserialize)]
450struct Tools {
451    /// Locked nixpkgs URL.
452    #[serde(default)]
453    nixpkgs: String,
454    /// Attribute paths, e.g. `jq`, `nodePackages.prettier`.
455    #[serde(default)]
456    packages: Vec<String>,
457    /// `python3Packages` attributes, e.g. `matplotlib`.
458    #[serde(default)]
459    python: Vec<String>,
460}
461
462/// One name the model asked for, as the classify expression wants it.
463#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
464pub struct Request {
465    pub query: String,
466    /// Top-level attribute path to try first.
467    pub top: Option<Vec<String>>,
468    /// `python3Packages` attribute to try (after `top`).
469    pub py: Option<String>,
470}
471
472impl Request {
473    /// Accepts `jq`, `nixpkgs#jq`, `pkgs.jq`, `nodePackages.prettier`, `python3Packages.numpy`,
474    /// `python:numpy`, and pip-style `numpy>=2` (the version is dropped: nixpkgs has one).
475    /// Common command names map to their package (`convert` → `imagemagick`).
476    pub fn parse(raw: &str) -> Result<Self, String> {
477        let bad = || format!("{raw:?} is not a package name (like \"jq\", \"ffmpeg\" or \"matplotlib\")");
478        let s = raw.trim();
479        if s.is_empty() || s.len() > 100 {
480            return Err(bad());
481        }
482        let s = s.strip_prefix("nixpkgs#").or_else(|| s.strip_prefix("nixpkgs.")).or_else(|| s.strip_prefix("pkgs.")).unwrap_or(s);
483        let explicit_py = ["python:", "py:", "pip:", "pypi:"].iter().find_map(|p| s.strip_prefix(p)).or_else(|| {
484            let (set, name) = s.split_once('.')?;
485            let digits = set.strip_prefix("python")?.strip_suffix("Packages")?;
486            digits.chars().all(|c| c.is_ascii_digit()).then_some(name)
487        });
488        if let Some(name) = explicit_py {
489            let py = python_attr(name).ok_or_else(bad)?;
490            return Ok(Self { query: s.to_owned(), top: None, py: Some(py) });
491        }
492        // A version constraint means a pip-style Python requirement.
493        if s.contains(['<', '>', '=', '!', '~', '[']) {
494            let py = python_attr(s).ok_or_else(bad)?;
495            return Ok(Self { query: s.to_owned(), top: None, py: Some(py) });
496        }
497        let segments: Vec<&str> = s.split('.').collect();
498        if segments.len() > 8 || !segments.iter().all(|seg| valid_attr(seg)) {
499            return Err(bad());
500        }
501        if let [one] = segments.as_slice() {
502            let top = command_package(one).unwrap_or(one);
503            return Ok(Self { query: s.to_owned(), top: Some(top.split('.').map(str::to_owned).collect()), py: python_attr(one) });
504        }
505        Ok(Self { query: s.to_owned(), top: Some(segments.iter().map(|x| (*x).to_owned()).collect()), py: None })
506    }
507}
508
509/// A Nix attribute name byob passes on (no quoting needed, nothing to inject).
510fn valid_attr(s: &str) -> bool {
511    let mut chars = s.chars();
512    chars.next().is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
513        && s.len() <= 100
514        && chars.all(|c| c.is_ascii_alphanumeric() || "_-'+".contains(c))
515        && !s.contains("''")
516}
517
518/// The `python3Packages` attribute for a pip name or import name: lowercase, `_`/`.` → `-`,
519/// version and extras dropped, well-known import names mapped (`sklearn` → `scikit-learn`).
520pub fn python_attr(name: &str) -> Option<String> {
521    let base = name.split(['<', '>', '=', '!', '~', '[', ';', ' ']).next()?.trim();
522    let lower = base.to_ascii_lowercase().replace(['_', '.'], "-");
523    let mapped = PYTHON_IMPORTS.iter().find(|(import, _)| import.eq_ignore_ascii_case(base)).map_or(lower.as_str(), |(_, pkg)| pkg).to_owned();
524    valid_attr(&mapped).then_some(mapped)
525}
526
527/// Import names whose nixpkgs package is called something else.
528pub const PYTHON_IMPORTS: &[(&str, &str)] = &[
529    ("sklearn", "scikit-learn"),
530    ("skimage", "scikit-image"),
531    ("cv2", "opencv4"),
532    ("opencv-python", "opencv4"),
533    ("PIL", "pillow"),
534    ("yaml", "pyyaml"),
535    ("bs4", "beautifulsoup4"),
536    ("dateutil", "python-dateutil"),
537    ("dotenv", "python-dotenv"),
538    ("docx", "python-docx"),
539    ("pptx", "python-pptx"),
540    ("magic", "python-magic"),
541    ("serial", "pyserial"),
542    ("usb", "pyusb"),
543    ("Crypto", "pycryptodome"),
544    ("jwt", "pyjwt"),
545    ("git", "gitpython"),
546    ("fitz", "pymupdf"),
547    ("attr", "attrs"),
548    ("google.protobuf", "protobuf"),
549    ("mpl_toolkits", "matplotlib"),
550];
551
552/// Commands whose nixpkgs package has another name.
553pub const COMMANDS: &[(&str, &str)] = &[
554    ("convert", "imagemagick"),
555    ("magick", "imagemagick"),
556    ("identify", "imagemagick"),
557    ("mogrify", "imagemagick"),
558    ("rg", "ripgrep"),
559    ("node", "nodejs"),
560    ("npm", "nodejs"),
561    ("npx", "nodejs"),
562    ("ffprobe", "ffmpeg"),
563    ("make", "gnumake"),
564    ("cc", "gcc"),
565    ("g++", "gcc"),
566    ("pdftotext", "poppler-utils"),
567    ("pdftoppm", "poppler-utils"),
568    ("pdfinfo", "poppler-utils"),
569    ("dot", "graphviz"),
570    ("7z", "p7zip"),
571    ("sqlite3", "sqlite"),
572    ("psql", "postgresql"),
573    ("java", "jdk"),
574    ("javac", "jdk"),
575    ("xmllint", "libxml2"),
576    ("dig", "dnsutils"),
577    ("nc", "netcat"),
578    ("pip", "python3Packages.pip"),
579    ("pip3", "python3Packages.pip"),
580    ("python", "python3"),
581    ("lualatex", "texliveMedium"),
582    ("pdflatex", "texliveMedium"),
583    ("latex", "texliveMedium"),
584    ("xelatex", "texliveMedium"),
585    ("rsvg-convert", "librsvg"),
586    ("inkscape", "inkscape"),
587    ("tesseract", "tesseract"),
588    ("yq", "yq-go"),
589    ("http", "httpie"),
590    ("unzip", "unzip"),
591    ("zip", "zip"),
592];
593
594/// The package for a command, if it differs from the command's name.
595pub fn command_package(cmd: &str) -> Option<&'static str> {
596    COMMANDS.iter().find(|(c, p)| *c == cmd && *p != cmd).map(|(_, p)| *p)
597}
598
599/// What the classify expression says about one request.
600#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
601#[serde(tag = "kind", rename_all = "lowercase")]
602enum Found {
603    Package {
604        attr: String,
605        name: String,
606    },
607    Python {
608        attr: String,
609        name: String,
610    },
611    Missing {
612        #[serde(default)]
613        suggestions: Vec<String>,
614    },
615}
616
617fn missing_text(query: &str, suggestions: &[String]) -> String {
618    let s: Vec<&str> = suggestions.iter().map(String::as_str).filter(|s| s.len() <= 100).take(8).collect();
619    if s.is_empty() { format!("\"{query}\"") } else { format!("\"{query}\" (similar: {})", s.join(", ")) }
620}
621
622/// For each request: a top-level package, else a Python library, else similar names.
623/// Inputs come from the environment (`BYOB_NIXPKGS`, `BYOB_REQ`), never spliced in.
624const CLASSIFY: &str = r#"
625let
626  pkgs = (builtins.getFlake (builtins.getEnv "BYOB_NIXPKGS")).legacyPackages.${builtins.currentSystem};
627  lib = pkgs.lib;
628  req = builtins.fromJSON (builtins.getEnv "BYOB_REQ");
629  nameAt = set: path:
630    let r = builtins.tryEval (let v = lib.attrByPath path null set; in if v != null && lib.isDerivation v then v.name else null);
631    in if r.success then r.value else null;
632  similar = q: set: prefix:
633    let lq = lib.toLower q;
634    in map (n: prefix + n) (lib.take 5 (builtins.filter (n: lib.hasInfix lq (lib.toLower n)) (builtins.attrNames set)));
635  one = r:
636    let
637      top = if r.top == null then null else nameAt pkgs r.top;
638      py = if r.py == null then null else nameAt pkgs.python3Packages [ r.py ];
639    in
640      if top != null then { kind = "package"; attr = lib.concatStringsSep "." r.top; name = top; }
641      else if py != null then { kind = "python"; attr = r.py; name = py; }
642      else { kind = "missing"; suggestions = similar (if r.py != null then r.py else lib.last r.top) pkgs "" ++ (if r.py != null then similar r.py pkgs.python3Packages "python3Packages." else [ ]); };
643in map one req
644"#;
645
646/// One environment with every installed package; Python libraries in one `python3`.
647const BUILD: &str = r#"
648let
649  pkgs = (builtins.getFlake (builtins.getEnv "BYOB_NIXPKGS")).legacyPackages.${builtins.currentSystem};
650  lib = pkgs.lib;
651  req = builtins.fromJSON (builtins.getEnv "BYOB_REQ");
652  python = lib.optional (req.python != [ ]) (pkgs.python3.withPackages (ps: map (n: ps.${n}) req.python));
653in pkgs.buildEnv {
654  name = "byob-workspace-tools";
655  paths = python ++ map (a: lib.getAttrFromPath (lib.splitString "." a) pkgs) req.packages;
656  ignoreCollisions = true;
657}
658"#;
659
660/// The `nixpkgs` entry of a registry file.
661pub fn nixpkgs_entry(registry: &str) -> Option<Value> {
662    let v: Value = serde_json::from_str(registry).ok()?;
663    v.get("flakes")?.as_array()?.iter().find(|f| f["from"]["id"].as_str() == Some("nixpkgs") && f["from"]["type"].as_str() == Some("indirect")).cloned()
664}
665
666fn registry_json(entry: Value) -> String {
667    json!({"version": 2, "flakes": [entry]}).to_string()
668}
669
670/// Nix's stderr from the first `error:` on (skipping warnings about unreachable caches),
671/// bounded.
672pub fn nix_error(stderr: &str) -> String {
673    let from = stderr.find("error:").map_or(stderr, |i| stderr.get(i..).unwrap_or(stderr));
674    let t = from.trim();
675    let max = 3000;
676    if t.len() <= max {
677        return t.to_owned();
678    }
679    let mut cut = max;
680    while !t.is_char_boundary(cut) {
681        cut -= 1;
682    }
683    format!("{}…", t.get(..cut).unwrap_or_default())
684}
685
686/// A git worktree's `.git` is a file pointing elsewhere; Nix must read that folder (and the
687/// main repository's) to see the flake.
688fn git_dirs(ws: &Path) -> Vec<PathBuf> {
689    let Ok(text) = std::fs::read_to_string(ws.join(".git")) else { return Vec::new() };
690    let Some(dir) = text.trim().strip_prefix("gitdir:").map(|d| ws.join(d.trim())) else { return Vec::new() };
691    let mut out = vec![dir.clone()];
692    if let Ok(common) = std::fs::read_to_string(dir.join("commondir")) {
693        out.push(dir.join(common.trim()));
694    }
695    out.into_iter().filter_map(|p| p.canonicalize().ok()).collect()
696}
697
698/// Run `job` in `cwd` with a fresh temp folder.
699fn in_tmp(run: &dyn Runner, job: Job, cwd: &Path) -> Result<byob_sandbox::Outcome, String> {
700    let tmp = byob_sandbox::TempDir::new("byob-nix-")?;
701    run.run(job, cwd, tmp.path())
702}
703
704#[cfg(test)]
705mod tests {
706    use super::*;
707
708    #[test]
709    fn requests() {
710        let r = Request::parse("jq").unwrap();
711        assert_eq!((r.top, r.py), (Some(vec!["jq".into()]), Some("jq".into())));
712        let r = Request::parse("nixpkgs#ripgrep").unwrap();
713        assert_eq!(r.top, Some(vec!["ripgrep".into()]));
714        assert_eq!(Request::parse("convert").unwrap().top, Some(vec!["imagemagick".into()]));
715        assert_eq!(Request::parse("pip").unwrap().top, Some(vec!["python3Packages".into(), "pip".into()]));
716        let r = Request::parse("python3Packages.Scikit_Learn").unwrap();
717        assert_eq!((r.top, r.py), (None, Some("scikit-learn".into())));
718        assert_eq!(Request::parse("python312Packages.numpy").unwrap().py, Some("numpy".into()));
719        assert_eq!(Request::parse("python:sklearn").unwrap().py, Some("scikit-learn".into()));
720        let r = Request::parse("numpy>=2").unwrap();
721        assert_eq!((r.top, r.py), (None, Some("numpy".into())));
722        assert_eq!(Request::parse("xarray[io]").unwrap().py, Some("xarray".into()));
723        let r = Request::parse("nodePackages.prettier").unwrap();
724        assert_eq!((r.top, r.py), (Some(vec!["nodePackages".into(), "prettier".into()]), None));
725        for bad in ["", "a b", "\"x\"", "${x}", "a;b", "../x", "x\\y", "a''b", "-rf", &"a".repeat(101), "a.b.c.d.e.f.g.h.i"] {
726            assert!(Request::parse(bad).is_err(), "{bad:?}");
727        }
728    }
729
730    #[test]
731    fn python_names() {
732        assert_eq!(python_attr("PIL").as_deref(), Some("pillow"));
733        assert_eq!(python_attr("typing_extensions").as_deref(), Some("typing-extensions"));
734        assert_eq!(python_attr("ruamel.yaml").as_deref(), Some("ruamel-yaml"));
735        assert_eq!(python_attr("pandas==2.0; python_version>'3'").as_deref(), Some("pandas"));
736        assert_eq!(python_attr("$(x)"), None);
737    }
738
739    #[test]
740    fn dev_env_json() {
741        let text = json!({"variables": {
742            "PATH": {"type": "exported", "value": "/nix/store/a-jq/bin:/nix/store/b-cowsay/bin"},
743            "HELLO": {"type": "exported", "value": "world"},
744            "HOME": {"type": "exported", "value": "/homeless-shelter"},
745            "TMPDIR": {"type": "exported", "value": "/build"},
746            "BASH": {"type": "var", "value": "/nix/store/c-bash/bin/bash"},
747            "shellHook": {"type": "var", "value": "export X=1"},
748            "buildInputs": {"type": "array", "value": ["a"]},
749            "notExported": {"type": "var", "value": "x"}
750        }})
751        .to_string();
752        let e = DevEnv::parse(&text).unwrap();
753        assert_eq!(e.layer.path, vec![PathBuf::from("/nix/store/a-jq/bin"), PathBuf::from("/nix/store/b-cowsay/bin")]);
754        assert_eq!(e.layer.vars, vec![("HELLO".to_owned(), "world".to_owned())]);
755        assert_eq!(e.shell_hook.as_deref(), Some("export X=1"));
756        assert_eq!(e.bash.as_deref(), Some("/nix/store/c-bash/bin/bash"));
757        assert!(DevEnv::parse("[]").is_err() && DevEnv::parse("nope").is_err());
758    }
759
760    #[test]
761    fn hook_changes_keep_only_what_changed() {
762        let dump = b"A=1\0B=new\0HOME=/tmp/x\0PATH=/ws/.venv/bin:/nix/store/a/bin:/usr/bin\0C=same\0broken\0";
763        let before = vec![("B".to_owned(), "old".to_owned()), ("C".to_owned(), "same".to_owned())];
764        let l = hook_changes(dump, &before, &[PathBuf::from("/nix/store/a/bin")], &[PathBuf::from("/usr/bin")]);
765        assert_eq!(l.path, vec![PathBuf::from("/ws/.venv/bin")]);
766        assert_eq!(l.vars, vec![("A".to_owned(), "1".to_owned()), ("B".to_owned(), "new".to_owned())]);
767    }
768
769    #[test]
770    fn registry_and_errors() {
771        let reg = r#"{"flakes":[{"exact":true,"from":{"id":"other","type":"indirect"},"to":{"type":"path","path":"/x"}},{"exact":true,"from":{"id":"nixpkgs","type":"indirect"},"to":{"path":"/nix/store/q-source","type":"path"}}],"version":2}"#;
772        let e = nixpkgs_entry(reg).unwrap();
773        assert_eq!(e["to"]["path"], "/nix/store/q-source");
774        assert!(nixpkgs_entry("{}").is_none() && nixpkgs_entry("x").is_none());
775        let stderr = "warning: unable to download 'https://x/nix-cache-info': HTTP error 401\nerror: flake 'path:/w' does not provide attribute 'devShells'\n";
776        assert_eq!(nix_error(stderr), "error: flake 'path:/w' does not provide attribute 'devShells'");
777        assert!(nix_error(&"é".repeat(5000)).chars().count() < 3002);
778        let found: Vec<Found> = serde_json::from_str(r#"[{"kind":"package","attr":"jq","name":"jq-1.8"},{"kind":"python","attr":"numpy","name":"python3-numpy"},{"kind":"missing","suggestions":["a"]}]"#).unwrap();
779        assert_eq!(found[2], Found::Missing { suggestions: vec!["a".into()] });
780        assert_eq!(missing_text("imagemagic", &["imagemagick".into()]), "\"imagemagic\" (similar: imagemagick)");
781    }
782
783    #[test]
784    fn project_files() {
785        let d = tempfile::tempdir().unwrap();
786        assert_eq!(ShellKind::find(d.path()), None);
787        std::fs::write(d.path().join("shell.nix"), "{}").unwrap();
788        assert_eq!(ShellKind::find(d.path()), Some(ShellKind::ShellNix));
789        std::fs::write(d.path().join("flake.nix"), "{}").unwrap();
790        assert_eq!(ShellKind::find(d.path()), Some(ShellKind::Flake));
791    }
792
793    #[test]
794    fn worktree_git_dirs_are_readable() {
795        let d = tempfile::tempdir().unwrap();
796        let main = d.path().join("repo/.git");
797        std::fs::create_dir_all(main.join("worktrees/wt")).unwrap();
798        std::fs::write(main.join("worktrees/wt/commondir"), "../..\n").unwrap();
799        let ws = d.path().join("wt");
800        std::fs::create_dir_all(&ws).unwrap();
801        std::fs::write(ws.join(".git"), format!("gitdir: {}\n", main.join("worktrees/wt").display())).unwrap();
802        let dirs = git_dirs(&ws);
803        assert!(dirs.contains(&main.canonicalize().unwrap()), "{dirs:?}");
804        assert!(git_dirs(d.path()).is_empty());
805    }
806
807    #[test]
808    fn config_enables_flakes_and_substitution() {
809        let d = tempfile::tempdir().unwrap();
810        let n = Nix { bin: "/bin/nix".into(), socket: Some(DAEMON_SOCKET.into()), state: Some(d.path().to_path_buf()) };
811        assert!(n.config().contains("nix-command flakes") && n.config().contains("substitute = true"));
812        assert!(!n.config().contains("flake-registry"));
813        std::fs::write(d.path().join("registry.json"), "{}").unwrap();
814        assert!(n.config().contains(&format!("flake-registry = {}", d.path().join("registry.json").display())));
815        let s = n.support();
816        assert_eq!(s.sockets, vec![PathBuf::from(DAEMON_SOCKET)]);
817        assert!(s.write.contains(&d.path().join("cache")) && s.var("NIX_CACHE_HOME").is_some());
818        let single = Nix { socket: None, ..n };
819        assert!(single.support().write.contains(&PathBuf::from("/nix/store")));
820    }
821}