Skip to main content

mvm_cli/
bootstrap.rs

1use anyhow::Result;
2
3use crate::ui;
4use mvm_core::platform::{self, Platform};
5use mvm_runtime::shell;
6
7/// Check that a package manager is available for the current platform.
8///
9/// - macOS: requires Homebrew
10/// - Linux: requires apt, dnf, or pacman
11pub fn check_package_manager() -> Result<()> {
12    if cfg!(target_os = "macos") {
13        check_homebrew()
14    } else {
15        check_linux_package_manager()
16    }
17}
18
19/// Check if Homebrew is installed and accessible (macOS only).
20pub fn check_homebrew() -> Result<()> {
21    which::which("brew").map_err(|_| {
22        anyhow::anyhow!(
23            "Homebrew is not installed.\n\
24             Install it first:\n\n  \
25             /bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"\n\n\
26             Then run 'mvmctl bootstrap' again."
27        )
28    })?;
29    ui::info("Homebrew found.");
30    Ok(())
31}
32
33/// Check that a Linux package manager is available.
34fn check_linux_package_manager() -> Result<()> {
35    for cmd in &["apt-get", "dnf", "pacman"] {
36        if which::which(cmd).is_ok() {
37            ui::info(&format!("Package manager found: {}", cmd));
38            return Ok(());
39        }
40    }
41    anyhow::bail!(
42        "No supported package manager found (apt-get, dnf, or pacman).\n\
43         Install Lima manually: https://lima-vm.io/docs/installation/"
44    )
45}
46
47/// Install Lima if not already installed.
48///
49/// On native Linux with KVM, Lima is not required — this is a no-op.
50/// On macOS or Linux without KVM: installs Lima via package manager.
51pub fn ensure_lima() -> Result<()> {
52    if platform::current() == Platform::LinuxNative {
53        ui::info("Native Linux with KVM detected — Lima not required.");
54        return Ok(());
55    }
56
57    if which::which("limactl").is_ok() {
58        let output = shell::run_host("limactl", &["--version"])?;
59        let version = String::from_utf8_lossy(&output.stdout).trim().to_string();
60        ui::info(&format!("Lima already installed: {}", version));
61        return Ok(());
62    }
63
64    if cfg!(target_os = "macos") {
65        ui::info("Installing Lima via Homebrew...");
66        shell::run_host_visible("brew", &["install", "lima"])?;
67    } else {
68        install_lima_linux()?;
69    }
70
71    which::which("limactl").map_err(|_| {
72        anyhow::anyhow!("Lima installation completed but 'limactl' not found in PATH.")
73    })?;
74
75    ui::success("Lima installed successfully.");
76    Ok(())
77}
78
79/// Install Lima on Linux via binary download from GitHub releases.
80fn install_lima_linux() -> Result<()> {
81    // Check for Homebrew first (works on Linux)
82    if which::which("brew").is_ok() {
83        ui::info("Installing Lima via Homebrew...");
84        shell::run_host_visible("brew", &["install", "lima"])?;
85        return Ok(());
86    }
87
88    // Check for Nix (cross-platform)
89    if which::which("nix-env").is_ok() {
90        ui::info("Installing Lima via Nix...");
91        shell::run_host_visible("nix-env", &["-i", "lima"])?;
92        return Ok(());
93    }
94
95    // Fallback: Download binary from GitHub releases
96    ui::info("Installing Lima from GitHub releases...");
97    let install_script = r#"
98set -euo pipefail
99LIMA_VERSION=$(curl -fsSL https://api.github.com/repos/lima-vm/lima/releases/latest | grep '"tag_name"' | sed -E 's/.*"v([^"]+)".*/\1/')
100ARCH=$(uname -m)
101case "$ARCH" in
102    x86_64) ARCH="x86_64" ;;
103    aarch64|arm64) ARCH="aarch64" ;;
104    *) echo "Unsupported architecture: $ARCH" >&2; exit 1 ;;
105esac
106URL="https://github.com/lima-vm/lima/releases/download/v${LIMA_VERSION}/lima-${LIMA_VERSION}-Linux-${ARCH}.tar.gz"
107echo "Downloading Lima ${LIMA_VERSION} for ${ARCH}..."
108curl -fsSL "$URL" | sudo tar -xz -C /usr/local
109sudo chmod +x /usr/local/bin/limactl
110echo "Lima ${LIMA_VERSION} installed successfully"
111"#;
112    shell::run_host_visible("bash", &["-c", install_script])?;
113    Ok(())
114}
115
116/// Check if the platform requires Lima.
117pub fn is_lima_required() -> bool {
118    platform::current().needs_lima()
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124
125    #[test]
126    fn test_check_homebrew_error_message() {
127        if which::which("brew").is_err() {
128            let err = check_homebrew().unwrap_err();
129            let msg = err.to_string();
130            assert!(msg.contains("Homebrew is not installed"));
131            assert!(msg.contains("curl -fsSL"));
132            assert!(msg.contains("mvmctl bootstrap"));
133        } else {
134            assert!(check_homebrew().is_ok());
135        }
136    }
137
138    #[test]
139    fn test_ensure_lima_when_limactl_present() {
140        if which::which("limactl").is_ok() {
141            assert!(ensure_lima().is_ok());
142        }
143    }
144
145    #[test]
146    fn test_is_lima_required() {
147        let _ = is_lima_required();
148    }
149}