### Platform-aware virtual environment creation with create_venv and select Source: https://github.com/bazel-contrib/rules_uv/blob/main/readme.md Configures `create_venv` to use platform-specific requirement files based on the target execution environment. The `select` function maps Bazel platforms (`@platforms//os:linux`, `@platforms//os:osx`) to the corresponding requirement files, ensuring the correct dependencies are installed for each platform. ```starlark load("@rules_uv//uv:venv.bzl", "create_venv") create_venv( name = "create_venv", requirements_txt = select({ "@platforms//os:linux": ":requirements_linux.txt", "@platforms//os:osx": ":requirements_macos.txt", }), ) ``` -------------------------------- ### sync_venv: Synchronize Existing Virtual Environment Source: https://context7.com/bazel-contrib/rules_uv/llms.txt The sync_venv rule provides a target to synchronize an existing virtual environment with the contents of a 'requirements.txt' file. Unlike 'create_venv', 'sync_venv' updates an existing environment efficiently by only modifying changed packages, preserving existing installations where possible. ```starlark # BUILD.bazel load("@rules_uv//uv:venv.bzl", "sync_venv") sync_venv( name = "sync-venv", requirements_txt = "//:requirements.txt", destination_folder = ".sync-venv", ) sync_venv( name = "sync-venv-with-extras", requirements_txt = "//:requirements.txt", destination_folder = ".venv", site_packages_extra_files = [ "//config:sitecustomize.py", ], ) # Sync virtual environment at .sync-venv # $ bazel run //:sync-venv # Sync with custom site packages # $ bazel run //:sync-venv-with-extras ``` -------------------------------- ### Install rules_uv with Bazel Bzlmod Source: https://github.com/bazel-contrib/rules_uv/blob/main/readme.md Adds the rules_uv dependency to your Bazel project using bzlmod. Ensure you replace "" with the appropriate version of rules_uv. This step requires a Python toolchain, typically provided by rules_python. ```starlark bazel_dep(name = "rules_uv", version = "") bazel_dep(name = "rules_python", version = "") ``` -------------------------------- ### Orchestrate multi-platform requirement generation with multirun Source: https://github.com/bazel-contrib/rules_uv/blob/main/readme.md Utilizes `rules_multirun` to execute multiple `pip_compile` targets sequentially for different platforms. Setting `jobs = 1` ensures that `uv`'s cache can be leveraged across invocations, improving efficiency. This setup generates platform-specific lock files. ```starlark load("@rules_multirun//:defs.bzl", "multirun") load("@rules_uv//uv:pip.bzl", "pip_compile") multirun( name = "generate_requirements_lock", commands = [ ":generate_requirements_linux_txt", ":generate_requirements_macos_txt", ], jobs = 1, ) ``` -------------------------------- ### Multi-platform requirements compilation with pip_compile Source: https://github.com/bazel-contrib/rules_uv/blob/main/readme.md Demonstrates generating platform-specific `requirements.txt` files for Linux and macOS using `pip_compile` with different `python_platform` values. This is often used in conjunction with `rules_multirun` to orchestrate these compilations. ```starlark load("@rules_uv//uv:pip.bzl", "pip_compile") pip_compile( name = "generate_requirements_linux_txt", python_platform = "x86_64-unknown-linux-gnu", requirements_txt = "requirements_linux.txt", ) pip_compile( name = "generate_requirements_macos_txt", python_platform = "aarch64-apple-darwin", requirements_txt = "requirements_macos.txt", ) ``` -------------------------------- ### pip_compile: Customize uv Arguments with args and extra_args Source: https://context7.com/bazel-contrib/rules_uv/llms.txt The pip_compile rule allows customization of uv arguments using the 'args' parameter to override defaults or 'extra_args' to append to them. Default arguments include '--generate-hashes', '--emit-index-url', and '--no-strip-extras'. This is useful for specifying custom index URLs or trusted hosts, or for completely overriding default behaviors. ```starlark # BUILD.bazel load("@rules_uv//uv:pip.bzl", "pip_compile") pip_compile( name = "generate_requirements_txt", requirements_in = "//:requirements.in", requirements_txt = "//:requirements.txt", extra_args = [ "--index-url=https://pypi.internal.company.com/simple", "--trusted-host=pypi.internal.company.com", ], ) # Override all default args pip_compile( name = "generate_requirements_minimal", requirements_in = "//:requirements.in", requirements_txt = "//:requirements_minimal.txt", args = [ "--no-annotate", "--no-header", ], ) # $ bazel run //:generate_requirements_txt ``` -------------------------------- ### pip_compile: Multiple Input Files Compilation (Starlark) Source: https://context7.com/bazel-contrib/rules_uv/llms.txt Supports additional requirement files through the `data` parameter, allowing composition from multiple sources via `-r` directives in the main `requirements.in` file. Dependencies: `rules_uv//uv:pip.bzl`. Inputs: `requirements.in` and additional requirement files. Outputs: `requirements.txt` file. ```starlark # BUILD.bazel load("@rules_uv//uv:pip.bzl", "pip_compile") pip_compile( name = "generate_requirements_txt", requirements_in = "//:requirements.in", requirements_txt = "//:requirements.txt", data = ["//:requirements.test.in"], ) # $ bazel run //:generate_requirements_txt ``` ```plaintext # requirements.in -r requirements.test.in click~=8.1.7 ``` ```plaintext # requirements.test.in pytest>=7.0.0 pytest-cov>=4.0.0 ``` -------------------------------- ### pip_compile: Basic Requirements Compilation (Starlark) Source: https://context7.com/bazel-contrib/rules_uv/llms.txt Compiles a `requirements.in` file into a locked `requirements.txt` with cryptographic hashes for reproducible builds. It automatically creates targets for updating and testing the requirements file. Dependencies: `rules_uv//uv:pip.bzl`. Inputs: `requirements.in` file. Outputs: `requirements.txt` file. ```starlark # BUILD.bazel load("@rules_uv//uv:pip.bzl", "pip_compile") pip_compile( name = "generate_requirements_txt", requirements_in = "//:requirements.in", # default requirements_txt = "//:requirements.txt", # default ) # Run to update requirements.txt # $ bazel run //:generate_requirements_txt # Run to verify requirements.txt is up-to-date # $ bazel test //:generate_requirements_txt_test ``` ```plaintext # requirements.in click~=8.1.7 requests>=2.28.0 ``` -------------------------------- ### create_venv: Add Custom Files to Site-Packages Source: https://context7.com/bazel-contrib/rules_uv/llms.txt The create_venv rule supports injecting custom files into the virtual environment's site-packages directory via the 'site_packages_extra_files' parameter. This is useful for adding files like 'sitecustomize.py' or '.pth' files for custom Python path manipulations or environment configurations. ```starlark # BUILD.bazel load("@rules_uv//uv:venv.bzl", "create_venv") create_venv( name = "create-venv-custom", requirements_txt = "//:requirements.txt", destination_folder = ".venv", site_packages_extra_files = [ "//config:sitecustomize.py", "//config:custom.pth", ], ) # $ bazel run //:create-venv-custom ``` ```python # config/sitecustomize.py import sys import os # Add custom import paths sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lib')) # Configure environment os.environ['APP_ENV'] = 'development' ``` -------------------------------- ### Group Requirements Generation with multirun Source: https://context7.com/bazel-contrib/rules_uv/llms.txt The multirun rule from rules_multirun can be used to execute multiple pip_compile targets sequentially. This is beneficial for generating all platform-specific requirements files efficiently, leveraging uv's caching mechanism by running them one after another. ```starlark load("@rules_multirun//:defs.bzl", "multirun") multirun( name = "generate_requirements_lock", commands = [ ":generate_requirements_linux_txt", ":generate_requirements_macos_txt", ], jobs = 1 ) ``` -------------------------------- ### Create virtual environment target with create_venv Source: https://github.com/bazel-contrib/rules_uv/blob/main/readme.md Sets up a Bazel target to create a Python virtual environment using `uv`. The target uses a specified `requirements.txt` file and the default Python 3 runtime from rules_python. The generated script allows specifying the virtual environment path. ```starlark load("@rules_uv//uv:venv.bzl", "create_venv") create_venv( name = "create_venv", requirements_txt = "//:requirements.txt", # default ) ``` -------------------------------- ### pip_compile: Inline Requirements Compilation (Starlark) Source: https://context7.com/bazel-contrib/rules_uv/llms.txt Compiles inline requirements specified as a list of strings, useful for simple dependencies or dynamic generation. The requirements are written to a temporary file during the build. Dependencies: `rules_uv//uv:pip.bzl`. Inputs: List of requirement strings. Outputs: `requirements.txt` file. ```starlark # BUILD.bazel load("@rules_uv//uv:pip.bzl", "pip_compile") pip_compile( name = "generate_requirements_twine", python_platform = "x86_64-unknown-linux-gnu", requirements_in = [ "twine~=5.1.1", "build>=0.10.0", ], requirements_txt = "requirements_twine.txt", ) # $ bazel run //:generate_requirements_twine ``` -------------------------------- ### Integrate rules_uv with rules_python using MODULE.bazel Source: https://context7.com/bazel-contrib/rules_uv/llms.txt This MODULE.bazel snippet demonstrates how to integrate rules_uv with rules_python. It declares the necessary dependencies and uses pip.parse to configure dependency resolution across different platforms by mapping requirements files to their respective operating systems. ```starlark bazel_dep(name = "rules_uv", version = "0.40.0") bazel_dep(name = "rules_python", version = "0.34.0") pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") pip.parse( hub_name = "pip", python_version = "3.11", requirements_darwin = "//:requirements_macos.txt", requirements_linux = "//:requirements_linux.txt", ) use_repo(pip, "pip") ``` -------------------------------- ### create_venv: Basic Virtual Environment Creation Source: https://context7.com/bazel-contrib/rules_uv/llms.txt The create_venv rule generates a Python virtual environment from a 'requirements.txt' file. It produces a runnable Bazel target that creates the venv using the default Python runtime from rules_python. The default output path for the venv is 'venv'. ```starlark # BUILD.bazel load("@rules_uv//uv:venv.bzl", "create_venv") create_venv( name = "create-venv", requirements_txt = "//:requirements.txt", # default ) # Create venv at default path 'venv' # $ bazel run //:create-venv # Create venv at custom path # $ bazel run //:create-venv -- custom_venv_path ``` -------------------------------- ### pip_compile: Platform-Specific Requirements Compilation (Starlark) Source: https://context7.com/bazel-contrib/rules_uv/llms.txt Generates platform-specific requirements files using the `python_platform` parameter, enabling separate lock files for different operating systems and architectures. Dependencies: `rules_uv//uv:pip.bzl`. Inputs: `requirements.in` file. Outputs: Platform-specific `requirements.txt` files. ```starlark # BUILD.bazel load("@rules_uv//uv:pip.bzl", "pip_compile") pip_compile( name = "generate_requirements_linux_txt", python_platform = "x86_64-unknown-linux-gnu", requirements_in = "//:requirements.in", requirements_txt = "requirements_linux.txt", ) pip_compile( name = "generate_requirements_macos_txt", python_platform = "aarch64-apple-darwin", requirements_in = "//:requirements.in", requirements_txt = "requirements_macos.txt", ) # Generate both platform requirements # $ bazel run //:generate_requirements_linux_txt # $ bazel run //:generate_requirements_macos_txt ``` -------------------------------- ### Bazel Bzlmod configuration for multi-platform Python dependencies Source: https://github.com/bazel-contrib/rules_uv/blob/main/readme.md A bzlmod configuration snippet that integrates with `rules_python` to manage multi-platform Python dependencies. It uses `pip.parse` to define hub names and associate platform-specific requirement files (e.g., `requirements_macos.txt`, `requirements_linux.txt`) with their respective platforms. ```starlark pip.parse( hub_name = "pip", python_version = "3.11", requirements_darwin = "//:requirements_macos.txt", requirements_linux = "//:requirements_linux.txt", ) ``` -------------------------------- ### create_venv: Specify Custom Destination Folder Source: https://context7.com/bazel-contrib/rules_uv/llms.txt The create_venv rule allows specifying a custom path for the virtual environment using the 'destination_folder' parameter. This enables the creation of the venv at a location other than the default 'venv' directory, providing flexibility in project structure. ```starlark # BUILD.bazel load("@rules_uv//uv:venv.bzl", "create_venv") create_venv( name = "create-venv-custom", requirements_txt = "//:requirements.txt", destination_folder = ".venv", ) # Creates virtual environment at .venv # $ bazel run //:create-venv-custom ``` -------------------------------- ### Create pip requirements compilation target with pip_compile Source: https://github.com/bazel-contrib/rules_uv/blob/main/readme.md Defines a Bazel target to compile pip requirements from a `requirements.in` or `pyproject.toml` file into a `requirements.txt` file using `uv`. It also automatically registers a diff test. Optional arguments allow customization of the `uv pip compile` command. ```starlark load("@rules_uv//uv:pip.bzl", "pip_compile") pip_compile( name = "generate_requirements_txt", requirements_in = "//:requirements.in", # default requirements_txt = "//:requirements.txt", # default ) ``` -------------------------------- ### pip_compile: Custom Python Runtime Compilation (Starlark) Source: https://context7.com/bazel-contrib/rules_uv/llms.txt Targets specific Python versions using the `py3_runtime` parameter, enabling separate lock files for different Python interpreter versions within the same workspace. Dependencies: `rules_uv//uv:pip.bzl`. Inputs: `requirements.in` file and specified Python runtime. Outputs: Python version-specific `requirements.txt` files. ```starlark # BUILD.bazel load("@rules_uv//uv:pip.bzl", "pip_compile") pip_compile( name = "generate_requirements_txt_3_10", py3_runtime = "@python_3_10//:py3_runtime", requirements_in = "//:requirements.in", requirements_txt = "requirements_3_10.txt", ) pip_compile( name = "generate_requirements_txt_3_11", py3_runtime = "@python_3_11//:py3_runtime", requirements_in = "//:requirements.in", requirements_txt = "requirements_3_11.txt", ) # $ bazel run //:generate_requirements_txt_3_10 # $ bazel run //:generate_requirements_txt_3_11 ``` -------------------------------- ### pip_compile: Override Dependency Versions with requirements_overrides Source: https://context7.com/bazel-contrib/rules_uv/llms.txt The pip_compile rule supports overriding specific package versions using the 'requirements_overrides' parameter. This parameter takes a file path that is passed to uv via the '--overrides' flag, enabling the resolution of dependency conflicts by forcing specific package versions. ```starlark # BUILD.bazel load("@rules_uv//uv:pip.bzl", "pip_compile") pip_compile( name = "generate_requirements_txt", requirements_in = "//:requirements.in", requirements_txt = "//:requirements.txt", requirements_overrides = "//:overrides.txt", ) # $ bazel run //:generate_requirements_txt ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.