Building on a Flatpak SDK

Here we demonstrate how to build and run software using a Flatpak SDK for the base runtime.

Note

This example is distributed with BuildStream in the doc/examples/flatpak-autotools subdirectory.

Project structure

The following is a simple project definition:

project.conf

name: flatpak-autotools

min-version: 2.0

aliases:
  flathub: https://dl.flathub.org/

element-path: elements

options:
  arch:
    type: arch
    description: The machine architecture
    values:
    - x86-64
    - x86-32

plugins:
  - origin: pip
    package-name: bst-plugins-experimental
    sources:
    - ostree

Here we use an arch option to allow conditional statements in this project to be made depending on machine architecture. For this example we only support the i386 and x86_64 architectures.

Note that we’ve added a source alias for the https://dl.flathuhb.org/ repository to download the SDK from.

elements/base/sdk.bst

kind: import
description: Import the base freedesktop SDK
sources:
- kind: ostree
  url: flathub:repo/
  gpg-key: keys/flathub.gpg
  (?):
  - arch == "x86-64":
      track: runtime/org.freedesktop.BaseSdk/x86_64/1.6
      ref: 7306169ea9c563f3ce75bb57be9e94b0acf1d742edacab0aa751cf6646a4b52e
  - arch == "x86-32":
      track: runtime/org.freedesktop.BaseSdk/i386/1.6
      ref: 63f9537eea89448ec865f907a3ec89b261493b3d999121a81603c827b6219d20
config:
  source: files
  target: usr

This is the import element used to import the actual Flatpak SDK, it uses an ostree source to download the Flatpak since these are hosted in OSTree repositories.

While declaring the ostree source, we specify a GPG public key to verify the OSTree download. This configuration is optional but recommended for OSTree repositories. The key is stored in the project directory at keys/gnome-sdk.gpg, and can be downloaded from https://sdk.gnome.org/keys/.

We also use conditional statements to decide which branch to download.

For the config section of this import element, it’s important to note two things:

  • source: We only want to extract the files/ directory from the SDK,

    This is becase Flatpak runtimes dont start at the root of the OSTree checkout, instead the actual files start in the files/ subdirectory

  • target: The content we’ve extracted should be staged at /usr

    This is because Flatpak runtimes only contain the data starting at /usr, and they expect to be staged at /usr at runtime, in an environment with the appropriate symlinks setup from /.

elements/base/usrmerge.bst

kind: import
description: Base usr merge symlinks

# Depend on the base-sdk.bst such that the
# symlinks get added after staging the SDK
depends:
- base/sdk.bst

sources:
- kind: local
  path: files/links

This is another import element, and it uses the local source type so that we can stage files literally stored in the same repository as the project.

The purpose of this element is simply to add the symlinks for /lib -> /usr/lib, /bin -> /usr/bin and /etc -> /usr/etc, we have it depend on the base/sdk.bst element only to ensure that it is staged after, i.e. the symlinks are created after the SDK is staged.

As suggested by the .bst file, the symlinks themselves are a part of the project and they are stored in the files/links directory.

elements/base.bst

kind: stack
description: Base stack

depends:
- base/sdk.bst
- base/usrmerge.bst

This is just a stack element for convenience sake.

Often times you will have a more complex base to build things on, and it is convenient to just use a stack element for your elements to depend on without needing to know about the inner workings of the base system build.

elements/hello.bst

kind: autotools
description: Autotools project

depends:
- base.bst

sources:
- kind: local
  path: files/src

Finally, we show an example of an autotools element to build our sample “Hello World” program.

We use another local source to obtain the sample autotools project, but normally you would probably use a git or other source to obtain source code from another repository.

Using the project

Now that we’ve explained the basic layout of the project, here are just a few things you can try to do with the project.

Note

The following examples assume that you have first changed your working directory to the project root.

Build the hello.bst element

To build the project, run bst build in the following way:

user@host:~/flatpak-autotools$ bst build hello.bst

[--:--:--][        ][    main:core activity                 ] START   Build
[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:core activity                 ] START   Checking sources
[00:00:00][        ][    main:core activity                 ] SUCCESS Checking sources

BuildStream Version 1.93.5
    Session Start: Wednesday, 19-08-2020 at 14:54:25
    Project:       flatpak-autotools (/home/user/flatpak-autotools)
    Targets:       hello.bst

User Configuration
    Configuration File:      /home/user/.config/buildstream.conf
    Cache Directory:         /home/user/.cache/buildstream
    Log Files:               /home/user/.cache/buildstream/logs
    Source Mirrors:          /home/user/.cache/buildstream/sources
    Build Area:              /home/user/.cache/buildstream/build
    Strict Build Plan:       Yes
    Maximum Fetch Tasks:     10
    Maximum Build Tasks:     4
    Maximum Push Tasks:      4
    Maximum Network Retries: 2

Project: flatpak-autotools

    Project Options
        arch: x86-64

    Element Plugins
        autotools: core plugin
        stack:     core plugin
        import:    core plugin

    Source Plugins
        local:  core plugin
        ostree: python package 'bst-plugins-experimental 1.93.4' at: /builds/BuildStream/buildstream/.tox/docs/lib/python3.7/site-packages

Pipeline
   buildable c41666fd5e499d795e1d1eb6311ee93dd96e035567757527d2df2914bd2201ca base/sdk.bst 
     waiting 7821f0388a7bf79b7c8381a366573be629c2bdb47add2dfb5b3a9fdb56f06898 base/usrmerge.bst 
     waiting c54aea1444248a5a47ae414ab55deb4cd91d67ccb067ebf836970e46eb949d54 base.bst 
     waiting 616b3e3177c56f81d3150b73b3501a44e280d0d6172271c10e8340010f298c37 hello.bst 
===============================================================================
[--:--:--][c41666fd][   build:base/sdk.bst                  ] START   flatpak-autotools/base-sdk/c41666fd-build.2040.log
[--:--:--][c41666fd][   build:base/sdk.bst                  ] START   Staging sources
[00:00:00][c41666fd][   build:base/sdk.bst                  ] SUCCESS Staging sources
[--:--:--][c41666fd][   build:base/sdk.bst                  ] START   Caching artifact
[00:00:00][c41666fd][   build:base/sdk.bst                  ] SUCCESS Caching artifact
[00:00:01][c41666fd][   build:base/sdk.bst                  ] SUCCESS flatpak-autotools/base-sdk/c41666fd-build.2040.log
[--:--:--][7821f038][   build:base/usrmerge.bst             ] START   flatpak-autotools/base-usrmerge/7821f038-build.2045.log
[--:--:--][7821f038][   build:base/usrmerge.bst             ] START   Staging sources
[00:00:00][7821f038][   build:base/usrmerge.bst             ] SUCCESS Staging sources
[--:--:--][7821f038][   build:base/usrmerge.bst             ] START   Caching artifact
[00:00:00][7821f038][   build:base/usrmerge.bst             ] SUCCESS Caching artifact
[00:00:00][7821f038][   build:base/usrmerge.bst             ] SUCCESS flatpak-autotools/base-usrmerge/7821f038-build.2045.log
[--:--:--][c54aea14][   build:base.bst                      ] START   flatpak-autotools/base/c54aea14-build.2049.log
[--:--:--][c54aea14][   build:base.bst                      ] START   Caching artifact
[00:00:00][c54aea14][   build:base.bst                      ] SUCCESS Caching artifact
[00:00:00][c54aea14][   build:base.bst                      ] SUCCESS flatpak-autotools/base/c54aea14-build.2049.log
[--:--:--][616b3e31][   build:hello.bst                     ] START   flatpak-autotools/hello/616b3e31-build.2053.log
[--:--:--][616b3e31][   build:hello.bst                     ] START   Staging dependencies
[00:00:00][616b3e31][   build:hello.bst                     ] SUCCESS Staging dependencies
[--:--:--][616b3e31][   build:hello.bst                     ] START   Staging sources
[00:00:00][616b3e31][   build:hello.bst                     ] SUCCESS Staging sources
[--:--:--][616b3e31][   build:hello.bst                     ] START   Running commands

    export NOCONFIGURE=1;
    
    if [ -x ./configure ]; then true;
    elif [ -x ./autogen ]; then ./autogen;
    elif [ -x ./autogen.sh ]; then ./autogen.sh;
    elif [ -x ./bootstrap ]; then ./bootstrap;
    elif [ -x ./bootstrap.sh ]; then ./bootstrap.sh;
    else autoreconf -ivf .;
    fi
    ./configure --prefix=/usr \
    --exec-prefix=/usr \
    --bindir=/usr/bin \
    --sbindir=/usr/sbin \
    --sysconfdir=/etc \
    --datadir=/usr/share \
    --includedir=/usr/include \
    --libdir=/usr/lib \
    --libexecdir=/usr/libexec \
    --localstatedir=/var \
    --sharedstatedir=/usr/com \
    Message contains 23 additional lines

[00:00:04][616b3e31][   build:hello.bst                     ] SUCCESS Running commands
[--:--:--][616b3e31][   build:hello.bst                     ] START   Caching artifact
[00:00:00][616b3e31][   build:hello.bst                     ] SUCCESS Caching artifact
[00:00:04][616b3e31][   build:hello.bst                     ] SUCCESS flatpak-autotools/hello/616b3e31-build.2053.log
[00:00:06][        ][    main:core activity                 ] SUCCESS Build

Pipeline Summary
    Total:       4
    Session:     4
    Fetch Queue: processed 0, skipped 4, failed 0 
    Build Queue: processed 4, skipped 0, failed 0

Run the hello world program

The hello world program has been built into the standard /usr prefix, and will automatically be in the default PATH for running things in a bst shell.

To just run the program, run bst shell in the following way:

user@host:~/flatpak-autotools$ bst shell hello.bst -- hello

[--:--:--][        ][    main:core activity                 ] START   Loading elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Loading elements
[--:--:--][        ][    main:core activity                 ] START   Resolving elements
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving elements
[--:--:--][        ][    main:core activity                 ] START   Resolving cached state
[00:00:00][        ][    main:core activity                 ] SUCCESS Resolving cached state
[--:--:--][        ][    main:hello.bst                     ] START   Staging dependencies
[00:00:00][        ][    main:hello.bst                     ] SUCCESS Staging dependencies
[--:--:--][        ][    main:hello.bst                     ] START   Integrating sandbox
[00:00:00][        ][    main:hello.bst                     ] SUCCESS Integrating sandbox
[--:--:--][        ][    main:hello.bst                     ] STATUS  Running command

    hello

2020-08-19T14:54:32.884+0000 [3007:140024975730752] [buildboxcommon_runner.cpp:547] [WARNING] The --use-localcas option will be deprecated. LocalCAS support is now enabled by default.
2020-08-19T14:54:32.885+0000 [3007:140024975730752] [buildboxcommon_client.cpp:87] [INFO] Setting d_maxBatchTotalSizeBytes = 4128768 bytes by default
Hello World!
This is amhello 1.0.