svm-rs

repository·master·Indexed 43 days ago

https://github.com/alloy-rs/svm-rs

A cross-platform Solidity compiler version manager that allows developers to install, switch, and manage multiple versions of the solc binary. It provides a CLI tool for version management and a solc wrapper to specify versions via a +prefix (e.g., +1.8.0). Features include NixOS compatibility patching for versions 0.7.6 through 0.8.28 and support for both asynchronous and blocking installation.

Tokens
5.3K
Snippets
26
Records
30
Agent score
54%

What's inside svm-rs

  1. Install svm-rs

    master

    You can install the svm-rs Solidity compiler version manager using cargo-binstall, via crates.io, or directly from the GitHub repository.

    cargo binstall svm-rs

    Using cargo install

    From crates.io:

    cargo install svm-rs

    From the repository:

    cargo install --locked --git https://github.com/alloy-rs/svm-rs/
    cargo binstall svm-rs
  2. Use svm-rs commands to manage Solc versions

    master

    The svm CLI tool allows you to install, list, remove, and switch between different Solidity compiler (solc) versions.

    Available Commands

    CommandAliasDescription
    installiInstall specific Solc versions
    listlsList all installed Solc versions
    removermRemove a Solc version (use all to remove everything)
    useSet a Solc version as the global default
    whichDisplay which binary will be run for a given version
    helpPrint help information

    Global Options

    • -h, --help: Print help
    • -V, --version: Print version
    svm <COMMAND>
  3. NixOS compatibility and patching

    master

    The svm-rs installer automatically handles NixOS compatibility for specific Solidity versions.

    Versions between 0.7.6 and 0.8.28 (inclusive) are not fully static and require an interpreter patch to work on NixOS. When install is called on a NixOS system for these versions, the library:

    1. Downloads the binary.
    2. Uses patchelf via nix-shell to set the correct dynamic linker.
    3. Adds a persistent gcroot via nix-store --add-root to ensure the dynamic linker is not garbage collected by Nix.

    Versions outside this range (e.g., 0.8.29 and later) are fully static and do not require patching.

  4. Use the svm CLI to manage Solidity compilers

    master

    The svm (Solidity Version Manager) CLI provides commands to install, list, switch between, and remove different versions of the solc compiler.

    Available commands:

    • list (alias: ls): List installed Solidity compiler versions.
    • install (alias: i): Install a specific Solidity compiler version.
    • use: Set a specific Solidity compiler version as the active version.
    • which: Determine the path of the currently active solc compiler.
    • remove (alias: rm): Remove an installed Solidity compiler version.
    # Example usage patterns
    svm ls
    svm i 0.8.20
    svm use 0.8.20
    svm which
    svm rm 0.8.20
  5. Error handling in the solc wrapper

    master

    The solc wrapper may exit with an error in the following scenarios:

    • Invalid Version Format: If the version specifier following the + prefix cannot be parsed as a valid semantic version, or if it contains pre-release/build metadata.
    • Version Not Installed: If the requested version is not found in the svm managed directories.
    • Global Version Not Set: If no version specifier is provided and no global version has been configured via svm.

    Errors are printed to stderr with the prefix svm: error: .

  6. Manage the global Solc version

    master

    You can manage which Solidity compiler version is currently set as the global default using get_global_version, set_global_version, and unset_global_version. The global version is stored in a specific file within the svm-rs data directory.

    • get_global_version(): Returns Ok(Some(Version)) if a version is set, or Ok(None) if no global version is configured.
    • set_global_version(&Version): Sets the provided semver::Version as the global default.
    • unset_global_version(): Clears the global version setting. Use this if you are removing all installed versions.
    use semver::Version;
    
    // Set a global version
    let version = Version::parse("0.8.20").unwrap();
    set_global_version(&version)?;
    
    // Check the current global version
    if let Some(current) = get_global_version()? {
        println!("Global version is: {}", current);
    }
    
    // Unset the global version
    unset_global_version()?;
  7. Locate the SVM data directory

    master

    The data_dir() function returns a static reference to the path used by SVM to store Solc versions and configuration.

    The resolution logic follows this priority:

    1. If ~/.svm exists, it uses that.
    2. Otherwise, it uses $XDG_DATA_HOME/svm (via the system's data directory provider).

    Note: In test environments, this returns a temporary directory.

    use svm_rs::paths::data_dir;
    
    let path = data_dir();
    println!("SVM data directory is at: {}", path.display());
  8. Get the path to a Solc version binary

    master

    The version_binary(version: &str) function returns the absolute path to the actual solc executable for a given version.

    The path follows the pattern: {data_dir}/{version}/solc-{version}.

    use svm_rs::paths::version_binary;
    
    let version = "0.8.26";
    let binary_path = version_binary(version);
    // Result: data_dir / "0.8.26" / "solc-0.8.26"
  9. Install a Solidity compiler version with `install`

    master

    Use the asynchronous install function to download and install a specific Solidity compiler (solc) version. If the version is already installed and the checksum matches, the existing binary is reused. The function handles platform-specific requirements, including NixOS patching and Windows .zip extraction.

    Returns the PathBuf to the installed solc binary.

    use semver::Version;
    // Assuming svm-rs is in your dependencies
    use svm_rs::install;
    
    #[tokio::main]
    async fn main() -> Result<(), Box<dyn std::error::Error>> {
        let version = Version::parse("0.8.25")?;
        let solc_path = install(&version).await?;
        println!("Solc installed at: {:?}", solc_path);
        Ok(())
    }
  10. Initialize the SVM data directory

    master

    Use setup_data_dir() to prepare the SVM environment. This function performs the following actions:

    1. Identifies the appropriate data directory (based on $XDG_DATA_HOME or ~/.svm).
    2. Creates the directory if it does not exist.
    3. Verifies that the path is a directory and not a file.
    4. Creates an empty .global-version file in the data directory if it is missing.

    This is typically called during the initial setup or installation of SVM.

    use svm_rs::paths::setup_data_dir;
    
    // Initialize the SVM environment
    setup_data_dir().expect("Failed to setup SVM data directory");
  11. Remove an installed Solc version

    master

    Use remove_version(&Version) to delete a specific version of Solc from the machine. This removes the entire version directory.

    Warning: Removing a version that is currently being installed or is actively being executed is racy. This operation also removes the version's install lock file, which may cause concurrent installations to fail or trigger a re-installation.

    use semver::Version;
    
    let version = Version::parse("0.8.10").unwrap();
    remove_version(&version)?;
  12. Identify the current machine's platform with `platform()`

    master

    The platform() function detects the current operating system and architecture to return a Platform enum variant. This is useful for determining which Solidity compiler binaries are compatible with the host machine.

    use svm_rs::platform::{platform, Platform};
    
    let current_platform = platform();
    match current_platform {
        Platform::LinuxAmd64 => println!("Running on Linux x86_64"),
        Platform::MacOsAarch64 => println!("Running on Apple Silicon"),
        Platform::Unsupported => println!("Platform not supported"),
        _ => println!("Other platform detected"),
    }