pak: Modern R Package Installer

repository·main·Indexed 21 days ago

https://github.com/r-lib/pak

A fast and safe alternative to install.packages() and devtools::install_github() for R. pak supports multiple sources including CRAN, Bioconductor, GitHub, git repositories, and local files. It features automated system requirement management for supported Linux platforms, dependency tree inspection via pkg_deps_tree(), and tools to identify and install missing system-level libraries.

Tokens
7.8K
Snippets
26
Records
55
Agent score
74%

What's inside pak

  1. Overview of the vendored Mbed TLS subset

    main

    The src/library/zip/src/mbedtls/ directory contains a trimmed subset of Mbed TLS specifically used for WinZip AES (ZIP compression method 99). It provides support for:

    • AES (runtime 128/192/256)
    • SHA-1
    • HMAC-SHA1
    • PBKDF2-HMAC-SHA1

    Technical Details:

    • Upstream Version: 3.6.6 (branch mbedtls-3.6)
    • License: Apache-2.0 (used via the Apache-2.0 OR GPL-2.0-or-later dual license)
    • Configuration: The subset is controlled by include/zip_mbedtls_config.h. This configuration is applied during compilation using the flag -DMBEDTLS_CONFIG_FILE="zip_mbedtls_config.h" (defined in src/Makevars).
    • Included Source Files: library/aes.c, library/sha1.c, library/md.c, library/pkcs5.c, and library/platform_util.c.
  2. Overview of pak for R package installation

    main

    pak is a modern alternative to install.packages() and devtools::install_github() designed to be fast, safe, and convenient. It provides a unified interface for installing R packages from various sources, including:

    • CRAN
    • Bioconductor
    • GitHub
    • URLs
    • git repositories
    • Local files and directories
  3. Manage system requirements with pak

    main

    Many R packages require external system software (e.g., libpq-dev for RPostgres) to function or load correctly. pak automates this by:

    • Looking up required system packages during R package installation.
    • Checking if required system packages are already installed.
    • Automatically installing missing packages if you are running as a superuser or have password-less sudo configured.

    If you are a regular user without sudo privileges, pak will identify missing requirements and print them, but it will not attempt to install them. In such cases, installing source packages that depend on these libraries will likely fail, and binary packages may install but fail to load.

  4. Encryption scheme used by the `zip` package

    main

    The zip package implements WinZip AES encryption, which is the industry standard compatible with 7-Zip, WinZip, modern Info-ZIP, and macOS Archive Utility.

    Supported Specifications:

    • Algorithm: AES-128, AES-192, or AES-256 in CTR mode.
    • Key Derivation: PBKDF2-HMAC-SHA1 with 1000 iterations, using a per-entry random salt.
    • Authentication: HMAC-SHA1 (truncated to 10 bytes) over the ciphertext to ensure integrity.

    Important Limitations:

    • The legacy ZipCrypto (traditional-PKWARE) is not supported. Attempting to use encryption = "zipcrypto" in R will raise an error.
    • The package uses the AE-2 vendor version, which stores the CRC-32 as 0 in headers and relies on the HMAC for integrity.
  5. Build a semantic CLI with high-level elements

    main
    Since version 2.0.0, cli provides a set of semantic tools to build command-line interfaces using high-level elements like headings, paragraphs, lists, alerts, and code blocks. The formatting of these elements can be customized via themes.
  6. How the Environment Variable backend works

    main

    The backend_env is the default fallback on Linux systems without a GUI. It stores secrets in environment variables.

    When an item includes a username, the environment variable name is formatted as service:username (using a colon separator).

    Note: While standard shells may have difficulty reading/writing these, programming languages can interact with them using standard functions like Sys.getenv() and Sys.setenv() in R.

  7. How the Linux Secret Service backend works

    main

    The Secret Service API operates via DBUS. Common implementations include gnome-keyring (GNOME) and KWallet (KDE).

    Important Limitations:

    • GUI Requirement: Linux systems without a GUI typically lack a secret service daemon. On such systems, keyring defaults to the backend_env (environment variable) backend.
    • Interactive Prompts: For some functions, passwords cannot be passed programmatically and must be entered interactively by the system (e.g., when creating a new keyring or unlocking one).
    • Package Unloading Crash: Due to libglib limitations, unloading the keyring package on Linux can leave libglib threads running. Reloading the package after unloading will cause R to crash. To avoid this during development (e.g., when using devtools), run tests directly from the command line instead of using devtools::test() or devtools::load_all().
    R -e 'devtools::test()'
  8. How pak manages system requirements

    main

    Data Sources

    • System Requirements Database: pak uses the database from r-system-requirements.
    • CRAN Packages: pak downloads SystemRequirements fields from https://cran.r-pkg.org/metadata.
    • Bioconductor Packages: pak downloads requirements from GitHub.
    • Other Sources (GitHub/Git): pak extracts SystemRequirements directly from the package DESCRIPTION file.

    Build-time vs Run-time Dependencies

    pak does not currently differentiate between build-time (needed to install from source) and run-time (needed to use the package) dependencies. It will install both types, which typically means it installs development packages (containing header files). While this ensures compatibility, it may consume extra disk space.

    Platform Support

    • Linux: Full support for determining and installing system requirements.
    • Windows: pak does not attempt to install system software on Windows. Most Windows binaries from CRAN/PPM/Bioconductor are statically linked. For source compilation, users should install Rtools and use the pacman tool if necessary.
    • macOS: pak does not currently have system requirement information for macOS. Users needing to compile from source should use Homebrew or CRAN's static library builds.
  9. How Windows Credential Store backend handles multiple keyrings

    main

    The keyring package uses the legacy Windows Credential Store API. Because the legacy API does not natively support multiple keyrings or locking/unlocking, keyring implements a manual layer for non-default keyrings:

    1. Metadata: For every non-default keyring, a credential with target name keyring:: is created containing a Version, Verify (for password validation), and Salt (for SHA256 hashing).
    2. Unlocking: When a keyring is unlocked, the AES key used for encryption is stored in a session credential with the target name keyring::unlocked.
    3. Storage:
      • Default keyring: Credentials use target name :service:username and are not encrypted.
      • Other keyrings: Credentials are encrypted using the keyring's AES key and stored with target name keyring:service:username. The first 16 bytes of the stored item contain the random initialization vector (IV).

    Workflow for set on a non-default keyring:

    1. Check if the keyring exists.
    2. Check if the keyring is unlocked (prompt user if not).
    3. Encrypt the key with the AES key.
    4. Store using target name keyring:service:username.

    Workflow for get on a non-default keyring:

    1. Check if the keyring is locked (prompt user if not).
    2. Retrieve the AES key from keyring::unlocked.
    3. Retrieve and decrypt the key using the AES key.
  10. Suppress cli messages specifically

    main
    In cli 2.1.0, messages (i.e., message conditions) generated by cli are assigned the cliMessage class. This allows developers to suppress cli messages specifically without suppressing all other types of messages.
  11. How macOS Keychain backend handles keyrings

    main

    On macOS, keyrings are stored as files in ~/Library/Keychains. They use the .keychain extension (pre-Sierra) or .keychain-db extension (Sierra and later).

    When a symbolic name (e.g., login) is provided, the backend searches for files in this priority order:

    1. .keychain file
    2. .keychain-db file
    3. If Sierra or later: .keychain-db
    4. Otherwise: .keychain
  12. Install pak

    main

    You can install pak using several methods depending on your needs:

    Install from CRAN

    For the standard released version, use the regular install.packages() command. Note that this may require a C compiler on some platforms.

    Install pre-built binaries

    To install a binary build directly from the r-lib GitHub repository (which can be faster or used when CRAN binaries are unavailable), use the following command:

    Supported Systems for Binaries

    OSCPUR version
    Linuxx86_64R 3.5.0 - R-devel
    Linuxaarch64R 3.5.0 - R-devel
    macOS High Sierra+x86_64R 3.5.0 - R-devel
    macOS Big Sur+aarch64R 4.1.0 - R-devel
    Windowsx86_64R 3.5.0 - R-devel

    Note: For macOS, only the official CRAN R build is supported. Other builds like Homebrew R are not supported.

    # Install from CRAN
    install.packages("pak")
    
    # Install pre-built binaries
    install.packages("pak", repos = sprintf("https://r-lib.github.io/p/pak/stable/%s/%s/%s", .Platform$pkgType, R.Version()$os, R.Version()$arch))