Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow configuring default backend with an environment variable #780

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,25 @@ Changing the sessions default backend

By default Nox uses ``virtualenv`` as the virtual environment backend for the sessions, but it also supports ``uv``, ``conda``, ``mamba``, and ``venv`` as well as no backend (passthrough to whatever python environment Nox is running on). You can change the default behaviour by using ``-db <backend>`` or ``--default-venv-backend <backend>``. Supported names are ``('none', 'uv', 'virtualenv', 'conda', 'mamba', 'venv')``.

.. code-block:: console

nox -db conda
nox --default-venv-backend conda
.. tabs::

.. code-tab:: console CLI options

nox -db conda
nox --default-venv-backend conda

.. code-tab:: console Environment variables

NOX_DEFAULT_VENV_BACKEND=conda

.. note::

The ``uv``, ``conda``, and ``mamba`` backends require their respective
programs be pre-installed. ``uv`` is distributed as a Python package
and can be installed with the ``nox[uv]`` extra.

You can also set this option in the Noxfile with ``nox.options.default_venv_backend``. In case both are provided, the commandline argument takes precedence.
You can also set this option with the ``NOX_DEFAULT_VENV_BACKEND`` environment variable, or in the Noxfile with ``nox.options.default_venv_backend``. In case more than one is provided, the command line argument overrides the environment variable, which in turn overrides the Noxfile configuration.

Note that using this option does not change the backend for sessions where ``venv_backend`` is explicitly set.

Expand Down
1 change: 1 addition & 0 deletions nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ def _tag_completer(
"--default-venv-backend",
group=options.groups["environment"],
noxfile=True,
default=lambda: os.environ.get("NOX_DEFAULT_VENV_BACKEND"),
merge_func=_default_venv_backend_merge_func,
help=(
"Virtual environment backend to use by default for Nox sessions, this is"
Expand Down
29 changes: 27 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,33 @@ def test_main_list_option_from_nox_env_var(monkeypatch, var, option, env, values
config = execute.call_args[1]["global_config"]
config_values = getattr(config, option)
assert len(config_values) == len(values)
for value in values:
assert value in config_values
assert all(value in config_values for value in values)


@pytest.mark.parametrize(
"options,env,expected",
[
(["--default-venv-backend", "conda"], "", "conda"),
([], "mamba", "mamba"),
(["--default-venv-backend", "conda"], "mamba", "conda"),
],
ids=["option", "env", "option_over_env"],
)
def test_default_venv_backend_option(monkeypatch, options, env, expected):
monkeypatch.setenv("NOX_DEFAULT_VENV_BACKEND", env)
monkeypatch.setattr(sys, "argv", [sys.executable, *options])
with mock.patch("nox.workflow.execute") as execute:
execute.return_value = 0

# Call the main function.
with mock.patch.object(sys, "exit") as exit:
nox.__main__.main()
exit.assert_called_once_with(0)
assert execute.called

# Verify that the default venv backend is set in the config.
config = execute.call_args[1]["global_config"]
assert config.default_venv_backend == expected


def test_main_positional_args(capsys, monkeypatch):
Expand Down
Loading