Nimbus answers SELECT InstanceName FROM Organization with 'Nimbus', so a one-line helper tells you which runtime you're in:
static Boolean isNimbus() {
return [SELECT InstanceName FROM Organization LIMIT 1].InstanceName == 'Nimbus';
}In a real org that same query returns the pod name (NA139, EU45, …), so the check is false there. Want a different sentinel? Set nimbus.org.instance-name in nimbus.properties.
Sometimes Apex needs to know where it's running
Most code shouldn't care whether it runs locally or in the cloud — that's the whole point of running your suite under a local Apex runtime. But a few paths legitimately do. You might want to short-circuit an outbound HTTP callout that has no business firing on a laptop, skip enqueuing a Schedulable job during a local run, point at a different endpoint, or branch a bit of environment-snapshot logic. The usual move is a sandbox check:
Boolean isSandbox = [SELECT IsSandbox FROM Organization LIMIT 1].IsSandbox;That tells production from sandbox — but it can't tell a sandbox from a local Nimbus run, and the local run is exactly the case you often want to guard. You need one more signal.
The one-liner
That signal already exists on the Organization object: InstanceName. In a real org it names the instance your org is hosted on. In Nimbus it's 'Nimbus'. So the entire environment check is standard SOQL:
public class Env {
public static Boolean isNimbus() {
return [SELECT InstanceName FROM Organization LIMIT 1].InstanceName == 'Nimbus';
}
}
// Guard a callout that shouldn't run locally
if (!Env.isNimbus()) {
PaymentGateway.charge(order);
}No new API surface, no @TestVisible plumbing, no build-time constant. Just a field that's already there, populated with a value you can recognize.
Why InstanceName, and not a proprietary hook
The design goal is that your code stays portable. A helper that calls some Nimbus.isLocal() method would compile only under Nimbus and break the moment you deploy — the opposite of what a faithful local runtime should do. Reading a real, standard Salesforce field means the exact same class compiles and runs everywhere:
- Under Nimbus,
InstanceNameis'Nimbus'→ the check istrue. - In an org, it's the actual instance (e.g.
NA139) → the check isfalse.
Nothing about the code is Nimbus-specific. You could hand it to a teammate who's never heard of Nimbus and it would behave correctly in their sandbox.
What Nimbus returns for the Organization singleton
Querying Organization locally returns a fully-populated mock record, not a handful of nulls — code that reads org.CreatedDate.format() or org.OrganizationType for an audit log or an environment page keeps working. The fields you're most likely to touch:
InstanceName—'Nimbus'by default (configurable, below).IsSandbox—false, unless you set the sandbox flag in config.OrganizationType—'Developer Edition'.LanguageLocaleKey/TimeZoneSidKey— follow your configured locale and time zone.CreatedDate,Name,Id,PrimaryContact— plausible non-null defaults.
Set the instance name yourself
'Nimbus' is a sensible default, but you own it. In your project's nimbus.properties, set:
# Value returned by SELECT InstanceName FROM Organization
nimbus.org.instance-name=NimbusTwo reasons you'd change it:
- Mimic a real instance. If some code asserts on a specific pod name, set
nimbus.org.instance-name=NA139so the local run matches production instead of tripping the assertion. - Use your own sentinel. Prefer
LOCALorCIas the marker yourisNimbus()equivalent looks for? Set it and check against that string.
Because it's an ordinary config key, it takes a profile prefix too — so CI can report a different instance name than your local machine without touching a line of Apex:
# Local default
nimbus.org.instance-name=Nimbus
# In CI (NIMBUS_PROFILE=ci), report a CI-specific instance
%ci.nimbus.org.instance-name=NimbusCIUse it sparingly
One caution worth stating plainly: environment branching is a tool for the edges — callouts, scheduled jobs, external side effects — not for business logic. If your core behaviour changes depending on isNimbus(), then the thing you're testing locally isn't the thing that runs in production, and the local suite stops meaning anything. Keep the branch at the boundary, keep the logic identical, and the local run stays a faithful stand-in for the org.
Try it locally
Drop the isNimbus() helper into a project, run your suite under Nimbus, and watch the guarded callouts stay quiet — no org, no sandbox, no deploy. Set nimbus.org.instance-name when you want the local runtime to answer to a different name.