okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-split-int' into f-split-fitfa6367a98

main
4.0 KiB71 linesraw
1/**
2 * The environment for programs that are NOT part of Strata: the folder chooser (zenity, kdialog, gdbus), xdg-open and the browser it
3 * starts, and the shell and tools the coding assistant runs for the person.
4 *
5 * Why: the Linux AppImage starts Strata with its own libraries and settings in the environment (LD_LIBRARY_PATH, GTK_*, GIO_*,
6 * GSETTINGS_SCHEMA_DIR, XDG_DATA_DIRS, PYTHONHOME, ...). A system program that inherits them loads Strata's copies of libssl, GTK and
7 * GLib instead of its own and dies (tester report 2026-10-17: zenity "libssl.so.3: version OPENSSL_3.2.0 not found", so the folder
8 * picker did nothing). Programs Strata ships itself (its Python, Node and engine) keep the environment they were built for; everything
9 * else gets the environment the AppImage was started from.
10 *
11 * The AppImage's launcher records that original environment in STRATA_ORIGINAL_ENV (repack-appimage.sh adds the hook). An older
12 * AppImage without the record is cleaned instead: every variable that points into the AppImage's own folder is dropped, and its
13 * folders are removed from the path lists.
14 */
15const RECORD = 'STRATA_ORIGINAL_ENV';
16
17/** Variables the AppImage tooling sets only for itself; never meant for another program. */
18const APPIMAGE_ONLY = [
19  'APPDIR', 'APPIMAGE', 'ARGV0', 'OWD', 'APPIMAGE_SILENT_INSTALL', 'APPIMAGE_EXTRACT_AND_RUN',
20  'GTK_DATA_PREFIX', 'GTK_EXE_PREFIX', 'GTK_PATH', 'GTK_IM_MODULE_FILE', 'GTK_THEME', 'GDK_PIXBUF_MODULE_FILE', 'GDK_PIXBUF_MODULEDIR',
21  'GSETTINGS_SCHEMA_DIR', 'GI_TYPELIB_PATH', 'GIO_MODULE_DIR', 'GIO_EXTRA_MODULES',
22  'GST_REGISTRY_REUSE_PLUGIN_SCANNER', 'GST_PLUGIN_SYSTEM_PATH_1_0', 'GST_PLUGIN_PATH_1_0', 'GST_PLUGIN_SCANNER_1_0', 'GST_PTP_HELPER_1_0',
23  'WEBKIT_EXEC_PATH', 'WEBKIT_INJECTED_BUNDLE_PATH', 'STRATA_ORIGINAL_ENV', 'STRATA_APPDIR',
24];
25/** Lists of folders: the AppImage prepends its own; only its entries are removed. */
26const PATH_LISTS = ['PATH', 'XDG_DATA_DIRS', 'XDG_CONFIG_DIRS', 'LD_LIBRARY_PATH', 'LD_PRELOAD', 'LIBRARY_PATH', 'PYTHONPATH', 'PYTHONHOME', 'PERLLIB', 'PERL5LIB', 'QT_PLUGIN_PATH', 'QML2_IMPORT_PATH', 'GST_PLUGIN_PATH', 'GTK_PATH', 'GIO_MODULE_DIR'];
27
28/** The environment the program was started from, decoded from the launcher's record, or null when there is none. */
29export function recordedOriginal(env = process.env) {
30  const raw = env[RECORD];
31  if (!raw) return null;
32  try {
33    const text = Buffer.from(raw, 'base64').toString('utf8');
34    const out = {};
35    for (const entry of text.split('\0')) {
36      const at = entry.indexOf('=');
37      if (at > 0) out[entry.slice(0, at)] = entry.slice(at + 1);
38    }
39    return Object.keys(out).length ? out : null;
40  } catch { return null; }
41}
42
43/** `env` with everything that belongs to a running AppImage removed (no record needed). */
44export function withoutAppImage(env = process.env) {
45  const dir = env.APPDIR || '';
46  const out = { ...env };
47  const inside = value => !!dir && (value === dir || value.startsWith(`${dir}/`) || value.startsWith(`${dir}//`));
48  for (const name of APPIMAGE_ONLY) delete out[name];
49  if (dir) {
50    for (const name of Object.keys(out)) if (/^(GTK|GDK|GIO|GST|GI_|GSETTINGS|WEBKIT)/.test(name) && typeof out[name] === 'string' && out[name].includes(dir)) delete out[name];
51    for (const name of PATH_LISTS) {
52      if (typeof out[name] !== 'string') continue;
53      const kept = out[name].split(':').filter(part => part && !inside(part));
54      if (kept.length) out[name] = kept.join(':'); else delete out[name];
55    }
56  }
57  return out;
58}
59
60/** True when this process was started from an AppImage. */
61export const insideAppImage = (env = process.env) => !!(env.APPIMAGE || env.APPDIR);
62
63/**
64 * The environment for an external program (see the top of this file). Outside an AppImage it is `env` unchanged.
65 * `extra` is added last (a variable the caller really means to pass).
66 */
67export function externalEnvironment(env = process.env, extra = {}) {
68  if (!insideAppImage(env) && !env[RECORD]) return { ...env, ...extra };
69  const original = recordedOriginal(env);
70  return { ...(original ?? withoutAppImage(env)), ...extra };
71}