Every Python team eventually hits the same wall: the test matrix grows, conda users and pip users must coexist, docs and lint jobs join the party, and “just run pytest” quietly becomes a shell script nobody dares touch. nox is the Python-native answer. You write a plain noxfile.py, decorate functions with @nox.session, and nox turns them into isolated, reproducible runs with their own interpreter and environment. It sits one abstraction above pytest, one below CI, and has stayed famously readable while growing from a tox alternative into a platform that understands eight environment backends.
What makes nox worth reading is how little machinery it needs. The entire session model hangs off one module-level dict; discovery is a single function call; dependency ordering is a recursive topological sort whose docstring admits it is sized for “noxfile-sized dependency graphs”; parallel scheduling under -j is a thread pool spawning child nox processes rather than a bespoke worker framework. Every stage is a plain function receiving the previous stage’s return value, which is why the central loop is forty lines long and testable without mocks.
This tour walks the real source on the main branch (version 2026.10.20 per pyproject.toml): the decorator and its registry, the manifest that expands python=[...] lists and @nox.parametrize into a queue, the resolver behind requires=, SessionRunner.execute as the beating heart, and the environment layer that speaks venv, virtualenv, uv, conda, mamba, micromamba, rattler and “no environment at all”. Every node in the diagram links straight into the file it names.
The whole engine at a glance: argv becomes a NoxConfig, a ten-task loop turns the decorator registry into a filtered, dependency-ordered manifest, and SessionRunner drives each session through one of eight environment backends.
Reading the overview from left to right, these are the anchor points worth having in your head before diving into the individual modules:
_REGISTRY, L41 — the entire discovery model is one dict mapping session names to wrapped functions.session_decorator, L95 — the branch that makes both@nox.sessionand@nox.session(...)legal syntax.Func, L67 — the wrapper that carriespython,tags,requires,venv_backendand friends alongside the user’s function.generate_calls, L187 — where one parametrized function becomes oneCallobject per parameter combination.execute, L54 — the task loop that chains ten plain functions and treats any integer return as an exit code.lazy_stable_topo_sort, L37 — the lazy, stable topological sort that ordersrequires=prerequisites.run_manifest, L436 — the sequential run loop; parallel mode is handed off just above it._create_venv, L1166 — the point where each session resolves and builds its environment.ALL_VENVS, L1109 — the table of eight backends, fromvenvtorattlertonone.
The decorator that keeps a dict
The first surprise in nox/registry.py is how small the whole authoring surface is. session_decorator accepts the function positionally; if func is None, the decorator was used with parentheses, and it returns a functools.partial of itself with the captured keyword arguments (L95-109). That single branch is why @nox.session and @nox.session(...) are both valid — no metaclass, no descriptor protocol, just partial application. The wrapped result is a Func instance stored under name or func.__name__; registering the same name twice emits a FutureWarning and overrides the old entry, with a message promising the hammer will eventually drop (L139-146).
Func itself, in nox/_decorators.py, is a thin callable wrapper whose __init__ (L67-94) records everything a session might customize: python, reuse_venv, name, venv_backend, venv_params, tags, default, requires, download_python, allow_parallel. Two details show the polish. The requires property formats {python} placeholders at access time, because a per-interpreter copy can carry a different interpreter than its parent (L120-124). And copy builds a genuine new function object through types.FunctionType, passing along code object, globals, defaults and closure (L41-54) — so the python=[...] fan-out gives each version its own function instead of a leaky alias.
Parametrization follows the same plain-objects philosophy. nox/_parametrize.py defines Param, whose call_spec is a zip of names and values (L67-69); parametrize_decorator splits "arg,arg2" on commas (L145-146); stacked decorators multiply via the Cartesian product in update_param_specs (L192-205). When the manifest expands the session, each combination becomes a Call via generate_calls (L187), stamped with a printable session_signature (L176-177).
Filtering, keywords and one clever AST trick
The Manifest in nox/manifest.py bridges the registry and execution with three lists: _all_sessions, _queue, _consumed (L74-76). Iteration pops the queue, so a session can enqueue more work mid-run — notify() re-appends an existing session (L404-442). Construction happens in make_session (L300-402): a python list fans out into per-version copies (L350-356), a parametrized function becomes one SessionRunner per Call (L373-374), and an empty parameter list produces a placeholder session that just calls session.skip (L394-399).
Selection then filters the queue. Names, defaults, tags and interpreters each get a small method, but -k is the interesting one: keyword_match evaluates a pytest-style expression with eval over a KeywordLocals mapping where a name matches as a substring of any session name, signature or tag (L457-458, eval at L471). Signature comparison is more specific still: _normalize_arg parses a signature’s arguments with ast and compares ast.dump output, so tests-3.1 never selects tests-3.10 (L492-503).
Dependencies turn the flat queue into an ordered one. add_dependencies (L239-298) synthesizes fake parent sessions whose requires point at each parametrized child, builds a graph keyed by signature, and asks lazy_stable_topo_sort for the order (L286-287). The resolver is recursive depth-first search over an ordered dict used as a walk set; cycles raise CycleError carrying the exact node path. Its docstring is candid about scope: unsuitable for large graphs, ideal for noxfile-sized ones (L107-108).
From registry dict to exit code in ten tasks
Entry is nox/_cli.py. After parsing argv, handling --version and negotiating script mode, _main resets the registry and calls execute_workflow (L473-474), which hands workflow.execute a tuple of exactly ten tasks (L79-90): load_nox_module, merge_noxfile_options, discover_manifest, honor_usage_request, filter_manifest, honor_list_request, run_manifest, print_summary, create_report, final_reduce. The loop in nox/workflow.py passes each task the previous return value and aborts the moment one returns an int (L54-65) — so every early exit, from “file not found” to “nothing selected”, is a plain return 2 or return 3, and Ctrl-C maps to exit code 130 (L66-67).
Some build facts worth stealing. Status is an enum with deliberately odd values — ABORTED = -1, FAILED = 0, SUCCESS = 1, SKIPPED = 2 (sessions.py L143-147) — so Result truthiness aligns with success. Configuration is ranked by a Source enum, DEFAULT = 0 through COMMAND_LINE = 3 (nox/_option_set.py L63-69), letting _merge.finalize settle -R-beats---reuse-venv precedence declaratively (nox/_merge.py L61-95). And nox.needs_version is extracted by pattern-matching an ast.Assign node instead of executing the noxfile, scanning text with a utf-8-sig decode to tolerate BOMs (nox/_version.py L52-78).
The run has two modes. Sequentially, run_manifest is a twenty-line loop: for session in manifest: then session.execute() (nox/tasks.py L436-449), honoring --stop-on-first-error (L453). With -j, nox lazily imports nox._parallel and hands the manifest over (L424-428) — but only if a selected session opted in via allow_parallel=True or --allow-parallel; otherwise it warns and runs sequentially (L413-423). The scheduler precomputes each node’s envdir and enforces three rules: sessions sharing an envdir never overlap (they would race on one virtualenv, L565), non-opting sessions run exclusively, and failed prerequisites abort their transitive dependents in place through a fixpoint loop (L552-587). Each child is a full nox process re-entering main() with --no-dependencies, output buffered and printed as one block when it finishes. Then print_summary renders humanized durations via humanize.naturaldelta, create_report dumps JSON, and final_reduce collapses everything to 0 or 1 (L535-547).
Eight environments behind one call
SessionRunner.execute (sessions.py L1189) is where everything converges. It first checks direct dependencies: a prerequisite without a passing Result yields an aborted_prerequisite result without running (L1194-1198). Then it chdirs beside the noxfile, calls _create_venv (L1166), builds a Session facade — a __slots__ object holding just the runner (L174) — stamps NOX_CURRENT_SESSION (L1207), and invokes the user’s function (L1208). Exception handling is an explicit map: missing interpreters become FAILED or SKIPPED per error_on_missing_interpreters, quits become ABORTED, skips become SKIPPED, CommandFailed becomes FAILED, and unexpected exceptions log a traceback before failing (L1215-1263).
Environment selection lives in nox/virtualenv.py. get_virtualenv accepts a fallback chain like "uv|virtualenv" — the repo’s own noxfile.py sets exactly default_venv_backend = "uv|virtualenv" (noxfile.py L39) — enforces that only optional backends may carry a fallback, and picks the first backend whose dependency is importable (L1138-1149). ALL_VENVS (L1109-1118) maps eight names: conda, mamba, micromamba share CondaEnv; rattler wraps py-rattler; venv, virtualenv and uv are all VirtualEnv with different commands; none is PassthroughEnv. VirtualEnv.create writes a .gitignore and a CACHEDIR.TAG beside .nox so backup tools skip it (L1055-1057), then dispatches: virtualenv runs sys.executable -m virtualenv --no-periodic-update, uv runs uv venv -p ... adding --clear from uv 0.8 (L1088-1089), defaulting to python -m venv. A missing interpreter is fetched by _install_python — via uv’s downloader from uv 0.4.16, otherwise python-build-standalone (L1016-1044).
The conda family is a first-class citizen: CondaEnv parses channels and python specs into a conda create invocation, and noxfile.py marks conda_tests default whenever a conda binary is on PATH (noxfile.py L123). Inside a session, session.install drives pip (or uv pip) against the session environment while conda_install matches the backend. Script mode completes the story: a PEP 723 noxfile has its TOML block extracted by a regex straight from the PEP (nox/project.py L55-57); nox verifies dependencies and requires-python, builds a _nox_script_mode environment, installs, and os.execle re-execs itself (L361-378). The repo dogfoods this — noxfile.py opens with #!/usr/bin/env -S uv run --script.
Honest Limits
The design has edges worth knowing. The dependency resolver is recursive and warns against large graphs (nox/_resolver.py L107-108); a noxfile is fine, a monorepo-wide DAG is not. Enjoyed this post? Never miss out on future posts by following us -k selection is an eval over a guarded locals mapping (nox/manifest.py L471) — an expression language, not arbitrary code, but still eval. Parallel execution is strictly opt-in per session: without allow_parallel, -j degrades to sequential with a warning — safe, but surprising if you expected free speedups (nox/tasks.py L413-423). Duplicate session names only FutureWarning and override today (nox/registry.py L139-146), so a typo can shadow a real session. Parallel children re-enter main() with --no-dependencies, so notify()-style queue mutations never cross process boundaries. And nox caches environments, not outcomes: every selected session re-executes its body each run; reuse_venv spares only the environment rebuild. None of this is hidden — each limit is documented in the code you just toured — but together they mark the boundary between what nox is (a readable, honest session runner) and what it is not (a distributed job scheduler).