feat: install and bundle a Flatpak through the host's flatpak
An executable packaged as a Flatpak came out as the files/ of one, which
only flatpak build-finish makes an app of, and getting it installed or
onto another machine took flatpak commands on the host, typed by hand.
Two verbs do it where the host's flatpak can be reached:
`crafter-build install` builds the project and installs its app for this
user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a
single file for `flatpak install --user` that names Flathub's repository
for the runtime. Inside crafter-build's Flatpak they need the
org.freedesktop.Flatpak permission, which its manifest does not grant in
this commit. Both put the app into the project's repo first, in
build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than
per variant, since flatpak-builder's state there is its cache. Every
variant exports into it, so the export and the install or bundle made
from it hold a lock there, and another crafter-build exporting meanwhile
waits rather than leave this one installing or bundling its program. The
user remote an install re-points is every checkout's that builds the
app-id, so re-pointing it and installing from it hold a second lock,
which every crafter-build installing there takes, natively or in the
Flatpak, whatever its XDG_CACHE_HOME: in
~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's
own installation, which flatpak hides from the sandbox behind a tmpfs of
the sandbox's own, and in the directory of a private one,
FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that
directory with the host. Without modules, the host's flatpak
build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh
each time from a metadata naming the app, its runtime and the Sdk and a
tree of hard links to the build's files/, copies where a link cannot be
made, across filesystems or on one without links, vfat or exFAT: it
writes the command, the sandbox's permissions and export/ by flatpak's
own rules, those of whichever flatpak the host runs, and refuses a
permission it does not know, and flatpak build-export puts the result
into the repo. When cfg.flatpak.modules names flatpak-builder module
files for what the app needs in /app besides the program,
flatpak-builder does both, from a manifest that lists them and copies
the built program in last, so nothing is compiled twice. That manifest
keeps share/locale in the app ("separate-locales": false):
flatpak-builder would split it into a .Locale extension by default,
which a bundle, holding the app alone, leaves out. The loader rebases a
dependency's module files as it does its cfg.files. install uses a user
remote named after the app-id, re-pointed at the repo each time, and
says how to remove both. The app's tree in
build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a
module's files too, is left out of the include walk with every other
build/, so the headers a module installed reach no compile.
cfg.flatpak gets the runtime, the Platform or the Sdk at the branch
crafter-build runs on, and the sandbox's permissions, finish-args as
Flatpak spells them, which any configuration may declare: an executable
gets its dependencies' first and its own last, so it can take one back.
The build reads neither, and a change to either links nothing. install
and bundle print the permissions, with where each came from, and hand
them to build-finish, the one judge of what they mean, so a refusal can
be traced to the configuration that declared it. FlatpakPermissions
checks the order everywhere, and that a change to one neither moves the
build to another directory nor links anything, and, where the host's
flatpak runs, what build-finish makes of them and that it refuses a
socket it does not have; FlatpakBundle that build-finish exports the
desktop entry and the icon, and that the build's own files/ is left as
it was; VariantId's test has the runtime, permissions and modules move
no directory.
The host's flatpak and flatpak-builder are reached only through
HostCommand in :Platform, which builds the command lines of
RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's
probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak,
started in the host's / and passing FLATPAK_USER_DIR on, and the command
itself natively. flatpak-spawn would otherwise start a command in
crafter-build's own working directory, which the host need not have, and
from /app, say, every host command would fail. HostFlatpakUnavailable
says why the host cannot be reached: no org.freedesktop.Flatpak
permission, no session bus (CI's build sandbox has neither), a host that
does not see the project's bin/ and build/ where crafter-build does, as
it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or
where a link leads into one, or no flatpak natively. That, and an
executable that is not packaged as a Flatpak, install and bundle refuse
as soon as the project is loaded, before anything is built;
FlatpakVerbRefusal shows the second. The host is asked rather than the
sandbox's paths modelled, which an override sharing a directory inside
the sandbox's own would defeat: its flatpak is started in a directory
just made in each of bin/ and build/ under a name no other has, which it
enters only where it sees that very directory, and the directory is
removed again. --help gets a section on both, and its Clean section says
the repo an install points at goes with build/. FlatpakModules runs
flatpak-builder only with a flatpak-builder program on the host,
crafter-build falling back to the org.flatpak.Builder app, whose
`flatpak run` would write ~/.var/app in the real home, and with the
Platform and Sdk it builds the app with. The library's install function
is InstallFlatpakApp because project.cpp files define an InstallFlatpak
script of their own under `using namespace Crafter`, this repository's
included, and a library InstallFlatpak would make the name an overload
set, which the scripts table's std::function cannot be built from;
BundleFlatpakApp is named to match. JsonQuote writes the manifest's
strings, and variants.json's, which escaped quotes and backslashes only.
FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission,
which only :Clang uses, stay out of the API a project.cpp compiles
against, as RecordFlatpakPlaced does, so their names cannot meet a
project's own under `using namespace Crafter`; ExportFlatpak's comment
names the app's lock, which its callers hold, beside
HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std ;
import Crafter . Build ;
namespace fs = std : : filesystem ;
using namespace Crafter ;
namespace {
std : : int32_t Failures = 0 ;
void Check ( bool cond , std : : string_view msg ) {
if ( ! cond ) {
std : : println ( std : : cerr , " FAIL: {} " , msg ) ;
+ + Failures ;
}
}
std : : string ReadFile ( const fs : : path & path ) {
std : : ifstream in ( path , std : : ios : : binary ) ;
return std : : string ( std : : istreambuf_iterator < char > ( in ) , std : : istreambuf_iterator < char > ( ) ) ;
}
}
// crafter-build reaches the host's flatpak through one helper, and says why
// when it cannot. Inside its Flatpak that needs the org.freedesktop.Flatpak
// permission and a session bus, which flatpak-builder's build sandbox, where
// CI runs this suite, has neither of; and a host that sees the project's
// bin/ and build/ where crafter-build does, which the host is asked: it sees
// the fixture, in the home, and not a project in the sandbox's own /tmp, nor
// one a link in the home leads there. Every host command starts in the
// host's /: one started in crafter-build's working directory failed
// wherever the host has none such, /app for one. Natively the flatpak on
// PATH is the host's. Installing or bundling an executable that is not
// packaged as a Flatpak is refused, naming what to set, before the host is
// involved.
int main ( ) {
const fs : : path fixture = fs : : current_path ( ) / " tests " / " HostFlatpak " / " fixture " ;
// Whatever the question makes in the fixture, it removes again.
const auto probesLeft = [ & ] {
std : : error_code ec ;
for ( const std : : string_view sub : { " bin " , " build " } ) {
if ( ! fs : : is_directory ( fixture / sub , ec ) ) continue ;
for ( const fs : : directory_entry & entry : fs : : directory_iterator ( fixture / sub ) ) {
if ( entry . path ( ) . filename ( ) . string ( ) . starts_with ( " .crafter-build-host-probe- " ) ) return true ;
}
}
return false ;
} ;
std : : error_code ec ;
fs : : remove_all ( fixture / " bin " , ec ) ;
fs : : remove_all ( fixture / " build " , ec ) ;
const std : : string why = HostFlatpakUnavailable ( fixture ) ;
if ( fs : : exists ( " /.flatpak-info " ) ) {
const std : : string info = ReadFile ( " /.flatpak-info " ) ;
const bool permitted = info . contains ( " org.freedesktop.Flatpak=talk " ) ;
if ( ! permitted ) {
Check ( why . contains ( " org.freedesktop.Flatpak " ) , std : : format ( " without the permission, the reason names it: {} " , why ) ) ;
} else {
Check ( why . empty ( ) , std : : format ( " the host sees the fixture, in the home, where crafter-build does: {} " , why ) ) ;
Check ( ! fs : : exists ( fixture / " bin " ) & & ! fs : : exists ( fixture / " build " ) , " and asking leaves nothing in the fixture, which had no bin/ or build/ " ) ;
}
// A project in the sandbox's own /tmp, a tmpfs nothing outside the
// sandbox sees, which goes with it; and one reached through a link
// in the fixture's build/, in the home, that leads there, which the
// host follows to its own /tmp. Both are made for the question and
// removed again. Where an override shares the host's /tmp with the
// sandbox, the host sees both, so they are left out.
bool tmpShared = false ;
for ( std : : size_t at = info . find ( " filesystems= " ) ; at ! = std : : string : : npos & & ! tmpShared ; ) {
const std : : size_t end = info . find ( ' \n ' , at ) ;
const std : : string line = info . substr ( at , end = = std : : string : : npos ? std : : string : : npos : end - at ) ;
for ( std : : size_t begin = std : : string_view ( " filesystems= " ) . size ( ) ; begin < line . size ( ) ; ) {
std : : size_t next = line . find ( ' ; ' , begin ) ;
if ( next = = std : : string : : npos ) next = line . size ( ) ;
const std : : string entry = line . substr ( begin , next - begin ) ;
if ( entry = = " /tmp " | | entry . starts_with ( " /tmp: " ) ) tmpShared = true ;
begin = next + 1 ;
}
at = info . find ( " filesystems= " , at + 1 ) ;
}
test: HostFlatpak asks about projects in a directory of its own
In the Flatpak, HostFlatpak makes two projects in the sandbox's /tmp,
one there and one through a link in the fixture's build/ that leads
there, and asks whether the host sees them. They had fixed names,
/tmp/crafter-build-host-flatpak and crafter-build-host-flatpak-linked
beside it, and every sandbox of the app has the same /tmp, so two runs
at once asked about, and removed, the same projects. Both are now in a
directory the test makes there, a PrivateDirectory, which the link
leads to, and which goes afterwards with the link, or, when the test is
killed, when a later one is made, once it has not changed for a minute.
The test still names /tmp rather than the TMPDIR it is given, since the
sandbox's /tmp is what it asks the host about, and AGENTS.template.md's
entry for it says it makes a directory of its own there now.
2026-11-27 09:17:58 +01:00
// Both in a directory of this run's own there, a PrivateDirectory,
// removed at the end of this block: every sandbox of the app has the
// same /tmp, so two runs at once would ask about, and remove, the
// same projects, and what a run that is killed leaves goes when a
// later one is made, once it has not changed for a minute.
const PrivateDirectory own ( " /tmp " , " crafter-build-host-flatpak- " ) ;
const fs : : path & scratch = own . path ;
feat: install and bundle a Flatpak through the host's flatpak
An executable packaged as a Flatpak came out as the files/ of one, which
only flatpak build-finish makes an app of, and getting it installed or
onto another machine took flatpak commands on the host, typed by hand.
Two verbs do it where the host's flatpak can be reached:
`crafter-build install` builds the project and installs its app for this
user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a
single file for `flatpak install --user` that names Flathub's repository
for the runtime. Inside crafter-build's Flatpak they need the
org.freedesktop.Flatpak permission, which its manifest does not grant in
this commit. Both put the app into the project's repo first, in
build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than
per variant, since flatpak-builder's state there is its cache. Every
variant exports into it, so the export and the install or bundle made
from it hold a lock there, and another crafter-build exporting meanwhile
waits rather than leave this one installing or bundling its program. The
user remote an install re-points is every checkout's that builds the
app-id, so re-pointing it and installing from it hold a second lock,
which every crafter-build installing there takes, natively or in the
Flatpak, whatever its XDG_CACHE_HOME: in
~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's
own installation, which flatpak hides from the sandbox behind a tmpfs of
the sandbox's own, and in the directory of a private one,
FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that
directory with the host. Without modules, the host's flatpak
build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh
each time from a metadata naming the app, its runtime and the Sdk and a
tree of hard links to the build's files/, copies where a link cannot be
made, across filesystems or on one without links, vfat or exFAT: it
writes the command, the sandbox's permissions and export/ by flatpak's
own rules, those of whichever flatpak the host runs, and refuses a
permission it does not know, and flatpak build-export puts the result
into the repo. When cfg.flatpak.modules names flatpak-builder module
files for what the app needs in /app besides the program,
flatpak-builder does both, from a manifest that lists them and copies
the built program in last, so nothing is compiled twice. That manifest
keeps share/locale in the app ("separate-locales": false):
flatpak-builder would split it into a .Locale extension by default,
which a bundle, holding the app alone, leaves out. The loader rebases a
dependency's module files as it does its cfg.files. install uses a user
remote named after the app-id, re-pointed at the repo each time, and
says how to remove both. The app's tree in
build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a
module's files too, is left out of the include walk with every other
build/, so the headers a module installed reach no compile.
cfg.flatpak gets the runtime, the Platform or the Sdk at the branch
crafter-build runs on, and the sandbox's permissions, finish-args as
Flatpak spells them, which any configuration may declare: an executable
gets its dependencies' first and its own last, so it can take one back.
The build reads neither, and a change to either links nothing. install
and bundle print the permissions, with where each came from, and hand
them to build-finish, the one judge of what they mean, so a refusal can
be traced to the configuration that declared it. FlatpakPermissions
checks the order everywhere, and that a change to one neither moves the
build to another directory nor links anything, and, where the host's
flatpak runs, what build-finish makes of them and that it refuses a
socket it does not have; FlatpakBundle that build-finish exports the
desktop entry and the icon, and that the build's own files/ is left as
it was; VariantId's test has the runtime, permissions and modules move
no directory.
The host's flatpak and flatpak-builder are reached only through
HostCommand in :Platform, which builds the command lines of
RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's
probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak,
started in the host's / and passing FLATPAK_USER_DIR on, and the command
itself natively. flatpak-spawn would otherwise start a command in
crafter-build's own working directory, which the host need not have, and
from /app, say, every host command would fail. HostFlatpakUnavailable
says why the host cannot be reached: no org.freedesktop.Flatpak
permission, no session bus (CI's build sandbox has neither), a host that
does not see the project's bin/ and build/ where crafter-build does, as
it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or
where a link leads into one, or no flatpak natively. That, and an
executable that is not packaged as a Flatpak, install and bundle refuse
as soon as the project is loaded, before anything is built;
FlatpakVerbRefusal shows the second. The host is asked rather than the
sandbox's paths modelled, which an override sharing a directory inside
the sandbox's own would defeat: its flatpak is started in a directory
just made in each of bin/ and build/ under a name no other has, which it
enters only where it sees that very directory, and the directory is
removed again. --help gets a section on both, and its Clean section says
the repo an install points at goes with build/. FlatpakModules runs
flatpak-builder only with a flatpak-builder program on the host,
crafter-build falling back to the org.flatpak.Builder app, whose
`flatpak run` would write ~/.var/app in the real home, and with the
Platform and Sdk it builds the app with. The library's install function
is InstallFlatpakApp because project.cpp files define an InstallFlatpak
script of their own under `using namespace Crafter`, this repository's
included, and a library InstallFlatpak would make the name an overload
set, which the scripts table's std::function cannot be built from;
BundleFlatpakApp is named to match. JsonQuote writes the manifest's
strings, and variants.json's, which escaped quotes and backslashes only.
FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission,
which only :Clang uses, stay out of the API a project.cpp compiles
against, as RecordFlatpakPlaced does, so their names cannot meet a
project's own under `using namespace Crafter`; ExportFlatpak's comment
names the app's lock, which its callers hold, beside
HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
const fs : : path link = fixture / " build " / " to-tmp " ;
fs : : create_directories ( link . parent_path ( ) , ec ) ;
fs : : remove ( link , ec ) ;
test: HostFlatpak asks about projects in a directory of its own
In the Flatpak, HostFlatpak makes two projects in the sandbox's /tmp,
one there and one through a link in the fixture's build/ that leads
there, and asks whether the host sees them. They had fixed names,
/tmp/crafter-build-host-flatpak and crafter-build-host-flatpak-linked
beside it, and every sandbox of the app has the same /tmp, so two runs
at once asked about, and removed, the same projects. Both are now in a
directory the test makes there, a PrivateDirectory, which the link
leads to, and which goes afterwards with the link, or, when the test is
killed, when a later one is made, once it has not changed for a minute.
The test still names /tmp rather than the TMPDIR it is given, since the
sandbox's /tmp is what it asks the host about, and AGENTS.template.md's
entry for it says it makes a directory of its own there now.
2026-11-27 09:17:58 +01:00
fs : : create_directory_symlink ( scratch , link , ec ) ;
const std : : array < fs : : path , 2 > unseen = { scratch / " project " , link / " linked " } ;
feat: install and bundle a Flatpak through the host's flatpak
An executable packaged as a Flatpak came out as the files/ of one, which
only flatpak build-finish makes an app of, and getting it installed or
onto another machine took flatpak commands on the host, typed by hand.
Two verbs do it where the host's flatpak can be reached:
`crafter-build install` builds the project and installs its app for this
user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a
single file for `flatpak install --user` that names Flathub's repository
for the runtime. Inside crafter-build's Flatpak they need the
org.freedesktop.Flatpak permission, which its manifest does not grant in
this commit. Both put the app into the project's repo first, in
build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than
per variant, since flatpak-builder's state there is its cache. Every
variant exports into it, so the export and the install or bundle made
from it hold a lock there, and another crafter-build exporting meanwhile
waits rather than leave this one installing or bundling its program. The
user remote an install re-points is every checkout's that builds the
app-id, so re-pointing it and installing from it hold a second lock,
which every crafter-build installing there takes, natively or in the
Flatpak, whatever its XDG_CACHE_HOME: in
~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's
own installation, which flatpak hides from the sandbox behind a tmpfs of
the sandbox's own, and in the directory of a private one,
FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that
directory with the host. Without modules, the host's flatpak
build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh
each time from a metadata naming the app, its runtime and the Sdk and a
tree of hard links to the build's files/, copies where a link cannot be
made, across filesystems or on one without links, vfat or exFAT: it
writes the command, the sandbox's permissions and export/ by flatpak's
own rules, those of whichever flatpak the host runs, and refuses a
permission it does not know, and flatpak build-export puts the result
into the repo. When cfg.flatpak.modules names flatpak-builder module
files for what the app needs in /app besides the program,
flatpak-builder does both, from a manifest that lists them and copies
the built program in last, so nothing is compiled twice. That manifest
keeps share/locale in the app ("separate-locales": false):
flatpak-builder would split it into a .Locale extension by default,
which a bundle, holding the app alone, leaves out. The loader rebases a
dependency's module files as it does its cfg.files. install uses a user
remote named after the app-id, re-pointed at the repo each time, and
says how to remove both. The app's tree in
build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a
module's files too, is left out of the include walk with every other
build/, so the headers a module installed reach no compile.
cfg.flatpak gets the runtime, the Platform or the Sdk at the branch
crafter-build runs on, and the sandbox's permissions, finish-args as
Flatpak spells them, which any configuration may declare: an executable
gets its dependencies' first and its own last, so it can take one back.
The build reads neither, and a change to either links nothing. install
and bundle print the permissions, with where each came from, and hand
them to build-finish, the one judge of what they mean, so a refusal can
be traced to the configuration that declared it. FlatpakPermissions
checks the order everywhere, and that a change to one neither moves the
build to another directory nor links anything, and, where the host's
flatpak runs, what build-finish makes of them and that it refuses a
socket it does not have; FlatpakBundle that build-finish exports the
desktop entry and the icon, and that the build's own files/ is left as
it was; VariantId's test has the runtime, permissions and modules move
no directory.
The host's flatpak and flatpak-builder are reached only through
HostCommand in :Platform, which builds the command lines of
RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's
probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak,
started in the host's / and passing FLATPAK_USER_DIR on, and the command
itself natively. flatpak-spawn would otherwise start a command in
crafter-build's own working directory, which the host need not have, and
from /app, say, every host command would fail. HostFlatpakUnavailable
says why the host cannot be reached: no org.freedesktop.Flatpak
permission, no session bus (CI's build sandbox has neither), a host that
does not see the project's bin/ and build/ where crafter-build does, as
it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or
where a link leads into one, or no flatpak natively. That, and an
executable that is not packaged as a Flatpak, install and bundle refuse
as soon as the project is loaded, before anything is built;
FlatpakVerbRefusal shows the second. The host is asked rather than the
sandbox's paths modelled, which an override sharing a directory inside
the sandbox's own would defeat: its flatpak is started in a directory
just made in each of bin/ and build/ under a name no other has, which it
enters only where it sees that very directory, and the directory is
removed again. --help gets a section on both, and its Clean section says
the repo an install points at goes with build/. FlatpakModules runs
flatpak-builder only with a flatpak-builder program on the host,
crafter-build falling back to the org.flatpak.Builder app, whose
`flatpak run` would write ~/.var/app in the real home, and with the
Platform and Sdk it builds the app with. The library's install function
is InstallFlatpakApp because project.cpp files define an InstallFlatpak
script of their own under `using namespace Crafter`, this repository's
included, and a library InstallFlatpak would make the name an overload
set, which the scripts table's std::function cannot be built from;
BundleFlatpakApp is named to match. JsonQuote writes the manifest's
strings, and variants.json's, which escaped quotes and backslashes only.
FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission,
which only :Clang uses, stay out of the API a project.cpp compiles
against, as RecordFlatpakPlaced does, so their names cannot meet a
project's own under `using namespace Crafter`; ExportFlatpak's comment
names the app's lock, which its callers hold, beside
HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
for ( const fs : : path & project : unseen ) {
if ( tmpShared ) {
std : : println ( " an override shares the host's /tmp with the sandbox, so the host sees {}: left out " , project . string ( ) ) ;
continue ;
}
if ( ! permitted ) {
const std : : string refused = HostFlatpakUnavailable ( project ) ;
Check ( refused . contains ( " org.freedesktop.Flatpak " ) , std : : format ( " and so is one at {}, for the same reason: {} " , project . string ( ) , refused ) ) ;
continue ;
}
fs : : create_directories ( project , ec ) ;
const std : : string refused = HostFlatpakUnavailable ( project ) ;
Check ( refused . contains ( " the host does not see " ) & & refused . contains ( ( project / " bin " ) . string ( ) ) , std : : format ( " inside the Flatpak, a project at {} is refused, saying why: {} " , project . string ( ) , refused ) ) ;
Check ( fs : : is_empty ( project , ec ) , std : : format ( " and asking leaves nothing in {} " , project . string ( ) ) ) ;
fs : : remove_all ( fs : : weakly_canonical ( project , ec ) , ec ) ;
}
test: HostFlatpak asks about projects in a directory of its own
In the Flatpak, HostFlatpak makes two projects in the sandbox's /tmp,
one there and one through a link in the fixture's build/ that leads
there, and asks whether the host sees them. They had fixed names,
/tmp/crafter-build-host-flatpak and crafter-build-host-flatpak-linked
beside it, and every sandbox of the app has the same /tmp, so two runs
at once asked about, and removed, the same projects. Both are now in a
directory the test makes there, a PrivateDirectory, which the link
leads to, and which goes afterwards with the link, or, when the test is
killed, when a later one is made, once it has not changed for a minute.
The test still names /tmp rather than the TMPDIR it is given, since the
sandbox's /tmp is what it asks the host about, and AGENTS.template.md's
entry for it says it makes a directory of its own there now.
2026-11-27 09:17:58 +01:00
// The link goes with the directory it leads to.
fs : : remove ( link , ec ) ;
feat: install and bundle a Flatpak through the host's flatpak
An executable packaged as a Flatpak came out as the files/ of one, which
only flatpak build-finish makes an app of, and getting it installed or
onto another machine took flatpak commands on the host, typed by hand.
Two verbs do it where the host's flatpak can be reached:
`crafter-build install` builds the project and installs its app for this
user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a
single file for `flatpak install --user` that names Flathub's repository
for the runtime. Inside crafter-build's Flatpak they need the
org.freedesktop.Flatpak permission, which its manifest does not grant in
this commit. Both put the app into the project's repo first, in
build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than
per variant, since flatpak-builder's state there is its cache. Every
variant exports into it, so the export and the install or bundle made
from it hold a lock there, and another crafter-build exporting meanwhile
waits rather than leave this one installing or bundling its program. The
user remote an install re-points is every checkout's that builds the
app-id, so re-pointing it and installing from it hold a second lock,
which every crafter-build installing there takes, natively or in the
Flatpak, whatever its XDG_CACHE_HOME: in
~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's
own installation, which flatpak hides from the sandbox behind a tmpfs of
the sandbox's own, and in the directory of a private one,
FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that
directory with the host. Without modules, the host's flatpak
build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh
each time from a metadata naming the app, its runtime and the Sdk and a
tree of hard links to the build's files/, copies where a link cannot be
made, across filesystems or on one without links, vfat or exFAT: it
writes the command, the sandbox's permissions and export/ by flatpak's
own rules, those of whichever flatpak the host runs, and refuses a
permission it does not know, and flatpak build-export puts the result
into the repo. When cfg.flatpak.modules names flatpak-builder module
files for what the app needs in /app besides the program,
flatpak-builder does both, from a manifest that lists them and copies
the built program in last, so nothing is compiled twice. That manifest
keeps share/locale in the app ("separate-locales": false):
flatpak-builder would split it into a .Locale extension by default,
which a bundle, holding the app alone, leaves out. The loader rebases a
dependency's module files as it does its cfg.files. install uses a user
remote named after the app-id, re-pointed at the repo each time, and
says how to remove both. The app's tree in
build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a
module's files too, is left out of the include walk with every other
build/, so the headers a module installed reach no compile.
cfg.flatpak gets the runtime, the Platform or the Sdk at the branch
crafter-build runs on, and the sandbox's permissions, finish-args as
Flatpak spells them, which any configuration may declare: an executable
gets its dependencies' first and its own last, so it can take one back.
The build reads neither, and a change to either links nothing. install
and bundle print the permissions, with where each came from, and hand
them to build-finish, the one judge of what they mean, so a refusal can
be traced to the configuration that declared it. FlatpakPermissions
checks the order everywhere, and that a change to one neither moves the
build to another directory nor links anything, and, where the host's
flatpak runs, what build-finish makes of them and that it refuses a
socket it does not have; FlatpakBundle that build-finish exports the
desktop entry and the icon, and that the build's own files/ is left as
it was; VariantId's test has the runtime, permissions and modules move
no directory.
The host's flatpak and flatpak-builder are reached only through
HostCommand in :Platform, which builds the command lines of
RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's
probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak,
started in the host's / and passing FLATPAK_USER_DIR on, and the command
itself natively. flatpak-spawn would otherwise start a command in
crafter-build's own working directory, which the host need not have, and
from /app, say, every host command would fail. HostFlatpakUnavailable
says why the host cannot be reached: no org.freedesktop.Flatpak
permission, no session bus (CI's build sandbox has neither), a host that
does not see the project's bin/ and build/ where crafter-build does, as
it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or
where a link leads into one, or no flatpak natively. That, and an
executable that is not packaged as a Flatpak, install and bundle refuse
as soon as the project is loaded, before anything is built;
FlatpakVerbRefusal shows the second. The host is asked rather than the
sandbox's paths modelled, which an override sharing a directory inside
the sandbox's own would defeat: its flatpak is started in a directory
just made in each of bin/ and build/ under a name no other has, which it
enters only where it sees that very directory, and the directory is
removed again. --help gets a section on both, and its Clean section says
the repo an install points at goes with build/. FlatpakModules runs
flatpak-builder only with a flatpak-builder program on the host,
crafter-build falling back to the org.flatpak.Builder app, whose
`flatpak run` would write ~/.var/app in the real home, and with the
Platform and Sdk it builds the app with. The library's install function
is InstallFlatpakApp because project.cpp files define an InstallFlatpak
script of their own under `using namespace Crafter`, this repository's
included, and a library InstallFlatpak would make the name an overload
set, which the scripts table's std::function cannot be built from;
BundleFlatpakApp is named to match. JsonQuote writes the manifest's
strings, and variants.json's, which escaped quotes and backslashes only.
FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission,
which only :Clang uses, stay out of the API a project.cpp compiles
against, as RecordFlatpakPlaced does, so their names cannot meet a
project's own under `using namespace Crafter`; ExportFlatpak's comment
names the app's lock, which its callers hold, beside
HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
// From a working directory only the sandbox has, the host is reached
// all the same, and sees the fixture.
if ( permitted ) {
const fs : : path before = fs : : current_path ( ) ;
fs : : current_path ( " /app " ) ;
const std : : string fromApp = HostFlatpakUnavailable ( fixture ) ;
Check ( fromApp . empty ( ) , std : : format ( " from /app, which only the sandbox has, the host sees the fixture: {} " , fromApp ) ) ;
Check ( RunHostFlatpak ( " --version " ) = = 0 , " and the host's flatpak runs from there " ) ;
fs : : current_path ( before ) ;
}
Check ( ! probesLeft ( ) , " asking leaves no directory of its own in the fixture's bin/ or build/ " ) ;
} else if ( std : : system ( " command -v flatpak > /dev/null 2>&1 " ) = = 0 ) {
Check ( why . empty ( ) , std : : format ( " natively, the flatpak on PATH is reachable: {} " , why ) ) ;
Check ( RunHostFlatpak ( " --version " ) = = 0 , " and runs " ) ;
} else {
Check ( why . contains ( " flatpak " ) , std : : format ( " without flatpak, the reason says so: {} " , why ) ) ;
}
Configuration raw ;
raw . path = fixture ;
raw . name = " raw-app " ;
raw . outputName = " raw " ;
raw . target = HostTarget ( ) ;
raw . packaging = Packaging : : None ;
{
std : : array < fs : : path , 0 > ifaces = { } ;
std : : array < fs : : path , 1 > impls = { " main " } ;
raw . GetInterfacesAndImplementations ( ifaces , impls ) ;
}
Check ( InstallFlatpakApp ( raw ) . contains ( " cfg.packaging = Packaging::Flatpak " ) , std : : format ( " installing an executable that is no Flatpak says what to set: {} " , InstallFlatpakApp ( raw ) ) ) ;
Check ( BundleFlatpakApp ( raw ) . contains ( " cfg.packaging = Packaging::Flatpak " ) , std : : format ( " bundling one says the same: {} " , BundleFlatpakApp ( raw ) ) ) ;
if ( Failures > 0 ) {
std : : println ( std : : cerr , " {} assertions failed " , Failures ) ;
return 1 ;
}
return 0 ;
}