spietras / rules_conda

Rules for creating conda environments in Bazel :green_heart:
https://spietras.github.io/rules_conda
MIT No Attribution
26 stars 11 forks source link
bazel conda python

rules_conda

[![Running tests](https://github.com/spietras/rules_conda/actions/workflows/test.yml/badge.svg)](https://github.com/spietras/rules_conda/actions/workflows/test.yml) [![Deploying docs](https://github.com/spietras/rules_conda/actions/workflows/docs.yml/badge.svg)](https://github.com/spietras/rules_conda/actions/workflows/docs.yml)

Rules for creating conda environments in Bazel 💚

For more info see the docs or the example.

Requirements

rules_conda don't have any strict requirements by themselves.

Just make sure you are able to use conda.

Quickstart

Add this to your WORKSPACE file and put appropriate values taken from chosen release:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_conda",
    sha256 = "...",  # copy from release
    url = "...",  # copy from release
)

load("@rules_conda//:defs.bzl", "conda_create", "load_conda", "register_toolchain")

load_conda(quiet = False)

conda_create(
    name = "env",
    environment = "@//:environment.yml",
    quiet = False,
)

register_toolchain(env = "env")

After that, all Python targets will use the environment specified in register_toolchain.

See below for more advanced example.

Advanced example

This example shows all possibilities of rules_conda:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_conda",
    sha256 = "...",  # copy from release
    url = "...",  # copy from release
)

load("@rules_conda//:defs.bzl", "conda_create", "load_conda", "register_toolchain")

load_conda(
    conda_version = "4.10.3",  # version of conda to download, default is 4.10.3
    installer = "miniforge",  # which conda installer to download, either miniconda or miniforge, default is miniconda
    install_mamba = True,  # whether to install mamba, which is a faster drop-in replacement for conda, default is False
    mamba_version = "0.17.0",  # version of mamba to install, default is 0.17.0
    quiet = False,  # True if conda output should be hidden, default is True
    timeout = 600,  # how many seconds each execute action can take, default is 3600
)

conda_create(
    name = "env",  # name of the environment
    environment = "@//:environment.yml",  # label pointing to environment configuration file
    use_mamba = True,  # Whether to use mamba to create the conda environment. If this is True, install_mamba must also be True
    clean = False,  # True if conda cache should be cleaned (less space taken, but slower subsequent builds), default is False
    quiet = False,  # True if conda output should be hidden True, default is True
    timeout = 600,  # how many seconds each execute action can take, default is 3600
)

register_toolchain(env = "env")

Who should use this?

These rules allow you to download and install conda, create conda environments and register Python toolchain from environments. This means you can achieve truly reproducible and hermetic local Python environments. Only Python 3 is supported.

Pros:

Cons:

So I think these rules suit you if: