Skip to content

Commit 93bacbd

Browse files
Allow configuring default_venv_backend with an environment variable (#780)
Closes #776
1 parent 5a097cd commit 93bacbd

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

docs/usage.rst

+11-4
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,25 @@ Changing the sessions default backend
128128

129129
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')``.
130130

131-
.. code-block:: console
132131

133-
nox -db conda
134-
nox --default-venv-backend conda
132+
.. tabs::
133+
134+
.. code-tab:: console CLI options
135+
136+
nox -db conda
137+
nox --default-venv-backend conda
138+
139+
.. code-tab:: console Environment variables
140+
141+
NOX_DEFAULT_VENV_BACKEND=conda
135142

136143
.. note::
137144

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

142-
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.
149+
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.
143150

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

nox/_options.py

+1
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ def _tag_completer(
419419
"--default-venv-backend",
420420
group=options.groups["environment"],
421421
noxfile=True,
422+
default=lambda: os.environ.get("NOX_DEFAULT_VENV_BACKEND"),
422423
merge_func=_default_venv_backend_merge_func,
423424
help=(
424425
"Virtual environment backend to use by default for Nox sessions, this is"

tests/test_main.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,33 @@ def test_main_list_option_from_nox_env_var(monkeypatch, var, option, env, values
259259
config = execute.call_args[1]["global_config"]
260260
config_values = getattr(config, option)
261261
assert len(config_values) == len(values)
262-
for value in values:
263-
assert value in config_values
262+
assert all(value in config_values for value in values)
263+
264+
265+
@pytest.mark.parametrize(
266+
"options,env,expected",
267+
[
268+
(["--default-venv-backend", "conda"], "", "conda"),
269+
([], "mamba", "mamba"),
270+
(["--default-venv-backend", "conda"], "mamba", "conda"),
271+
],
272+
ids=["option", "env", "option_over_env"],
273+
)
274+
def test_default_venv_backend_option(monkeypatch, options, env, expected):
275+
monkeypatch.setenv("NOX_DEFAULT_VENV_BACKEND", env)
276+
monkeypatch.setattr(sys, "argv", [sys.executable, *options])
277+
with mock.patch("nox.workflow.execute") as execute:
278+
execute.return_value = 0
279+
280+
# Call the main function.
281+
with mock.patch.object(sys, "exit") as exit:
282+
nox.__main__.main()
283+
exit.assert_called_once_with(0)
284+
assert execute.called
285+
286+
# Verify that the default venv backend is set in the config.
287+
config = execute.call_args[1]["global_config"]
288+
assert config.default_venv_backend == expected
264289

265290

266291
def test_main_positional_args(capsys, monkeypatch):

0 commit comments

Comments
 (0)