Writing a module
Use the public Nix SDK to contribute settings, actions, services, and Studio UI.
Start with a flake and a module. The module receives the project's pkgs, config, and lib.stackpanel; it does not import any files from Stackpanel's source tree.
To start with editor support, run these commands in an empty directory with flake-enabled Nix:
nix flake init -t github:darkmatter/stackpanel#module
nix flake lock ./devIn a Git repository, track the starter files before locking. Commit dev/flake.lock, then open and trust the module root in VS Code with Nix IDE or Zed with the Nix extension. The workspace launches nixd and loads option descriptions from that lock; it needs nix on the editor’s PATH, not a global nixd installation. The published module has no SDK input: consumers supply their own Stackpanel version. Update the editor pin with nix flake update stackpanel --flake ./dev and restart the language server when you adopt a newer SDK.
# flake.nix
{
description = "Workspace report";
outputs = _: { stackpanelModules.default = import ./module.nix; };
}# module.nix
{ lib, pkgs, config, ... }@args:
let
fields = {
title = lib.stackpanel.spField.string {
index = 1;
description = "Report title";
default = "Workspace";
ui.label = "Title";
};
};
report = pkgs.writeShellApplication {
name = "workspace-report";
runtimeInputs = [ pkgs.jq ];
text = ''jq -c '{title: (.title // "Workspace")}' '';
};
in
lib.stackpanel.mkModule {
name = "report";
meta = { name = "Workspace report"; category = "development"; icon = "clipboard-list"; };
settings = fields;
readme = ./README.md;
packages = [ report ];
actions.report = {
label = "Run report";
script = lib.getExe report;
argsSchema = {
type = "object";
properties.title.type = "string";
additionalProperties = false;
};
outputMode = "json";
};
} argssettings becomes a typed Nix option tree and a runtime JSON Schema. There is no per-extension .proto generation step. mkModule registers the metadata even while the module is disabled. Use its config = cfg: { ... }; for enabled behavior. For files, scripts, tasks and services contributed there, set module = "report"; so the agent can show the outputs without guessing from names.
You don't need to write a UI. Studio builds the configuration form from settings, lists the module's actions with their argument forms, attributes outputs to the module, and shows its README. Start there.
Actions and daemons
An action's executable reads a JSON object on stdin. The agent validates it against argsSchema. outputMode = "stream" sends stdout, stderr and the exit code; "json" expects one JSON document on stdout. Never form a shell command from a panel field. Use confirm on actions that modify project or external state. A table or status panel can reference a read-only JSON action with dataAction; the user runs it to refresh the panel.
A service can opt into the local HTTP/WebSocket proxy with module = "report"; proxy = { enable = true; port = 9070; }; on the evaluated stackpanel.services.<name> entry. This declares an existing module-managed daemon, not an arbitrary hostname. Bind it to loopback. Anyone using the extension UI may be able to call this service; do not proxy an unrestricted admin API.
App extensions
Use appOptions for per-app settings and panels.mkPanelFromSpFields to make an app form. A runtime registers appRuntimes, build derivations under appBuildProviders.<runtime>.<app>, a container builder if appropriate, and the resolved deployment command. A framework registers appFrameworks.<framework> with its compatible runtimes, default tasks, and deployment metadata. The registry's Rust and Next.js modules demonstrate the two different contracts.
Author a panel in Nix
Inside lib.stackpanel.mkModule, give each widget a name and one kind. In the starter’s module.nix, typing ui.widgets.appCount.me suggests metric. Inside that widget, la suggests label; hovering shows its type and documentation. A single widget needs no explicit root:
ui.widgets.appCount.metric = {
label = "Configured apps";
value = builtins.length (builtins.attrNames config.stackpanel.apps);
};For multiple widgets, list child IDs in display order on a stack or section. The compiler infers the only widget without a parent as the root; setting ui.root explicitly does not hide disconnected widgets. Each kind also has a lib.stackpanel.ui constructor for composing fragments:
ui.widgets = {
report.section = { title = "Workspace report"; children = [ "hint" "run" ]; };
hint.text.content = "Inspect the current workspace configuration.";
run = lib.stackpanel.ui.actions { ids = [ "report" ]; };
};The option path is the editor-friendly form: nixd can describe run.actions.ids without inferring a function argument. A larger layout can live in ui = import ./studio.nix { inherit lib config; };, but a bare imported attrset does not necessarily give nixd the same enclosing option path.
| Widget kind | Fields |
|---|---|
stack, section | Group children. stack.direction is vertical or horizontal; section.title labels the group. |
text, badge, metric | content; text; or label and a string/number value. |
table | Ordered columns with key and label, and rows of scalar cells. |
actions | Optional ids. Omit to show all declared actions of the owning module; an empty list shows none. |
appPicker | apps (app IDs), optional label, and value bound to local state. |
statusView | Literal action and optional app. Runs on request; results stay inside the panel. |
Only expose data intended for display, not all of config. lib.stackpanel.ui.read builds a read-only binding; bindLocal builds a picker’s writable binding. For example:
ui = {
state.data.appIds = [ "api" "web" ];
state.local.app = "api";
widgets = {
health.stack.children = [ "target" "status" ];
target.appPicker = {
apps = lib.stackpanel.ui.read "/data/appIds";
value = lib.stackpanel.ui.bindLocal "app";
};
status.statusView = { action = "status"; app = lib.stackpanel.ui.read "/local/app"; };
};
};Nix turns the layout into the json-render document Studio consumes. There is no contributor codegen step. Tools may still supply a raw { root, elements, state? } document or ui = ./studio.json;; a bare file path is read as JSON. Do not mix raw elements and typed widgets. Nix catches missing fields, typos and invalid trees; Studio still validates the evaluated document, and the agent still verifies live actions and targets.
Load once executes for the selected app. Updating on selection changes is a separate, stoppable opt-in, not permission to poll in the background. The agent binds live calls to the project, panel, evaluated spec and executable. A changed grant requires new approval. An action with a confirmation requirement belongs in Actions, never in StatusView. A status command can use the network: approving the command is not a per-host firewall for its process.
Placing a panel
For another surface, use panels.report-summary = { title = "Report"; slot = "overview"; spec.widgets.run.actions.ids = [ "report" ]; }; inside mkModule. The same widget declarations work in any slot. mkModule supplies the owner; directly registered stackpanel.panels also need module. No type is needed with spec.
| Slot | Surface |
|---|---|
module | Module detail and filtered panels (default) |
sidebar | Navigation to /studio/m/<module-id>; nav selects the section |
overview, configuration, setup | Summary cards, settings, onboarding |
app:runtime, app:framework | App runtime and compatible framework configuration |
app:container, app:deployment, app:tab | App-specific tabs |
Stock forms and tables can also use actions, rowActions and tabs. Reference only actions and child panels of the same module. A secret-reference field carries a reference name, never decrypted secret data.
Use the same Web Components from HTML
Use a sandboxed HTML panel only when the document cannot express the interaction. The SDK exports actual browser elements; contributors do not import Studio's React components. Place index.html, panel.js and the SDK's prebuilt dist/extension-sdk.js in the evaluated static package. Load a local module script, not a CDN or inline handler:
<sp-app-picker id="target" label="Fly app"></sp-app-picker>
<sp-status-view id="status" action="status"></sp-status-view>
<script type="module" src="./panel.js"></script>// panel.js — or import from "@stackpanel/extension-sdk/elements" in a bundler.
import { registerPanelElements } from "./extension-sdk.js";
registerPanelElements();
const picker = document.querySelector("#target");
const status = document.querySelector("#status");
picker.apps = ["api", "web"];
picker.value = "api";
const select = () => { status.args = { app: picker.value }; };
picker.addEventListener("change", select);
select();sp-metric takes label and value; sp-data-table takes columns and rows as DOM properties. sp-action takes an action ID and optional args property. Neither an element nor a synthetic DOM event authorizes a request. Module HTML executes in its own iframe; register only Stackpanel's elements on the Studio page.
Sandbox permissions
Declare the bundle and the smallest set of capabilities it needs:
stackpanel.panels.report-ui = {
module = "report";
title = "Report viewer";
type = "PANEL_TYPE_CUSTOM";
slot = "sidebar";
uiBundle = { package = reportUi; entry = "index.html"; permissions = [ "actions:run" ]; actionIds = [ "report" ]; };
};The local agent serves the evaluated bundle. Studio runs it in an opaque-origin iframe with scripts enabled, without same-origin access. Bundle JavaScript and styles locally; use the @stackpanel/extension-sdk bridge instead of fetching the agent directly:
import { getContext, runAction } from "@stackpanel/extension-sdk";
const context = await getContext();
if (context.permissions.includes("actions:run")) {
const result = await runAction("report", { title: "Nightly" });
if (result.exitCode !== 0) throw new Error(result.stderr);
document.body.textContent = JSON.stringify(result.json);
}Request the fewest permissions needed: config:read, config:patch, actions:run, service:proxy. For example, patchModuleConfig({ title: "Nightly" }) changes settings under the host-bound module; it takes neither an absolute Nix path nor a module ID. proxyFetch takes a declared service name and a relative request path. onConfigChange, theme CSS variables, and React hooks are also available. Do not pass agent tokens or plaintext secrets through the bridge.
Opening executable UI is a separate operator choice. Deny does not prepare the bundle or contact the linked development server. Open panel once authorizes preparation (which may download or build the bundle). The agent checks that the source and executable revision still match the displayed project snapshot before proceeding. If the agent cannot supply a matching review, refresh the project or update the agent; Studio does not fall back to an unreviewed launch.
Each iframe request to run an action or contact a module service is reviewed in Studio before dispatch. Deny sends nothing to the service; allowing a request does not allow the next one. Stop the panel to remove the frame and cancel outstanding work. Production asset policy blocks direct fetch and WebSocket connections. A linked development server supplies its own headers: open only one you trust. To forbid module-supplied JavaScript entirely, start the agent with STACKPANEL_DISABLE_CUSTOM_UI=1. This leaves native spec panels available; it does not sandbox Nix evaluation or a module's native processes.
Studio renders images in module Markdown as their alt text. Use an explicit link to a diagram or badge rather than requiring a third-party request just to open a module.
Exposing settings for one app
An app-slot panel can edit only the app that Studio used to open it. To make a setting public, include its key in that app's enabled panel.apps entry. For example, suppose the evaluated apps.shop.report.workers option is the number 2:
stackpanel.panels.report-settings = {
module = "report";
title = "Report workers";
type = "PANEL_TYPE_CUSTOM";
slot = "app:tab";
apps.shop = { enabled = true; config.workers = "2"; };
uiBundle = { package = reportUi; entry = "index.html"; permissions = [ "config:read" "config:patch" ]; };
};The public settings are the intersection of keys in panel.apps.shop.config and portable values actually present in apps.shop.report. The panel declaration names the keys; its string metadata does not supply the values or their types. Here getConfig() returns { workers: 2 }, not { workers: "2" }. Do not include secrets. Declaring a panel field alone does not expose an app option.
Call await patchModuleConfig({ workers: 4 }) to update apps.shop.report.workers. There is no app, module, or path argument: Studio sends the relative patch with the original document grant, and the agent checks the current public settings before writing. A patch cannot add undeclared keys, switch apps, or fall back to module-global config. With both grants, onConfigChange receives the refreshed app settings; a patch-only panel receives no values. In React, useAppModuleConfig() from @stackpanel/extension-sdk/react returns the bound appId, settings, and a patchModuleConfig function only when config:patch is granted.
Exposing a service to a panel
For a panel with service:proxy, declare an owned service and an explicit loopback target. Setting the service's primary port alone does not grant access.
stackpanel.services.report = {
enable = true;
module = "report";
command = "exec ${reportServer}/bin/report-server --listen 127.0.0.1:8765";
proxy = { enable = true; port = 8765; };
};Start process-compose from the project root. Its launcher opens .stack/state/proxy/report/report.identity read-only on file descriptor 9 before starting the service. The file must be a regular, single-link file owned by the current user with mode 0600; directories below the project root must not be symlinks or writable by others. The process serving the connection must retain FD 9. If a supervisor closes extra descriptors, configure it to pass FD 9 through or launch the server directly with exec. Listening on the right port without the descriptor is not sufficient. The marker is local process identity, not a token to send to Studio.
Panel code uses proxyFetch("report", "/status") for HTTP. Studio supplies the project and authorization headers plus the panel's X-Stackpanel-UI-Grant; none of these belong in iframe code. The bridge does not expose WebSockets. Direct HTTP clients and native WebSocket clients must send Authorization: Bearer … and X-Stackpanel-Project as headers. A browser WebSocket cannot supply them; do not put credentials in URLs, cookies, or subprotocols. The proxy does not accept query strings, follow redirects, or forward agent credentials to the service.
Develop, link, and release
From the extension checkout, run stack module dev (use --id example for the unrenamed starter). It creates a scratch consumer project, evaluates your checkout as a local override and opens the module in Studio. Editing tracked Nix or JSON panel sources triggers reevaluation; no UI dev server is needed. For a trusted sandbox development server, stack module dev --ui-url http://localhost:5174 uses the same host bridge permissions, but not the production asset server's CSP.
To test against an existing project, install its module input once, then run stack module link /path/to/report --id report from the consumer. Edit the linked module; watch Studio for evaluation errors. Run stack module unlink report to return to the locked version. Use stack module eject report ../report to copy a local .stack/modules/report/ into a standalone flake. Test the standalone input and use the registry publishing guide before announcing a release.