|
1 |
| -Building your first Flatpak |
| 1 | +Building Your First Flatpak |
2 | 2 | ===========================
|
3 | 3 |
|
4 |
| -This tutorial provides a quick introduction to building Flatpaks. In it, you will learn how to create a basic Flatpak application, which can be installed and run. |
| 4 | +This tutorial provides a quick, step-by-step example of how to build a simple application as a Flatpak. It also takes the opportunity to explain a few key concepts. |
5 | 5 |
|
6 |
| -Please follow the :doc:`setup` page before doing this tutorial. |
| 6 | +In order to complete this tutorial, you should have followed the `setup guide on flatpak.org <http://flatpak.org/setup/>`_. You also need to have installed ``flatpak-builder``, which is usually available from the same repository as the ``flatpak`` package. |
7 | 7 |
|
8 |
| -1. Install a runtime and the matching SDK |
9 |
| ------------------------------------------ |
| 8 | +1. Create a manifest |
| 9 | +-------------------- |
10 | 10 |
|
11 |
| -Flatpak requires every app to specify a runtime that it uses for its basic |
12 |
| -dependencies. Each runtime has a matching SDK (Software Development Kit), which |
13 |
| -contains all the things that are in the runtime, plus headers and development |
14 |
| -tools. This SDK is required to build apps for the runtime. |
15 |
| - |
16 |
| -In this tutorial we will use the Freedesktop 1.6 runtime and SDK. To install these, run:: |
17 |
| - |
18 |
| - $ flatpak install flathub org.freedesktop.Platform//1.6 org.freedesktop.Sdk//1.6 |
19 |
| - |
20 |
| -2. Create the app |
21 |
| ------------------ |
22 |
| - |
23 |
| -The app that is going to be created for this tutorial is a simple script. To |
24 |
| -create it, copy the following:: |
25 |
| - |
26 |
| - #!/bin/sh |
27 |
| - echo "Hello world, from a sandbox" |
28 |
| - |
29 |
| -Now paste this into an empty file and save it as `hello.sh`. |
30 |
| - |
31 |
| -3. Add a manifest |
32 |
| ------------------ |
33 |
| - |
34 |
| -Each Flatpak is built using a manifest file which provides basic information about the application and instructions for how it is to be built. To add a manifest to the hello world app, add the following to an empty file: |
| 11 | +The input to ``flatpak-builder`` is a JSON file that describes the parameters for building the application. This is called the manifest. The following example is the manifest for the GNOME Dictionary application: |
35 | 12 |
|
36 | 13 | .. code-block:: json
|
37 | 14 |
|
38 | 15 | {
|
39 |
| - "app-id": "org.flatpak.Hello", |
40 |
| - "runtime": "org.freedesktop.Platform", |
41 |
| - "runtime-version": "1.6", |
42 |
| - "sdk": "org.freedesktop.Sdk", |
43 |
| - "command": "hello.sh", |
44 |
| - "modules": [ |
| 16 | + "app-id": "org.gnome.Dictionary", |
| 17 | + "runtime": "org.gnome.Platform", |
| 18 | + "runtime-version": "3.26", |
| 19 | + "sdk": "org.gnome.Sdk", |
| 20 | + "command": "gnome-dictionary", |
| 21 | + "finish-args": [ |
| 22 | + "--socket=x11", |
| 23 | + "--share=network" |
| 24 | + ], |
| 25 | + "modules": [ |
| 26 | + { |
| 27 | + "name": "gnome-dictionary", |
| 28 | + "sources": [ |
45 | 29 | {
|
46 |
| - "name": "hello", |
47 |
| - "buildsystem": "simple", |
48 |
| - "build-commands": [ |
49 |
| - "install -D hello.sh /app/bin/hello.sh" |
50 |
| - ], |
51 |
| - "sources": [ |
52 |
| - { |
53 |
| - "type": "file", |
54 |
| - "path": "hello.sh" |
55 |
| - } |
56 |
| - ] |
| 30 | + "type": "archive", |
| 31 | + "url": "https://download.gnome.org/sources/gnome-dictionary/3.26/gnome-dictionary-3.26.0.tar.xz", |
| 32 | + "sha256": "387ff8fbb8091448453fd26dcf0b10053601c662e59581097bc0b54ced52e9ef" |
57 | 33 | }
|
58 |
| - ] |
| 34 | + ] |
| 35 | + } |
| 36 | + ] |
59 | 37 | }
|
60 | 38 |
|
61 |
| -Now save the file alongside `hello.sh` and call it `org.flatpak.Hello.json`. |
62 |
| - |
63 |
| -In a more complex application, the manifest would list multiple modules. The |
64 |
| -last one would typically be the application itself, and the earlier ones would |
65 |
| -be dependencies that are bundled with the app because they are not part of the |
66 |
| -runtime. |
| 39 | +As can be seen, this manifest includes basic information about the application, including: |
67 | 40 |
|
68 |
| -4. Build the application |
69 |
| ------------------------- |
| 41 | +- ``app-id`` - the application ID |
| 42 | +- ``runtime`` - the runtime, which provides the environment that the application will run in, as well as basic dependencies it can use |
| 43 | +- ``sdk`` - the Software Development Kit, which is a version of the runtime with development files and headers; this is required to build the app |
| 44 | +- ``command`` - the command that is used to run the application |
| 45 | +- ``finish-args`` - configuration options which give the application access to resources outside of its sandbox; in this case, the application is being given network access and access to the X11 display server |
70 | 46 |
|
71 |
| -Now that the app has a manifest, `flatpak-builder` can be used to build it. |
72 |
| -This is done by specifying the manifest file and a target directory:: |
| 47 | +The next part of the manifest is the ``modules`` list. This describes each of the modules that are to be built as part of the build process. One of these modules is always the application. Others can be libraries and other resources that are bundled as part of the application. |
73 | 48 |
|
74 |
| - $ flatpak-builder app-dir org.flatpak.Hello.json |
| 49 | +Module sources can be of several types, including ``.tar`` or ``.zip`` archives, Git or Bzr repositories. In this case, there is only one module, which is a ``.tar`` file which will be downloaded and built. |
75 | 50 |
|
76 |
| -This command will build each module that is listed in the manifest and install |
77 |
| -it to the `/app` subdirectory, inside the `app-dir` directory. |
| 51 | +The modules section of the Dictionary manifest is short, because only one module is built: the application itself. |
78 | 52 |
|
79 |
| -5. Test the build |
80 |
| ------------------ |
| 53 | +To create a manifest for the Dictionary, create a file called ``org.gnome.Dictionary.json`` and paste the JSON from above into it. |
81 | 54 |
|
82 |
| -To verify that the build was successful, run the following:: |
| 55 | +2. Run the build |
| 56 | +---------------- |
83 | 57 |
|
84 |
| - $ flatpak-builder --run app-dir org.flatpak.Hello.json hello.sh |
| 58 | +To use the manifest to build the Dictionary application, run the following command:: |
85 | 59 |
|
86 |
| -Congratulations, you've made an app! |
| 60 | + $ flatpak-builder --repo=tutorial-repo dictionary org.gnome.Dictionary.json |
87 | 61 |
|
88 |
| -6. Put the app in a repository |
89 |
| ------------------------------- |
| 62 | +This will: |
90 | 63 |
|
91 |
| -Before you can install and run the app, it first needs to be put in a |
92 |
| -repository. This is done by passing the `--repo` argument to `flatpak-builder`:: |
| 64 | +* Create a new directory called dictionary |
| 65 | +* Download and verify the Dictionary source code |
| 66 | +* Build and install the source code, inside the SDK rather than the host system |
| 67 | +* Finish the build, by setting permissions (in this case giving access to X11 and the network) |
| 68 | +* Create a new repository called repo (if it doesn't exist) and export the resulting build into it |
93 | 69 |
|
94 |
| - $ flatpak-builder --repo=repo --force-clean app-dir org.flatpak.Hello.json |
| 70 | +``flatpak-builder`` will also do some other useful things, like creating a separately installable debug runtime (called ``org.gnome.Dictionary.Debug`` in this case) and a separately installable translation runtime (called ``org.gnome.Dictionary.Locale``). |
95 | 71 |
|
96 |
| -This does the build again, and at the end exports the result to a local |
97 |
| -directory called `repo`. Note that `flatpak-builder` keeps a cache of previous |
98 |
| -builds in the `.flatpak-builder` subdirectory, so doing a second build like |
99 |
| -this is very fast. |
| 72 | +3. Add the new repository |
| 73 | +------------------------- |
100 | 74 |
|
101 |
| -This second time we passed in `--force-clean`, which means that the previously |
102 |
| -created `app-dir` directory was deleted before the new build was started. |
| 75 | +To test the application that has been built, you need to add the new repository that has been created:: |
103 | 76 |
|
104 |
| -7. Install the app |
105 |
| ------------------- |
| 77 | + $ flatpak --user remote-add --no-gpg-verify --if-not-exists tutorial-repo tutorial-repo |
106 | 78 |
|
107 |
| -Now we're ready to add the repository that was just created and install the |
108 |
| -app. This is done with two commands:: |
| 79 | +4. Install the application |
| 80 | +-------------------------- |
109 | 81 |
|
110 |
| - $ flatpak --user remote-add --no-gpg-verify tutorial-repo repo |
111 |
| - $ flatpak --user install tutorial-repo org.flatpak.Hello |
| 82 | +The next step is to install the Dictionary application from the repository. To do this, run:: |
112 | 83 |
|
113 |
| -The first command adds the repository that was created in the previous step. |
114 |
| -The second command installs the app from the repository. |
| 84 | + $ flatpak --user install tutorial-repo org.gnome.Dictionary |
115 | 85 |
|
116 |
| -Both these commands use the `--user` argument, which means that the repository |
117 |
| -and the app are added per-user rather than system-wide. This is useful for testing. |
| 86 | +To check that the application has been successfully installed, you can compare the sha256 commit of the installed app with the commit ID that was printed by ``flatpak-builder``:: |
118 | 87 |
|
119 |
| -Note that the repository was added with `--no-gpg-verify`, since a GPG key |
120 |
| -wasn't specified when the app was built. This is fine for testing, but for |
121 |
| -official repositories you should sign them with a private GPG key. |
| 88 | + $ flatpak info org.gnome.Dictionary |
| 89 | + $ flatpak info org.gnome.Dictionary.Locale |
122 | 90 |
|
123 |
| -8. Run the app |
124 |
| --------------- |
| 91 | +5. Run the application |
| 92 | +---------------------- |
125 | 93 |
|
126 |
| -All that's left is to try the app. This can be done with the following command:: |
| 94 | +Finally, you can run the application that you've built:: |
127 | 95 |
|
128 |
| - $ flatpak run org.flatpak.Hello |
| 96 | + $ flatpak run org.gnome.Dictionary |
129 | 97 |
|
130 |
| -This runs the app, so that it prints `Hello world, from a sandbox`. |
| 98 | +The rest of the documentation provides a complete guide to using ``flatpak-builder``. If you are new to Flatpak, it is recommended to start with the :doc:`introduction`. |
0 commit comments