Skip to content

nf.sdk

nf.sdk is every nf command as a Python API. The nf CLI is built on it: each command parses its options, calls the API and prints the result. Anything nf does, a program can do the same way, with the same settings and app inputs, and get structured results back.

Install

uv add netherframe.sdk
pip install netherframe.sdk

netherframe.cli depends on it, so a project with the CLI has it already.

A project and its commands

from nf.sdk import Project


def deploy() -> None:
    # Runs in a project directory that declares a `web` app
    project = Project(".", settings={"max_concurrent_resources": 20}, inputs={"env": "prod"})

    plan = project.plan(app="web", plan_out="prod.plan")
    if plan.has_changes:
        run = project.apply(app="web", plan_file="prod.plan", auto_approve=True)

    report = project.drift.check(paths=("infra",))
    for app in project.vars():
        print(app.app, [(item.name, item.value) for item in app.inputs])

Project(root) is the directory a project's pyproject.toml is in (default the current one). Each nf command is a method of it, named as the command:

Command Method
nf plan --app web --out p.json project.plan(app="web", plan_out="p.json")
nf drift check infra project.drift.check(paths=("infra",))
nf list apps project.list.apps()
nf discover list resource-types aws project.discover.list.resource_types("aws")
nf import ... project.import_(...)
nf tui --app web project.tui(app="web")
nf tui follow project.tui.follow()
nf tui --record s.nftui / nf tui replay s.nftui project.tui(record="s.nftui") / project.tui.replay("s.nftui")

A command's path names the method (- as _, a Python keyword with _ after it), and each of its options is a keyword argument named as the option's parameter, usually the option without its dashes (--auto-approve is auto_approve; --out is plan_out, --view is view_name; help(project.plan) lists them). A test of the CLI (test_sdk_parity) fails when a command, or an option of one, is missing from the API, so the two cannot drift apart. The only options the API leaves out are those that only say how the CLI shows a result (--json, for one: results are structured already); each module of nf.sdk lists them, with why, in its CLI_ONLY table.

Results and errors

Commands return results and raise errors; they never print and never exit. All public result dataclasses are re-exported from nf.sdk.types. For async callers, nf.sdk.AsyncProject exposes the same command surface using anyio without mutating the process working directory (Project uses a ContextVar project root rather than os.chdir()).

  • A result is a dataclass (or a core type, such as an apply's ExecutionResult). Where the CLI prints --json, the result's to_dict() is that document.
  • An exit status that is not an error (3 for changes/drift, 4 for incomplete/deferred applies) is a field of the result.
  • A command that cannot do what it was asked raises an NfError (the CLI's Error: <message>), with stable machine-readable code (NF_ERROR, NF_USAGE, NF_PROJECT, NF_NOT_FOUND, NF_AMBIGUOUS, NF_INPUTS, NF_DECLINED, NF_RUN_FAILED), details, exit_code, and .to_dict(). Its kinds: UsageError (exit 2), ProjectError (pyproject.toml or [tool.nf] missing or invalid), NotFound (an app, stack, run or file), Ambiguous, InputsError (app inputs that cannot be given), Declined (a change not approved) and RunFailed (a run that ended without doing what it was asked; result says how far it got).
  • What the CLI asks a person becomes an argument: an approval is auto_approve=True or an approve callback, progress an on_progress callback, a choice the argument that makes it.

Settings and inputs

settings are nf's flags, by key: {"graph_lease_ttl": 300} is nf --graph-lease-ttl 300. Project checks them against the table of settings (nf.sdk.SETTINGS; see Settings) and names every unknown key or bad value at once. They are looked up as for the CLI: a setting given here wins over [tool.nf.config], which wins over the App's argument. Nothing is read from the environment.

inputs are the app factory's parameters as Python values: {"env": "prod", "replicas": 3} is ++env=prod ++replicas=3. They are checked against the parameters' annotations (not parsed from text) and may include secrets, which no shell history keeps. See nf vars.

project.using(settings=..., inputs=...) is the project with more of either:

from nf.sdk import Project


def plan_staging(project: Project) -> None:
    staging = project.using(inputs={"env": "staging"})
    staging.plan(app="web")

Threads and directories

A command's settings and inputs are its own: they follow it into the threads Netherframe starts for it, and calls in different threads keep theirs apart.

A call's project root is its own as well: the call binds its Project's root (a ContextVar), never changes the process's working directory, and so runs alongside calls on other projects. Netherframe resolves relative paths against that root, whatever the working directory: app modules, output directories, state and plan files, baselines, nf.lock, and the files and directories settings name (provider_cache_dir, a provider_mirrors directory, ...), from [tool.nf.config], Project(settings=...) or flags alike. Providers run in the root too, as Terraform runs them in the root module's directory, so a relative path a provider reads (a local_file, an external program, a kubeconfig) is the project's. For nf, the root is the working directory, so nothing changes there.

A project's root is put first on sys.path when its apps are loaded and stays there, so the modules an app imports while it is built, planned or applied are found. sys.path and sys.modules are the process's: projects used in one process should not have modules of the same name (helpers) that their apps import while another project is running.

The process's working directory is still the one Python itself uses: app code that opens a file by a relative path (open("policy.json"), Path("templates")) gets the file in the working directory, which is not the project's when it is used through Project(root) from another directory. Resolve such paths from the module that uses them:

from pathlib import Path

POLICY = Path(__file__).parent / "policy.json"

Interactive commands

project.tui(...), project.tui.follow(...) and project.tui.replay(...) run the terminal UI on the caller's terminal, as nf tui does, and return when it is closed; they need netherframe.cli, which has it. project.follow(...) follows a run elsewhere and calls on_status and on_event as it goes.