Skip to content

nf vars

List the inputs each app takes and the values it would be built with. An app's inputs are the parameters of the function its spec names; nf gives them values from ++name=value on any command that builds the app.

Synopsis

nf vars [OPTIONS] [++name=value ...] [++@file ...]

Declaring Inputs

An app spec (hybrid_apps = ["infra:web"]) may name a function that builds the app. Its parameters are the app's inputs: the annotation is the type, the default is the default, and a parameter without one is required.

from typing import Literal

from nf.core import App, Secret, Stack


def web(env: Literal["dev", "prod"] = "dev", replicas: int = 2, db_password: Secret[str] | None = None) -> App:
    app = App(nf_id="web")
    network = Network(app, "network", cidr="10.0.0.0/16" if env == "prod" else "10.9.0.0/16")
    Compute(app, "compute", network=network, replicas=replicas, db_password=db_password)
    # Each environment keeps its own state
    app.enable_hybrid_execution_s3(bucket="acme-state", key=f"web/{env}.tfstate")
    return app

Inputs are ordinary Python values by the time the function runs, so they can choose anything: names, counts, branches, the state backend. Stacks get them as constructor arguments from the function; nothing in planning or execution sees inputs, so they cost nothing past reading them once. An app built when its module is imported (web = App(...) at module level) takes none: make it a function to take them.

Giving Values

nf apply ++env=prod ++replicas=3
nf plan ++@envs/prod.toml ++replicas=5 --out prod.plan
nf apply --plan-file prod.plan ++@<(printf '{"db_password": "%s"}' "$DB_PASSWORD")
  • ++name=value anywhere on the command line (before a --); ++name alone is true, for a bool input. A later ++name replaces an earlier one.
  • ++@path reads a file of them: TOML when its name ends in .toml, JSON otherwise (.json, or a file from process substitution, ++@<(...)). A later file replaces an earlier one's values.
  • The parameter's default.

The first that has a value wins, in that order. Nothing is read from the environment: every value that shapes a run is on its command line or in a file it names, so a variable exported earlier in a shell cannot retarget a command. A name the app does not take, a required input without a value, or a value that does not convert stops the command before anything is built, with every problem listed. Shells pass ++ arguments as they are, without quoting.

Types

Text from the command line is converted by the annotation:

Annotation Text
str, Path, other classes built from text (Decimal, IPv4Network) as it is
int, float 3, 1.5
bool true/false, yes/no, on/off, 1/0
Literal["dev", "prod"], an Enum (by name or value) one of the choices
list[T], tuple[T, ...], set[T] a,b,c, or JSON (["a,b", "c"])
dict[K, V] JSON
T \| None as T

Values in files are typed already; they are checked against the annotation the same way. Unannotated parameters take the type of their default (replicas=2 is an int).

Groups

A parameter annotated with a dataclass is a group of inputs, one per field, named parameter.field (++db.size=large, or [db] in TOML). When it is the function's only parameter, its fields are named by themselves, so a settings dataclass can declare an app's inputs in one place:

from dataclasses import dataclass
from typing import Literal

from nf.core import App


@dataclass
class Settings:
    env: Literal["dev", "prod"] = "dev"
    replicas: int = 2


def web(settings: Settings) -> App: ...  # nf apply ++env=prod

Secrets

Secret[str] (from nf.core) marks an input that is not given on the command line, where shell history and the process list would show it: ++db_password=... is refused. Give it in a file. Process substitution passes one that never touches disk, and whose value appears in no command line (the shell expands $DB_PASSWORD inside it), as a CI job would from its secret store:

nf apply ++env=prod ++@<(printf '{"db_password": "%s"}' "$DB_PASSWORD")

It is shown as (secret) everywhere nf shows inputs. Type checkers see Secret[str] as str.

A resource attribute set from a secret is masked in plans only when its provider marks the attribute sensitive, as for any other value.

Saved Plans and Runs

nf plan --out records the inputs in the plan (each secret only as a salted digest), and nf apply --plan-file builds the app with them, so the plan applies to the state, providers and region it was made for. A value given again must match: a different one is refused, as is a secret that differs. Secrets are not recorded, so give them again.

Plan and run headers show the inputs (with env=prod replicas=3 db_password=(secret)), as do --json documents (inputs), a run's live status (nf follow, nf tui), and nf tui's header.

Deployment And Teardown Inputs

An input is a deployment input: it shapes what is deployed (env, region, replicas). An apply records the deployment inputs it built the app with in state (secrets as salted digests), and a destroy builds the app with them, so it acts on what was applied: an input it is not given takes the last apply's value, not the default, and one it is given otherwise is refused, saying which. A secret the last apply used must be given again. So must the inputs a destroy needs before it can read state at all: those that say where state is (env in a state key) and those the app's prerequisite checks read, which run before any state is read.

An input that only decides how a run treats what is deployed is a teardown input, marked Teardown[T]. It is never recorded, and a destroy takes it as given:

# coding: nf_autoid

from nf.core import App, Bind, BindContext, LifecycleRules, Stack, Teardown
from nf.providers.hashicorp.google import CloudRunV2Service, GoogleProvider, SqlDatabaseInstance


def web(project: str, env: str = "dev", keep_data: Teardown[bool] = False) -> App:
    with App(nf_id="web") as app, Stack(nf_id=f"web-{env}") as web, BindContext:
        google: Bind = GoogleProvider(project=project, region="europe-west1")
        database: Bind = SqlDatabaseInstance(
            name=f"web-{env}",
            database_version="POSTGRES_16",
            region="europe-west1",
            settings=[{"tier": "db-custom-1-3840"}],
            deletion_protection=False,
            lifecycle=LifecycleRules(retain="customer data") if keep_data else None,
        )
        # Refers to the database, so a destroy keeping it would keep this too: it goes
        service: Bind = CloudRunV2Service(
            name=f"web-{env}",
            location="europe-west1",
            template=[{"containers": [{"image": "europe-docker.pkg.dev/acme/web/web:1", "env": [{"name": "DB", "value": database.output.connection_name}]}]}],
            lifecycle=LifecycleRules(retain=False),
        )
    return app
nf apply --app web ++project=acme-web ++env=prod
nf destroy --app web ++project=acme-web ++keep_data   # env=prod, as applied; the database is kept

A teardown input may change only lifecycle policy (retain, prevent_destroy). Whenever one is given otherwise than its default, nf builds the app again with it at its default and compares every resource, data source, provider and output: one that changes anything else is refused, naming what it changes, as it would change what a destroy acts on. The destroy's notes say which inputs were the last apply's.

A teardown input also suits a value that belongs to one run and changes nothing deployed, such as a change ticket an app check reads (change_ticket: Teardown[str] = ""). As a deployment input, the ticket would be recorded by an apply, and a later destroy given its own would be refused (the last apply was made with change_ticket=CHG-1, not CHG-3). examples/guardrails requires one for every run in prod.

Several Apps

Each command that builds an app passes it the inputs given, and an input it does not take is an error: select the app with --app when apps take different inputs. nf tui is the exception: each app takes the inputs it declares and ignores the rest. A command that builds no app warns of the inputs given to it (Ignored ++env: this command builds no app that takes them).

Legacy apps are synthesized in a process of their own (nf synth): nf passes it the inputs on its standard input, never its command line, and the app is built with them there. A piped file (++@<(...), ++@/dev/stdin) can be read once: nf reads it, and passes what it gave to each app's process.

Options

  • --app TEXT: list this app's inputs (a name it does not take is then an error); every app's otherwise
  • --json: write them as JSON

Examples

# What web takes, and what it would be built with for production
nf vars --app web ++@envs/prod.toml ++@secrets.json

# Every app's inputs, as JSON
nf vars --json
web (hybrid, infra:web)
  NAME          TYPE                     DEFAULT   VALUE      FROM
  env           Literal['dev', 'prod']   dev       prod       envs/prod.toml
  replicas      int                      2         2          default
  db_password   Secret[str] | None       None      (secret)   secrets.json

Notes

  • Variable constructs are Terraform variables (variable blocks in synthesized configuration), apart from inputs.
  • Inputs are given to the function each time the app is built: nf tui rebuilding an app after a code change uses the same ones.