Skip to content

t3code: use nixpkgs Chromium for browser previews - #582335

Open
otavio wants to merge 3 commits into
NixOS:masterfrom
otavio:t3code-package-preview-browser
Open

otavio wants to merge 3 commits into
NixOS:masterfrom
otavio:t3code-package-preview-browser

Conversation

@otavio

@otavio otavio commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Closes #582297.

T3 Code downloads a headless browser whose FHS interpreter and shared-library lookup do not work on NixOS without nix-ld or manual patching. Use nixpkgs’ existing source-built chromium package for Linux previews and HTML rendering instead. The browser can receive nixpkgs security updates independently of T3 Code releases, without introducing another pinned browser build or maintenance role.

Both the renderer and t3 browser setup use the packaged executable. Setup no longer diagnoses stale cached downloads. t3code.browser exposes the Chromium dependency.

The generated AppArmor profile matches Chromium’s exact canonical executable, rather than its launcher script. Its name and filename include that executable’s store hash, so an upstream cache-only profile or a profile for another store result cannot cause setup to skip registration. Older profiles remain available to other installations. No broad store-path wildcard is used.

Integration applies on x86_64-linux and aarch64-linux. Darwin and unsupported Linux systems retain the upstream browser lookup without evaluating Chromium’s unsupported dependencies.

Validation with T3 Code v0.1.6 and Chromium 156.0.8078.79:

  • nix-build -A t3code -A t3code.tests.browser --no-out-link passed with sandboxing enabled on x86_64-linux. The VM test checks setup with an empty cache, setup with a stale downloaded browser that must not execute, and page rendering plus a screenshot as a normal user with Chrome’s sandbox enabled. The VM has no nix-ld configuration.
  • A source-level compatibility probe exercised T3’s actual screenshot and layout-measurement functions, plus Playwright incognito and persistent contexts and screenshots. These passed with the sandbox enabled. The browser service made zero download requests and created no browser cache.
  • A separate AppArmor-enabled NixOS VM loaded the profile exported by the patched PreviewBrowserHost.ts, launched Chromium, and confirmed the generated profile through /proc/<pid>/attr/current. A restricted-host filesystem fixture also checks legacy/current profile detection and browser-store updates. A full Ubuntu restricted-userns test is not claimed.
  • The runtime closure retains the existing Chromium outputs and no private t3code-browser derivation.
  • ARM Linux substitutions evaluate. Darwin and i686 evaluation retain the original lookup. No ARM or Darwin build, or desktop UI test, is claimed.
  • nixfmt --check and git diff --check pass.

Code, tests, commit messages, description, and the local compatibility/AppArmor probes were assisted by Codex (gpt-6.1-sol).

Things done

The runtime download has an FHS interpreter and cannot launch on NixOS
without nix-ld or manual patching. Package the Chrome for Testing shell
pinned by T3 Code and fix its ELF interpreter and library paths at build
time. Redirect the installed-browser lookup to that store executable.

Expose the browser through passthru and check the upstream version pin
when patching T3 Code, so release updates cannot silently use a different
browser. Add a NixOS VM test that renders a page and saves a screenshot
as a normal user with Chrome's sandbox enabled.

The browser derivation is based on OSSystems/t3code-nix, with the store
layout and integration adapted for nixpkgs.

Assisted-by: Codex (gpt-6.1-sol)
@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1 This PR causes 1 package to rebuild on Linux. llm-assisted Contributions that include code generation with LLMs labels Nov 2, 2026
@otavio
otavio marked this pull request as ready for review November 2, 2026 18:17
@nixpkgs-ci
nixpkgs-ci Bot requested review from 0x4bs3nt, iamanaws and qweered November 2, 2026 18:30

@0x4bs3nt 0x4bs3nt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the packaged-browser approach addresses the missing libraries and ELF interpreter behind #582297.

however, please update the apparmor integration for the new exec location. PreviewBrowserHost.ts generates a profile matching only /**/tools/chrome-headless-shell/*/*/chrome-headless-shell, whereas this change launches the browser from /nix/store/…/libexec/t3code-browser/chrome-headless-shell. on Ubuntu hosts enforcing user namespace restrictions, the profile will no longer cover the browser.

make the profile cover the packaged executable and handle updating an already installed profile.

the separate installedBrowser lookup in cli/browser.ts should also use the packaged executable, otherwise setup continues diagnosing stale cached downloads.

please add Closes #582297 to the description as well.

Match the canonical packaged executable in the AppArmor profile. Key
its profile name and filename by the browser store path so an existing
cache-only profile, or one for an older store result, cannot make setup
skip registration. Keep those older profiles available to other T3
installations.

Use the same packaged executable for setup's library diagnostics rather
than scanning cached runtime downloads.

Validated profile migration with a restricted-host filesystem fixture,
profile parsing and kernel attachment in an AppArmor-enabled NixOS VM,
and setup with a stale cached browser. The full t3code build passes.

Assisted-by: Codex (gpt-6.1-sol)
@otavio

otavio commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 48f8576.

  • The AppArmor attachment now matches the packaged executable’s exact canonical store path.
  • The profile name and filename include the browser’s store hash. An existing cache-only profile, or one for an older store result, cannot satisfy setup’s existence check. Running setup installs and loads the new profile while keeping older profiles available to other installations.
  • Setup’s library diagnostics now use the same packaged executable as the renderer and ignore cached downloads.
  • Added Closes #582297 to the description.

Validation passed: full t3code rebuild; generated-profile parsing; a restricted-host filesystem fixture covering old/current profiles; and an AppArmor-enabled NixOS VM confirming that the running browser attaches to the generated profile with its sandbox enabled. A before/after CLI check also reproduced the stale-cache diagnostic and verified the fix. A full Ubuntu restricted-userns test is not claimed.

Assisted by Codex (gpt-6.1-sol).

@0x4bs3nt
0x4bs3nt self-requested a review November 2, 2026 20:19

@0x4bs3nt 0x4bs3nt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes look good to me, i'd have loved if this was tested on ubuntu, but approving.

@nixpkgs-ci nixpkgs-ci Bot added 12.approvals: 1 This PR was reviewed and approved by one person. 12.approved-by: package-maintainer This PR was reviewed and approved by a maintainer listed in any of the changed packages. labels Nov 2, 2026

@RossSmyth RossSmyth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking upstream this seems to act as a web browser (meaning you can use it to browse the web), meaning that it falls under the Nixpkgs browser security rules:

  1. Upstream must keep up-to-date with their upstream (Chromium it seems in this case) and able to release non-trivial updates from their upstream within a few hours.
  2. An active nixpkgs committer is listed in this package to merge patches within a few hours, generally once or twice a week.

These rules exist because browser are of particular importance in our security profile; they process untrusted inputs by design.
https://nvd.nist.gov/vuln/search?cpeFilterMode=cpe&cpeName=cpe:2.3:a:google:chrome:156.0.8078.70:*:*:*:*:*:*:*&resultType=records

Use the existing Chromium package so browser security updates do not
depend on T3 Code releases or a separately maintained binary pin.
Remove the private Chrome for Testing derivation and its version check.

Keep setup and rendering on the same browser, with AppArmor matching
Chromium's canonical executable. Add a VM regression test for setup with
empty and stale caches, and for rendering with the sandbox enabled.

T3 Code's screenshot, layout measurement, and Playwright incognito and
persistent contexts work with Chromium 156.0.8078.79. The full package
build and both the regression and AppArmor VM checks pass.

Assisted-by: Codex (gpt-6.1-sol)
@otavio otavio changed the title t3code: package the pinned Linux preview browser t3code: use nixpkgs Chromium for browser previews Nov 2, 2026
@otavio

otavio commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Agreed. I revised the PR in 82dad32 to use the existing nixpkgs chromium package directly.

The separate pinned Chrome for Testing derivation and its version check are removed. Browser engine updates now follow the existing Chromium package, independently of T3 releases, reusing its source build and maintenance. Both setup diagnostics and rendering use that dependency, and the AppArmor profile matches Chromium’s actual executable.

Compatibility checks passed with T3 Code v0.1.6 and Chromium 156.0.8078.79: T3’s screenshot/layout-measurement functions and Playwright incognito/persistent contexts, with the sandbox enabled. The full package build, a new NixOS VM regression test for empty/stale caches and sandboxed rendering, and the AppArmor attachment VM also pass. The description now covers this implementation and validation.

Assisted by Codex (gpt-6.1-sol).

@nixpkgs-ci nixpkgs-ci Bot added 12.approvals: 2 This PR was reviewed and approved by two persons. and removed 12.approvals: 1 This PR was reviewed and approved by one person. 12.approvals: 2 This PR was reviewed and approved by two persons. labels Nov 4, 2026
@nixpkgs-ci nixpkgs-ci Bot added the 12.approvals: 3+ This PR was reviewed and approved by three or more persons. label Nov 4, 2026
@merrkry

merrkry commented Nov 26, 2026

Copy link
Copy Markdown
Member

Maybe we can also use playwright-driver.components.chromium-headless-shell?

@repparw

repparw commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Think I prefer this approach vs the other pr, but I'd agree with @merrkry

that gets us shipping the same as upstream, basically. (though release cadence could be a negative vs shipping whole chromium, see #585789 )

I'd prefer not to use chromium when considering #555814 , as shipping headless would be feasible in the -cli version, but shipping chromium would lose ~all of the upside (not shipping electron)

Comment on lines 1 to 5
{
callPackage,
cctools,
chromium,
copyDesktopItems,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move browser wiring to package.nix (via T3CODE_BROWSER_PATH, matching T3CODE_CLOUDFLARED_PATH / T3CODE_RESOURCE_MONITOR_PATH) so overriding or disabling the browser doesn't rebuild unwrapped.

Suggested change
{
callPackage,
cctools,
chromium,
copyDesktopItems,
{
cctools,
copyDesktopItems,

Comment on lines 37 to 44
pnpm = pnpm_12;
browser = chromium;
browserProfileName = "t3-nix-${builtins.baseNameOf (toString browser.browser)}";
withBrowser = lib.elem stdenv.hostPlatform.system [
"x86_64-linux"
"aarch64-linux"
];
desktopIcon =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pnpm = pnpm_12;
browser = chromium;
browserProfileName = "t3-nix-${builtins.baseNameOf (toString browser.browser)}";
withBrowser = lib.elem stdenv.hostPlatform.system [
"x86_64-linux"
"aarch64-linux"
];
desktopIcon =
pnpm = pnpm_12;
desktopIcon =

Comment on lines +72 to 88
''
+ lib.optionalString withBrowser ''
substituteInPlace apps/server/src/preview/PreviewBrowser.ts \
--replace-fail 'const executable = path.join(installRoot, release.version, executableName);' \
'const executable = "${lib.getExe browser}";'

# Key the profile by store path so setup also runs after browser updates
# or when an upstream cache-only profile is already installed.
substituteInPlace apps/server/src/preview/PreviewBrowserHost.ts \
--replace-fail 't3-chrome-headless-shell' '${browserProfileName}' \
--replace-fail '/**/tools/chrome-headless-shell/*/*/chrome-headless-shell' \
'${browser.browser}/libexec/chromium/chromium'

substituteInPlace apps/server/src/cli/browser.ts \
--replace-fail 'const browser = yield* installedBrowser(baseDirPath);' \
'const browser = Option.some("${lib.getExe browser}");'
'';

@repparw repparw Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read T3CODE_BROWSER_PATH (and optional T3CODE_BROWSER_APPARMOR_PATH for Chromium's canonical ELF) at runtime, falling back to upstream behavior when unset.

Suggested change
''
+ lib.optionalString withBrowser ''
substituteInPlace apps/server/src/preview/PreviewBrowser.ts \
--replace-fail 'const executable = path.join(installRoot, release.version, executableName);' \
'const executable = "${lib.getExe browser}";'
# Key the profile by store path so setup also runs after browser updates
# or when an upstream cache-only profile is already installed.
substituteInPlace apps/server/src/preview/PreviewBrowserHost.ts \
--replace-fail 't3-chrome-headless-shell' '${browserProfileName}' \
--replace-fail '/**/tools/chrome-headless-shell/*/*/chrome-headless-shell' \
'${browser.browser}/libexec/chromium/chromium'
substituteInPlace apps/server/src/cli/browser.ts \
--replace-fail 'const browser = yield* installedBrowser(baseDirPath);' \
'const browser = Option.some("${lib.getExe browser}");'
'';
''
+ lib.optionalString stdenv.hostPlatform.isLinux ''
substituteInPlace apps/server/src/preview/PreviewBrowser.ts \
--replace-fail 'const executable = path.join(installRoot, release.version, executableName);' \
'const executable = process.env.T3CODE_BROWSER_PATH || path.join(installRoot, release.version, executableName);'
# Key the AppArmor profile by the canonical browser store path at runtime so
# setup tracks wrapper overrides and updates without rebuilding unwrapped.
substituteInPlace apps/server/src/preview/PreviewBrowserHost.ts \
--replace-fail 'export const APPARMOR_PROFILE_PATH = "/etc/apparmor.d/t3-chrome-headless-shell";' \
'const nixBrowserAppArmorPath = process.env.T3CODE_BROWSER_APPARMOR_PATH || process.env.T3CODE_BROWSER_PATH;
const nixBrowserProfileName = nixBrowserAppArmorPath ? `t3-nix-''${nixBrowserAppArmorPath.split("/")[3] ?? "browser"}` : "t3-chrome-headless-shell";
export const APPARMOR_PROFILE_PATH = `/etc/apparmor.d/''${nixBrowserProfileName}`;' \
--replace-fail 'profile t3-chrome-headless-shell /**/tools/chrome-headless-shell/*/*/chrome-headless-shell flags=(unconfined) {' \
'profile ''${nixBrowserProfileName} ''${nixBrowserAppArmorPath || "/**/tools/chrome-headless-shell/*/*/chrome-headless-shell"} flags=(unconfined) {' \
--replace-fail 'include if exists <local/t3-chrome-headless-shell>' \
'include if exists <local/''${nixBrowserProfileName}>'
substituteInPlace apps/server/src/cli/browser.ts \
--replace-fail 'const browser = yield* installedBrowser(baseDirPath);' \
'const browser = env.T3CODE_BROWSER_PATH ? Option.some(env.T3CODE_BROWSER_PATH) : yield* installedBrowser(baseDirPath);'
'';

Comment on lines +256 to 263
}
// lib.optionalAttrs withBrowser {
inherit browser;
tests.browser = callPackage ./browser-test.nix {
inherit browser;
t3code = finalAttrs.finalPackage;
};
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move passthru.browser and passthru.tests.browser to package.nix so the VM test exercises the wrapped t3code package.

Suggested change
}
// lib.optionalAttrs withBrowser {
inherit browser;
tests.browser = callPackage ./browser-test.nix {
inherit browser;
t3code = finalAttrs.finalPackage;
};
};
};

@repparw

repparw commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Context for the inline suggestions on unwrapped.nix above:

  • No unwrapped rebuilds on browser overrides: Reading T3CODE_BROWSER_PATH at runtime (matching how package.nix already wires T3CODE_CLOUDFLARED_PATH and T3CODE_RESOURCE_MONITOR_PATH) lets users swap browser (e.g. chromium vs playwright-driver.components.chromium-headless-shell) or disable it (enableBrowser = false on minimal headless hosts) by only rebuilding the wrapper, not the full Node/pnpm build.
  • Cleaner split with t3code: split desktop app into t3code-desktop and t3code-cli #555814: Keeping unwrapped.nix browser-agnostic lets t3code-cli and t3code-desktop share the same cached unwrapped derivation even if they end up with different browser defaults.
  • Test coverage: Moving passthru.browser and passthru.tests.browser to package.nix ensures the NixOS VM test exercises the wrapped t3code binary with the env vars applied.

Companion diff for pkgs/by-name/t3/t3code/package.nix (not in this PR's diff yet so it couldn't be attached as an inline suggestion):

--- a/pkgs/by-name/t3/t3code/package.nix
+++ b/pkgs/by-name/t3/t3code/package.nix
@@ -1,8 +1,10 @@
 {
   lib,
+  stdenv,
   callPackage,
   symlinkJoin,
   makeBinaryWrapper,
+  chromium,
   enableAzureDevOps ? false,
   azure-cli,
   azure-cli-extensions,
@@ -26,11 +28,23 @@
   enableOpencode ? false,
   opencode,
   enableResourceMonitor ? true,
+  enableBrowser ? lib.elem stdenv.hostPlatform.system [
+    "x86_64-linux"
+    "aarch64-linux"
+  ],
+  browser ? chromium,
   t3code-unwrapped ? callPackage ./unwrapped.nix { },
   t3code-resource-monitor ? callPackage ./resource-monitor.nix { inherit t3code-unwrapped; },
 }:
 
 let
+  browserExe = lib.getExe browser;
+  # Chromium's `lib.getExe` is a wrapper script; AppArmor needs the canonical ELF.
+  browserAppArmorPath =
+    if browser ? browser then
+      "${browser.browser}/libexec/chromium/chromium"
+    else
+      browserExe;
+
   runtimePackages =
     lib.optionals enableAzureDevOps [
       (azure-cli.withExtensions [ azure-cli-extensions.azure-devops ])
@@ -62,10 +76,18 @@ let
       "--set-default"
       "T3CODE_RESOURCE_MONITOR_PATH"
       (lib.getExe t3code-resource-monitor)
+    ]
+    ++ lib.optionals (enableBrowser && browser != null) [
+      "--set-default"
+      "T3CODE_BROWSER_PATH"
+      browserExe
+      "--set-default"
+      "T3CODE_BROWSER_APPARMOR_PATH"
+      browserAppArmorPath
     ];
 
 in
-symlinkJoin {
+symlinkJoin (finalAttrs: {
   pname = "t3code";
   inherit (t3code-unwrapped) version;
   __structuredAttrs = true;
@@ -85,7 +107,14 @@ symlinkJoin {
     unwrapped = t3code-unwrapped;
     resourceMonitor = t3code-resource-monitor;
   }
-  // t3code-unwrapped.passthru;
+  // t3code-unwrapped.passthru
+  // lib.optionalAttrs (enableBrowser && browser != null) {
+    inherit browser;
+    tests.browser = callPackage ./browser-test.nix {
+      inherit browser;
+      t3code = finalAttrs.finalPackage;
+    };
+  };
 
   meta = {
     # Manually inherit so that pos works
@@ -100,4 +129,4 @@ symlinkJoin {
       platforms
       ;
   };
-}
+})

Note: T3CODE_BROWSER_APPARMOR_PATH is only needed because nixpkgs chromium's lib.getExe is a shell wrapper while AppArmor attaches to ${chromium.browser}/libexec/chromium/chromium; direct ELF binaries fall back to browserExe automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-linux: 1 This PR causes 1 package to rebuild on Linux. 12.approvals: 3+ This PR was reviewed and approved by three or more persons. 12.approved-by: package-maintainer This PR was reviewed and approved by a maintainer listed in any of the changed packages. llm-assisted Contributions that include code generation with LLMs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

t3code: built-in browser reports missing Chrome libraries

7 participants