1=================================
2Package Management Infrastructure
3=================================
4This pages gives an overview of what components belong to Haiku's package
5management infrastructure and how they work and interact.
6
7- **package** and **package_repo** are command line tools for building package
8  and package repository files. They are discussed in `Building Packages`_.
9- **packagefs** is the file system that virtually extracts activated packages.
10- The **package kit** is an API for package management related programming.
11- The **dependency solver** is a part of the package kit. It solves dependencies
12  between packages.
13- The **package management daemon** (short: package daemon) is a background
14  process that is activated whenever the user adds packages to or removes them
15  from one of their activation locations. It verifies that all dependencies are
16  fulfilled (prompting the user, if necessary) and performs whatever package
17  pre-activation/post-deactivation tasks are required. The daemon is also
18  contacted by the package manager (after it has resolved dependencies and
19  downloaded all required packages) to do the package de-/activation.
20- The **package manager** provides the user interface for software installation,
21  update, and removal. There are actually two programs, ``pkgman``, a command
22  line tool, and, ``HaikuDepot``, a GUI application.
23
24.. _Building Packages: BuildingPackages.rst
25
26.. image:: package-management-infrastructure.png
27   :align: center
28
29Software Installation Locations
30===============================
31In Haiku there are two main locations where software is installed.
32"/boot/system" and "/boot/home/config". "/boot/system" is where system-wide
33software (i.e. software for all users), including the base system, is installed,
34while "/boot/home/config" is only for the user's software. The discrimination
35between those two doesn't make that much sense yet, but when Haiku supports
36multi-user it will (obviously each user will need their own home directory then,
37e.g. "/boot/home/<user>").
38
39At both main installation locations a packagefs instance is mounted. Each
40instance presents a virtually extracted union of the contents of all packages in
41the subdirectory "packages" of that location. E.g. if one would extract the
42contents of all package files in "/boot/system/packages" to an actual directory,
43that would match exactly what is presented by packagefs in "/boot/system". With
44a few exceptions -- packagefs provides several additional directories.
45
46There are so-called shine-through directories which live on the underlying BFS
47volume. Normally a file system mounted at a directory would completely hide
48anything that is in that directory. These shine-through directories are handled
49specially, though; packagefs shows them just like they are on the BFS volume.
50One of those directories is "packages". This is necessary since otherwise it
51wouldn't be possible to add, remove, or update any packages. Further
52shine-through directories are "settings", "cache", "var", and "non-packaged".
53The latter is a place where software can be installed the "traditional",
54i.e. unpackaged, way.
55
56Software Installation
57=====================
58Manual Installation
59-------------------
60At the lowest level software can be installed by simply dropping a respective
61package file in a "packages" subdirectory of one of "/boot/system" or
62"/boot/home/config". The package daemon, a background process that sleeps most
63of the time, monitors the directory and, when happy with the newly added
64package, it asks packagefs to presents its contents on the fly in the directory
65structure. The package is said to be activated. Removing a package has the
66opposite effect.
67
68Things are a bit more complicated due to the fact that packages usually have
69dependencies. E.g. when adding a package that has an unsatisfied dependency
70(e.g. needs a certain library that is not installed) it is not a good idea to
71activate the package nonetheless. The package contents (programs, libraries,...)
72might not work correctly, and, e.g. when shadowing other installed software,
73might even break things that worked before.
74
75That's why the package daemon doesn't just activate any well-formed packages.
76Instead it examines the new situation and checks whether all dependencies are
77fulfilled and whether there are any conflicts. If they aren't any problems, it
78tells packagefs to activate/deactivate the packages as requested. In case there
79are issues with the dependencies, according to how it has been configured via
80settings, the daemon prompts the user immediately, checks remote repositories
81for solutions to the problem and presents the user with the possible options, or
82it even performs all necessary actions without bothering the user, if possible.
83In the end, if the problems could be solved (e.g. by downloading additional
84packages), the respective packages will be de-/activated, or, otherwise, nothing
85will be changed.
86
87To avoid always having to check all dependencies when booting, the package
88daemon writes the last consistent state of package activations to the file
89"packages/administrative/activated-packages". When being mounted packagefs,
90reads that file and only activates the packages specified by it. If the file is
91missing or packages it refers to cannot be found or loaded, packagefs falls back
92to activating all packages in the "packages" directory. The package daemon, once
93started, checks the state.
94
95Installation via Package Manager
96--------------------------------
97While manual software installation is possible, the more comfortable way is to
98use the package manager. The package manager has a configurable list of remote
99software repositories. It knows what software is available in those repositories
100and what is installed locally. After the user has selected software packages to
101be installed/deinstalled, package dependencies are resolved, and packages are
102downloaded and moved to their installation location.
103
104The package manager prepares a transaction directory, a subdirectory in the
105"packages/administrative" directory, which contains the new packages. It then
106contacts the package management daemon (via the package kit) to perform the
107package activation changes. The daemon moves the new packages to the "packages"
108directory, moves obsolete packages to an old state directory (also a
109subdirectory in the "packages/administrative" directory, with the current
110date and time encoded in its name) and asks packagefs to activate/deactivate the
111respective packages. The old state directories allow recovery of old states.
112That is particularly interesting for the system installation location. As as
113safe mode/recovery option, the boot loader offers the user to select an old
114installation state which can then be booted into, instead of the latest state.
115
116Application Bundles
117-------------------
118Haiku also supports a concept that is commonly referred to as application
119bundles. An application bundle is a fully self-contained package that doesn't
120need to be installed anywhere. The implementation details have not yet been
121decided on. The basic idea is to either mount a dedicated packagefs with the
122content of such a package or have a special location where one of the three
123already mounted packagefs instances (likely the "/boot/home/config" one) shows
124that content. With a bit of Tracker (or even libbe) integration that will allow
125the mounted directory to be opened or the application to be started when such a
126package file has been double-clicked.
127
128Installation Location Order and Consistency
129-------------------------------------------
130Having two separate installation locations for software requires some
131considerations regarding their consistency and interactions. There's a
132well-defined order of the installation locations: "/boot/home/config",
133"/boot/system". This has already been the order in which on BeOS commands,
134libraries, and add-ons where searched (according to the environmental variables
135``PATH``, ``LIBRARY_PATH``, and ``ADDON_PATH``). That e.g. allows a user to
136install a new/different version of a program in "/boot/home/config" and have it
137override the version in "/boot/system".
138
139This order also needs to be the order in which package dependencies are
140directed. While it shall be possible to have a library in "/boot/home/config"
141override one in "/boot/system" and have programs installed in the latter
142location use the overriding library, packages in an installation location must
143not have dependencies that can only be resolved to packages installed in a
144location that is prior according to the order. E.g. a program installed in
145"/boot/system" must not depend on a library that is only installed in
146"/boot/home/config". When going multi-user that would mean the program would
147work for one user, but not for another one who hasn't installed the library.
148Consequently "/boot/system" is fully self-contained. All dependencies must be
149resolved within it. A safe-mode boot should be possible with only the
150"/boot/system" packagefs being mounted. As a consequence these constraints have
151to be respected when software is installed or uninstalled.
152
153Another challenge that comes with having two installation locations is that some
154packages have compiled-in absolute paths to their own files (e.g. data files) or
155to their dependencies. The former could be solved by building two different
156versions of a package, but that wouldn't work with the latter and would be
157tedious anyway. The solution are dynamically generated symbolic links in a fixed
158location, "/boot/system/package-links" (symlinked to "/packages"), that for each
159installed package and its dependencies refer to the respective installation
160location.
161
162For each installed package a subdirectory named like the package (package name
163plus version) will be generated automatically. That subdirectory contains a
164symlink ".self" which refers to the installation location of the package itself
165as well as a symlink for each of its dependencies pointing to their installation
166locations. Furthermore there's a symlink ".settings" which points to the
167installation location's directory for global settings. E.g. for an OpenSSH
168package installed in "/boot/home/config" and OpenSSL installed in "/boot/system"
169the directory could look like this::
170
171  /boot/system/package-links/openssh-5.8p2-1/
172  	.self		-> ../../../home/config
173  	.settings	-> ../../../home/config/settings/global
174  	haiku		-> ../..
175  	lib:libssl	-> ../..
176
177Installing a different, compatible version of OpenSSL in "/boot/home/config"
178would automatically change the respective dependency symlink. Once supporting
179multi-user fully, the symlinks targets would also depend on the user ID of the
180process that checks them, so software installed only for the user is handled
181correctly.
182
183While it depends on the location the package has been installed in where the
184paths refer to, the absolute paths of the package links themselves remain
185stable. So they can be compiled in, when a package is built, and will work
186regardless of where the package is installed.
187
188Another problem the package links can solve are incompatible versions of the
189same package being installed in different locations. E.g. when a program and
190another program it depends on are installed in "/boot/system", installing an
191incompatible version of the latter in "/boot/home/config" will not break the
192former, since the dependency link will continue to point to the compatible
193version. With a bit of help from the runtime loader the same would work for
194libraries. In practice that's less of a problem, though, since libraries usually
195have a naming scheme and matching shared object names that already prevent
196mismatches.
197
198Software Repositories
199=====================
200A software repository is a collection of packages, usually accessible via the
201internet. Haiku's package management solution allows to refer to any number of
202software repositories from which packages can be downloaded and installed. The
203structure of the respository is very simple. It's just a set of files which can
204be downloaded via a common protocol (HTTP or FTP). One file is the repository
205index file in `HPKR format`_. It lists all packages that are available in the
206repository together with their descriptions and dependency information. It is
207downloaded and cached, allowing user interfaces to show the information and the
208dependency solver to do the computation locally. The other files are the
209individual package files.
210
211.. _HPKR format: FileFormat.rst#haiku-package-repository-format
212
213Standard Repositories
214---------------------
215There are two standard repositories for Haiku:
216
217- the Haiku repository, which only contains the small set of packages that is
218  built by Haiku's build system (haiku.hpkg, haiku_devel.hpkg, etc.) and
219- the HaikuPorts repository, which contains the packages maintained by
220  HaikuPorts.
221
222For the different builds and releases there are different instances of those two
223repositories:
224
225- There are snapshot repository instances for any repository version that was
226  ever available (to save space old versions may be removed/thinned out). Those
227  repositories will never be updated. Their main purpose is to be able to
228  retrospectively get a certain Haiku version for testing and comparisons.
229- For each official major release there will be an instance of the two
230  repositories. For small updates the repositories will simply be updated. An
231  official Haiku release is pre-configured with the corresponding repositories,
232  so that the user can conveniently keep their Haiku up-to-date. The update to
233  the next major release has to be requested explicitly.
234- Similar to the nightly images there are repository instances that are
235  continuously updated to the latest head of development. Those are suitable
236  mainly for testers and developers.
237- For each state of the HaikuPorts repository a Haiku development revision
238  refers to a snapshot version of the repository is created. This allows to
239  check out and build older Haiku revisions with their corresponding HaikuPorts
240  packages.
241
242The repositories are maintained via files in the Haiku git repository. For each
243architecture and each repository the Haiku git repository contains a file
244listing the packages for that repository. For the HaikuPorts repositories the
245packages are listed with the respective version. For the Haiku repositories the
246version is implied.
247
248Whenever a developer wants to update or add a HaikuPorts package, the new
249package file has to be uploaded to their git.haiku-os.org account and the
250package list file for the repository has to be adjusted accordingly.
251``jam upload-packages <packages-list>`` can be used in order to upload the
252package(s) or the packages could just be ``scp``'ed into the 'hpkg-upload'
253folder in the developer home directory on git.haiku-os.org. When that is done,
254the change can be pushed to git.haiku-os.org, where a push hook will analyze the
255change, move the new package file(s) from the developer's account to the
256repository directory, and build a new repository snapshot. If a package file is
257missing or broken, the push will be rejected with a message notifying the
258developer about the problem.
259
260The creation and update of repositories for official releases has to be
261triggered explicitly on the server. In either case the Haiku repository is built
262by the build service.
263
264The Package Kit
265===============
266The package kit provides an API for all package management related tasks,
267including:
268
269- Reading and writing HPKG and HPKR files.
270- Adding, removing, and querying software repositories.
271- Solving package dependencies.
272- Adding, removing, and updating packages.
273
274Localization
275============
276Package files and repository index files contain text strings -- e.g. the
277package short and long description -- that are presented to the user. Therefore
278translations for these strings must be available. Since it is impractical to
279include the translations in the package and repository index files, they must be
280provided in separate files. How exactly has not been decided on yet.
281