Skip to content

Make env var options configurable via pyproject.toml #1175

@lmmx

Description

@lmmx

Splitting this out from #785 as it is a separate ask:

I was expecting to be able to use these config options in pyproject.toml

Originally posted by ulgens in #785

The env vars should also be configurable via pyproject.toml settings


Config story

We currently have src/config.rs where .pre-commit-config.yaml is read in to define both the config schema (the concern of this issue) and the hooks to be provisioned (to be handled separately in #785).

We need to now use it to intercept the env vars we currently directly take from the env in src/env_vars.rs, and instead have config.rs be our source of truth for env configurable vars (i.e. they can be set in the env but overridden by the config).

  • Note that this has historically been a deliberate decision to restrict configuration of prek's predecessor pre-commit, as in issues like #2696:

    nope, it can only be set through the command line or an environment variable

This follows the hierarchical control of libraries like pydantic-settings or figment (both these libraries refer to the sources of configuration settings the "providers"

  • "Hierarchical" here means that the CLI flags overrule the env vars if both are set

The hierarchy would be:

  1. CLI flags
  2. pyproject.toml
  3. .pre-commit-config.yaml
  4. Env vars

That's the global hierarchy though: we're going to start with a separation of config (pyproject.toml/env vars) from hook definitions (just .pre-commit-config.yaml for now).

We have one config item that can be set by both CLI flags and env vars:

  • --color = PREK_COLOR

That's configured in the clap CLI arg parser itself via the env attribute

prek/src/cli/mod.rs

Lines 144 to 152 in 3ff564e

/// Whether to use color in output.
#[arg(
global = true,
long,
value_enum,
env = EnvVars::PREK_COLOR,
default_value_t = ColorChoice::Auto,
)]
pub(crate) color: ColorChoice,

This gets quite complicated: I am wondering if maybe figment would be a good solution for us...

prek.toml

A follow-on PR can introduce prek.toml, which would mean we had 5 potential 'providers'.

  • I'd let prek.toml override pyproject.toml like how .cargo/config.toml overrides Cargo.toml.

Metadata

Metadata

Assignees

No one assigned

    Labels

    thinkingI'm thinking on this

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions