feat: run x86-64 Flatpaks under FEX and add apps from a .flatpakrepo link

Flatpak sandboxes whose runtime is x86-64 now start under the rootfs's FEX,
with FEX's ARM64 libraries and a per-arch ld.so.preload bound in. The add-app
dialog takes a .flatpakrepo link (such as NVIDIA's GeForce NOW repo), Flathub
IDs fall back to the x86-64 build when there is no ARM64 one, and x86-64
apps are listed and launched with --arch=x86_64.
This commit is contained in:
The412Banner committed 2026-10-19 21:18:27 +00:00
1 parent af57286c98
commit f722239bd9
9 files changed
+259 -43

No files matched your search

@@ -75,7 +75,10 @@ object BwrapSpawner {
}
}
class Plan(val root: File, val binds: List<Bind>, val cwd: String, val argv: List<String>, val env: Map<String, String>)
class Plan(
val root: File, val binds: List<Bind>, val cwd: String, val argv: List<String>, val env: Map<String, String>,
val launcher: List<String> = emptyList(),
)
data class Bind(val host: String, val guest: String) {
val spec: String get() = if (host == guest) host else "$host:$guest"
@@ -179,10 +182,67 @@ object BwrapSpawner {
val argv = request.getJSONArray("argv").let { a -> (0 until a.length()).map { a.getString(it) } }
val env = LinkedHashMap<String, String>()
request.optJSONObject("env")?.let { e -> e.keys().forEach { k -> env[k] = e.getString(k) } }
if (binds.any { it.guest == "/dev" }) gpu(rootfs, root, env)?.forEach { addBind(it) }
preload(session, rootfs, env).forEach { addBind(it) }
val x86 = LinuxFex.isX86(LinuxFex.elfMachine(File(hostPath(binds, root.path, "/usr/bin/env"))))
if (x86) {
fex(rootfs, dir, env)?.forEach { addBind(it) }
?: throw IOException("this app is built for x86-64 and the Linux runtime has no FEX to run it")
fakeEvdev(session, rootfs, env).forEach { addBind(it) }
} else {
if (binds.any { it.guest == "/dev" }) gpu(rootfs, root, env)?.forEach { addBind(it) }
preload(session, rootfs, env).forEach { addBind(it) }
}
browserSandboxes(env)
return Plan(root, binds, request.optString("cwd", "/").ifEmpty { "/" }, argv, env)
return Plan(root, binds, request.optString("cwd", "/").ifEmpty { "/" }, argv, env,
if (x86) listOf("$FEX_DIR/bin/FEX") else emptyList())
}
private const val FEX_DIR = "/run/droiddeck-fex"
private val FEX_HOST_LIBS = listOf("ld-linux-aarch64.so.1", "libc.so.6", "libm.so.6", "libstdc++.so.6", "libgcc_s.so.1")
private val FEX_PRELOADS = listOf("libblsession.so", "libfakeinput.so")
// An x86-64 runtime starts under the rootfs's FEX. The sandbox's own tree is the x86 root, so FEX needs no RootFS.
internal fun fex(rootfs: String, dir: File, env: MutableMap<String, String>): List<Bind>? {
val bin = File(rootfs, "usr/local/bin")
val fex = File(bin, "FEX")
val server = File(bin, "FEXServer")
if (!fex.isFile || !server.isFile) return null
val binds = arrayListOf(Bind(fex.path, "$FEX_DIR/bin/FEX"), Bind(server.path, "$FEX_DIR/bin/FEXServer"))
// FEXServer has no RUNPATH, so the ARM64 libraries go where the ARM64 loader looks by default. x86 programs
// search /usr/lib/x86_64-linux-gnu first and skip an ARM64 library they meet.
for (name in FEX_HOST_LIBS + "libxxhash.so.0") {
val lib = listOf(File(rootfs, "usr/lib/$name"), File(rootfs, "usr/local/lib/$name")).firstOrNull { it.exists() }
?: run { Log.w(TAG, "fex: the runtime has no $name"); return null }
binds.add(Bind(lib.canonicalPath, "/usr/lib/$name"))
}
// One ld.so.preload serves both: $LIB is lib for the ARM64 loader and lib/x86_64-linux-gnu (or i386) for the
// runtime's. libblsession turns FEX's execveat of /proc/self/exe into the execve proot supports.
val listed = FileUtils.readString(File(rootfs, "etc/ld.so.preload")) ?: ""
val lines = ArrayList<String>()
for (name in FEX_PRELOADS) {
val builds = listOf(
File(rootfs, "usr/local/lib/$name") to "lib",
File(rootfs, "usr/local/lib/droiddeck-fex/x86_64/$name") to "lib/x86_64-linux-gnu",
File(rootfs, "usr/local/lib/droiddeck-fex/i386/$name") to "lib/i386-linux-gnu",
)
if (name !in listed || builds.any { !it.first.isFile }) continue
builds.forEach { (lib, sub) -> binds.add(Bind(lib.path, "$FEX_DIR/$sub/$name")) }
lines.add("$FEX_DIR/\$LIB/$name")
}
if ("libblsession.so" !in lines.joinToString()) Log.w(TAG, "fex: no libblsession; x86 programs cannot start others")
val preload = File(dir, "ld.so.preload").apply { writeText(lines.joinToString("\n", postfix = "\n")) }
binds.add(Bind(preload.path, "/etc/ld.so.preload"))
val config = File(dir, "fex-config").apply { mkdirs() }
val cache = File(rootfs, "root/.cache/droiddeck-fex-flatpak").apply { mkdirs() }
binds.add(Bind(config.path, "$FEX_DIR/config"))
binds.add(Bind(cache.path, "$FEX_DIR/cache"))
env["FEX_APP_CONFIG_LOCATION"] = "$FEX_DIR/config/"
env["FEX_APP_DATA_LOCATION"] = "$FEX_DIR/config/"
env["FEX_APP_CACHE_LOCATION"] = "$FEX_DIR/cache/"
// Each sandbox gets its own server: the session's x86 Steam has one on the default socket, set up for its RootFS.
env["FEX_SERVERSOCKETPATH"] = "droiddeck-bwrap-${dir.name}-${android.os.SystemClock.elapsedRealtime()}.FEXServer.Socket"
// The session's tunables are for the ARM64 libc; an x86 ld.so copying them under FEX fails in sbrk.
env.remove("GLIBC_TUNABLES")
return binds
}
private const val GPU_DIR = "/run/droiddeck-gpu"
@@ -219,12 +279,12 @@ object BwrapSpawner {
val binds = ArrayList<Bind>()
val paths = libs.map { lib -> "$PRELOAD_DIR/${lib.substringAfterLast('/')}".also { binds.add(Bind(rootfs + lib, it)) } }
env["LD_PRELOAD"] = (paths + listOfNotNull(env["LD_PRELOAD"]?.takeIf { it.isNotBlank() })).joinToString(":")
env["FAKE_EVDEV_DIR"]?.let { File(it).parent }?.let { dev ->
binds.addAll(bindsFor(session, rootfs, dev, dev))
}
return binds
return binds + fakeEvdev(session, rootfs, env)
}
private fun fakeEvdev(session: List<Bind>, rootfs: String, env: Map<String, String>): List<Bind> =
env["FAKE_EVDEV_DIR"]?.let { File(it).parent }?.let { dev -> bindsFor(session, rootfs, dev, dev) } ?: emptyList()
private const val PRELOAD_DIR = "/run/droiddeck-preload"
private fun browserSandboxes(env: MutableMap<String, String>) {
@@ -282,6 +342,7 @@ object BwrapSpawner {
val verbose = plan.env["DROIDDECK_PROOT_VERBOSE"]?.toIntOrNull()
if (verbose != null) { cmd.add(1, "-v"); cmd.add(2, verbose.toString()) }
plan.binds.forEach { cmd.add("-b"); cmd.add(it.spec) }
cmd.addAll(plan.launcher)
cmd.add("/usr/bin/env")
cmd.add("-i")
plan.env.forEach { (k, v) -> cmd.add("$k=$v") }
@@ -19,7 +19,7 @@ object FlatpakManager {
@Volatile var busy: String? = null
private set
class App(val id: String, val name: String, val icon: File?, val branch: String, val summary: String?)
class App(val id: String, val name: String, val icon: File?, val branch: String, val summary: String?, val arch: String = arch())
fun ready(context: Context): Boolean {
val root = LinuxRuntime.rootDir(context)
@@ -33,24 +33,28 @@ object FlatpakManager {
else -> "aarch64"
}
/** The arches apps can be installed for: this device's, then x86-64 under FEX. */
private fun arches(): List<String> = listOf(arch(), "x86_64").distinct()
private fun activeBranch(app: File): Pair<String, File>? = arches().firstNotNullOfOrNull { arch ->
File(app, arch).listFiles()?.firstOrNull { File(it, "active").exists() }?.let { arch to it }
}
fun installedApps(context: Context): List<App> {
val apps = File(LinuxRuntime.rootDir(context), "$USER_DIR/app")
val arch = arch()
return apps.listFiles()?.mapNotNull { dir ->
val id = dir.name
val archDir = File(dir, arch)
val branch = archDir.listFiles()?.firstOrNull { File(it, "active").exists() } ?: return@mapNotNull null
val (arch, branch) = activeBranch(dir) ?: return@mapNotNull null
val active = File(branch, "active")
val export = File(active, "export/share")
val desktop = FileUtils.readString(File(export, "applications/$id.desktop"))
val name = desktop?.let { entry(it, "Name") } ?: id.substringAfterLast('.')
val summary = desktop?.let { entry(it, "Comment") }
App(id, name, icon(export, id), branch.name, summary)
App(id, name, icon(export, id), branch.name, summary, arch)
}?.sortedBy { it.name.lowercase() } ?: emptyList()
}
fun isInstalled(context: Context, id: String): Boolean =
File(LinuxRuntime.rootDir(context), "$USER_DIR/app/$id/${arch()}").listFiles()?.any { File(it, "active").exists() } == true
fun isInstalled(context: Context, id: String): Boolean = activeBranch(File(LinuxRuntime.rootDir(context), "$USER_DIR/app/$id")) != null
private fun entry(desktop: String, key: String): String? {
var inMain = false
@@ -175,6 +179,12 @@ object FlatpakManager {
val problem = transaction(context, "install-bundle", guestPath, onProgress) { id = it.optString("id").ifEmpty { null } }
return id to problem
}
/** Adds the remote a .flatpakrepo file describes and installs the one app it carries (GeForce NOW's is one). */
fun installRepo(context: Context, url: String, onProgress: (String, Int) -> Unit): Pair<String?, String?> {
var id: String? = null
val problem = transaction(context, "install-repo", url, onProgress) { id = it.optString("id").ifEmpty { null } }
return id to problem
}
fun update(context: Context, id: String?, onProgress: (String, Int) -> Unit) = transaction(context, "update", id, onProgress)
fun prune(context: Context, onProgress: (String, Int) -> Unit) = transaction(context, "prune", null, onProgress)
@@ -58,6 +58,7 @@ object UserApps {
class GitHub(val repo: String) : Source()
class Flatpak(val id: String) : Source()
class FlatpakBundle(val file: File) : Source()
class FlatpakRepo(val url: String) : Source()
}
class Request(val source: Source, val name: String?, val icon: String?, val meta: Map<String, String> = emptyMap())
@@ -111,6 +112,7 @@ object UserApps {
is Source.GitHub -> addFromGitHub(context, s.repo, name, icon, onProgress)
is Source.Flatpak -> addFlatpak(context, s.id, name, icon, onProgress)
is Source.FlatpakBundle -> addFlatpakBundle(context, s.file, name, icon, onProgress)
is Source.FlatpakRepo -> addFlatpakRepo(context, s.url, name, icon, onProgress)
}
}
}
@@ -392,6 +394,17 @@ object UserApps {
return t.takeIf { Regex("^[A-Za-z][\\w-]*(\\.[A-Za-z0-9_][\\w-]*){2,}$").matches(it) }
}
fun flatpakRepo(input: String): String? =
input.trim().takeIf { it.startsWith("https://") && it.substringBefore('?').endsWith(".flatpakrepo", ignoreCase = true) && ' ' !in it }
private fun addFlatpakRepo(context: Context, url: String, name: String?, icon: File?, onProgress: (String, Int) -> Unit): String? {
if (!FlatpakManager.ready(context)) FlatpakManager.setup(context, onProgress)?.let { return it }
val (id, problem) = FlatpakManager.installRepo(context, url, onProgress)
problem?.let { return it }
if (id != null) customiseFlatpak(context, id, name, icon)
return null
}
private fun addFlatpak(context: Context, id: String, name: String?, icon: File?, onProgress: (String, Int) -> Unit): String? {
if (!FlatpakManager.ready(context)) FlatpakManager.setup(context, onProgress)?.let { return it }
FlatpakManager.install(context, id, onProgress)?.let { return it }
@@ -143,6 +143,7 @@ internal fun AddAppDialog(runtimeReady: Boolean, onDismiss: () -> Unit, onAdd: (
val repoId = UserApps.githubRepo(repo)
val flatpakId = UserApps.flatpakId(flatpak)
val flatpakRepo = UserApps.flatpakRepo(flatpak)
val custom = name.trim().takeIf { it.isNotEmpty() }
val request: Pair<UserApps.Request, String>? = when (source) {
AddSource.SCRIPT -> scriptPath?.takeIf { it.endsWith(".sh", ignoreCase = true) }?.let { File(it) }
@@ -152,6 +153,7 @@ internal fun AddAppDialog(runtimeReady: Boolean, onDismiss: () -> Unit, onAdd: (
AddSource.GITHUB -> repoId?.let { UserApps.Request(UserApps.Source.GitHub(it), custom, iconPath) to (custom ?: it) }
AddSource.FLATPAK -> bundlePath?.takeIf { it.endsWith(".flatpak", ignoreCase = true) }?.let { File(it) }
?.let { UserApps.Request(UserApps.Source.FlatpakBundle(it), custom, iconPath) to (custom ?: it.nameWithoutExtension) }
?: flatpakRepo?.let { UserApps.Request(UserApps.Source.FlatpakRepo(it), custom, iconPath) to (custom ?: it.substringAfterLast('/').substringBefore('.')) }
?: flatpakId?.let { UserApps.Request(UserApps.Source.Flatpak(it), custom, iconPath) to (custom ?: it) }
}
val busy = UserAppsState.working
@@ -201,7 +203,7 @@ internal fun AddAppDialog(runtimeReady: Boolean, onDismiss: () -> Unit, onAdd: (
CheckedField(
flatpak, { flatpak = it.take(200); bundlePath = null }, stringResource(R.string.add_app_flatpak_id),
stringResource(R.string.add_app_flatpak_placeholder),
stringResource(R.string.add_app_flatpak_invalid).takeIf { flatpak.isNotBlank() && flatpakId == null },
stringResource(R.string.add_app_flatpak_invalid).takeIf { flatpak.isNotBlank() && flatpakId == null && flatpakRepo == null },
)
Small(stringResource(R.string.add_app_flatpak_bundle_hint))
FileRow(bundlePath) {
+2 -2
View File
@@ -181,7 +181,7 @@
<string name="app_fex_not_ready">This program is built for %1$s PCs and runs through FEX, which is not set up yet. Open Steam once while signed in: DroidDeck has it download FEX and the Steam Linux Runtime it needs, then this opens.</string>
<string name="session_end_program">This program</string>
<string name="session_end_fex_missing">%1$s is built for x86 PCs and runs through FEX, which is not installed yet. Start Steam once while signed in and let it download FEX and the Steam Linux Runtime, then start this again.</string>
<string name="add_app_flatpak_hint">A Flathub app ID or its Flathub page. Flatpak is set up first if it is not yet.</string>
<string name="add_app_flatpak_hint">A Flathub app ID, its Flathub page, or a .flatpakrepo link such as GeForce NOW\'s. Flatpak is set up first if it is not yet. Apps with only an x86-64 build run under FEX.</string>
<string name="add_app_file">File</string>
<string name="add_app_no_file">No file chosen</string>
<string name="add_app_choose_file">Choose file</string>
@@ -194,7 +194,7 @@
<string name="add_app_repository_invalid">Not a GitHub repository</string>
<string name="add_app_flatpak_id">App ID</string>
<string name="add_app_flatpak_placeholder">org.ppsspp.PPSSPP</string>
<string name="add_app_flatpak_invalid">Not a Flathub app ID</string>
<string name="add_app_flatpak_invalid">Not a Flathub app ID or .flatpakrepo link</string>
<string name="add_app_flatpak_bundle_hint">Or a .flatpak file, for an app not on Flathub.</string>
<string name="add_app_pick_flatpak">Choose a .flatpak file</string>
<string name="add_app_source">Source</string>
@@ -4,12 +4,15 @@ import android.util.Base64
import org.robolectric.RuntimeEnvironment
import com.droiddeck.launcher.runtime.BwrapSpawner.Bind
import java.io.File
import java.io.IOException
import java.nio.file.Files
import org.json.JSONArray
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
@@ -118,4 +121,66 @@ class BwrapSpawnerPlanTest {
assertEquals("/app", plan.cwd)
assertEquals("org.example.Game", plan.env["FLATPAK_ID"])
}
private fun elf(file: File, machine: Int) = file.apply {
parentFile!!.mkdirs()
writeBytes(ByteArray(64).also { b ->
"\u007fELF".toByteArray().copyInto(b); b[4] = 2; b[5] = 1; b[18] = machine.toByte()
})
}
private fun x86Request() = request(
op("t" to "bind", "src" to "/runtime-files", "dest" to "/usr", "ro" to true),
op("t" to "symlink", "target" to "usr/lib", "dest" to "/lib"),
)
private fun fexInRootfs(rootfs: File) {
for (name in listOf("usr/local/bin/FEX", "usr/local/bin/FEXServer", "usr/lib/ld-linux-aarch64.so.1", "usr/lib/libc.so.6",
"usr/lib/libm.so.6", "usr/lib/libstdc++.so.6", "usr/lib/libgcc_s.so.1", "usr/local/lib/libxxhash.so.0")) {
elf(File(rootfs, name), LinuxFex.ELF_AARCH64)
}
for (dir in listOf("usr/local/lib", "usr/local/lib/droiddeck-fex/x86_64", "usr/local/lib/droiddeck-fex/i386")) {
for (name in listOf("libblsession.so", "libfakeinput.so")) File(rootfs, "$dir/$name").apply { parentFile!!.mkdirs(); writeText("") }
}
File(rootfs, "etc/ld.so.preload").apply { parentFile!!.mkdirs() }
.writeText("/usr/local/lib/libblsession.so\n/usr/local/lib/libfakeinput.so\n/usr/local/lib/libblfastpath.so\n")
}
@Test fun anX86RuntimeStartsUnderFexWithPreloadsForBothArches() {
val context = RuntimeEnvironment.getApplication()
val rootfs = LinuxRuntime.rootDir(context)
fexInRootfs(rootfs)
elf(File(runtime, "bin/env"), LinuxFex.ELF_X86_64)
try {
val plan = BwrapSpawner.plan(context, x86Request(), dir)
assertEquals(listOf("/run/droiddeck-fex/bin/FEX"), plan.launcher)
assertTrue(Bind(File(rootfs, "usr/local/bin/FEXServer").path, "/run/droiddeck-fex/bin/FEXServer") in plan.binds)
assertTrue(plan.binds.any { it.guest == "/usr/lib/ld-linux-aarch64.so.1" })
assertTrue(plan.binds.any { it.guest == "/usr/lib/libxxhash.so.0" })
assertTrue(plan.binds.any { it.guest == "/run/droiddeck-fex/lib/x86_64-linux-gnu/libblsession.so" })
val preload = File(plan.binds.single { it.guest == "/etc/ld.so.preload" }.host).readText()
assertEquals("/run/droiddeck-fex/\$LIB/libblsession.so\n/run/droiddeck-fex/\$LIB/libfakeinput.so\n", preload)
assertNull(plan.env["LD_PRELOAD"])
assertNull(plan.env["VK_ICD_FILENAMES"])
assertTrue(plan.env.getValue("FEX_SERVERSOCKETPATH").endsWith(".FEXServer.Socket"))
assertEquals("/run/droiddeck-fex/config/", plan.env["FEX_APP_CONFIG_LOCATION"])
} finally {
rootfs.deleteRecursively()
}
}
@Test fun anArmRuntimeIsStartedDirectly() {
val context = RuntimeEnvironment.getApplication()
elf(File(runtime, "bin/env"), LinuxFex.ELF_AARCH64)
val plan = BwrapSpawner.plan(context, x86Request(), dir)
assertEquals(emptyList<String>(), plan.launcher)
assertFalse(plan.binds.any { it.guest == "/etc/ld.so.preload" })
}
@Test fun anX86RuntimeWithoutFexIsRefused() {
val context = RuntimeEnvironment.getApplication()
elf(File(runtime, "bin/env"), LinuxFex.ELF_X86_64)
File(LinuxRuntime.rootDir(context), "usr/local/bin/FEX").delete()
assertThrows(IOException::class.java) { BwrapSpawner.plan(context, x86Request(), dir) }
}
}