Package management is the process of installing, updating and removing software, tools and libraries.
On GNU/Linux systems, package management is usually done with a package manager. 

# MatterLinux Package Manager 
In a Matter system, package management is generally done with the MatterLinux Package Manager (`mp`).
`mp` lets you install, remove, update and search different MatterLinux packages across different repos.

### Installation
`mp` should be already installed on a MatterLinux system. However if you want to install `mp` from
the source, then you should follow [this section](https://git.matterlinux.xyz/matter/mp#installation) 
from the README.

> **Note** 
>
> You should run all the `mp` and related commands as the root user.

### Configuration
Configuration file for `mp` can be found at `/etc/mp/cfg`. This file is used specify repos and 
configure general settings.

Here is the configuration shipped with the MatterLinux 24 releases:
```
[general]
tmpdir = /tmp/mp

[base]
uri = https://pkgs.matterlinux.xyz/base

[desktop]
uri = https://pkgs.matterlinux.xyz/desktop
```
Lets start by breaking down the `[general]` section:

- `tmpdir (path)`: Specifies the temporary directory used by the `mp`. This directory will be used to save 
packages and signatures, most of the files in this directory will be removed after operation.

Other general configuration options are:

- `debug (yes/no)`: This setting is used to enable the debug logging, however `mp` don't have any 
debug logging (it will be implemented in the future) so there is no point at enabling it.

Next sections specify the repos. Section name is the repo name, and it **should match with the 
actual repo name**. Repo options are:

- `uri (URI)`: Repo URI, you can use `http`, `https` or `ftp` protocols.
- `signing (yes/no)`: Enable/disable PGP signature verification for the repo, by default it's enabled.

> **Important** 
>
> Do NOT disable signature verification if you don't know what are you doing! There is no 
> way to confirm the legitimacy of the packages without signature verification!

### Environment options 
`mp` also has a few other options that you specify as an environment variable.

One of these is the `MP_ROOT` option. This option lets you change the target root directory. 
By default `mp` install all the packages to `/`, stores all the information under the `/` etc. 
By changing the target root directory you can change this.

For example lets say you want to install all the packages to `/tmp/testroot`. To do so, 
first you will need to setup a root file system structure inside `/tmp/testroot`:
```
# mkdir /tmp/testroot
# pushd /tmp/testroot
# mkdir -p etc boot dev home proc root run srv sys tmp usr/lib usr/bin usr/sbin var/lib
# ln -sf bin usr/bin
# ln -sf sbin usr/sbin
# ln -sf lib usr/lib
# ln -sf lib64 usr/lib
# popd
```
Now you can use `mp` with the `MP_ROOT` variable, for example:
```
# MP_ROOT=/tmp/testroot mp-sync
# MP_ROOT=/tmp/testroot mp-install which 
```

Another one of these environment options is the `MP_YES` option. By default operations such as 
installing packages, updating packages and removing packages will ask you to confirm the operation 
using an interactive yes/no prompt. To disable this prompt and confirm all operations by default,
you can set the `MP_YES` environment variable. You can set it to whatever you want, just make sure 
it's set.

`MP_NORET` is another option that you can set. By default `mp` will print the return status for each 
command, you can disable this by setting `MP_NORET` to whatever you want.

The last one of these options is the `MP_GREP` option. This option is only useful if you are using 
the query or the list command. If set, `mp` will print grep-able output for these commands. Again, you can set it 
to whatever you want.
 
### Syncing Repos
To sync remote repo information and package lists, you can use the `mp-sync` command. After running it 
`mp` will:

1. Download the repo metadata for all the repos. Repo metadata is located at `REPO_URI/repo`
2. Parse the repo metadata and download all the repo package lists. These lists are located at 
`REPO_URI/REPO_NAME.tar.gz`
3. Move and extract the repo lists to `$MP_ROOT/var/lib/mp/repos/REPO_NAME` and parse the plain package lists (`pkgs`)
4. Tell you the repo signatures 

You can add these signatures to your public key ring with `gpg`: 
```
# gpg --receive-keys [fingerprint]
```

### Installing Packages 
To install a package, you can use the `mp-install` command:
```
# mp-install which 
```
After running this command, `mp` will:

1. Check all the synced repos for the `which` package 
2. Check if the package is already installed
3. Resolve all the dependencies for the `which` package
4. Ask you if you want to continue with the installation
5. Download the `which` package and it's signature from the repo to the `tmpdir`
6. Verify the package using the signature
7. Extract the package to `$MP_ROOT`
8. Run the package install script if it contains one 
9. Add an entry for the `which` package to `$MP_ROOT/var/lib/mp/db` and `$MP_ROOT/var/lib/mp/files`

You can also install multiple packages using the `mp-install` command:
```
# mp-install grub systemd linux
```

### Removing/Uninstalling Packages
To remove an installed package, you can use the `mp-remove` command:
```
# mp-remove which
```

After running this command, `mp` will:

1. Check all the installed packages to see if `which` is installed or not
2. Find all the files that `which` package extracted during the installation 
3. Ask you if you want to continue with the removal
4. Delete all the files found in the step 2
5. Remove the package entry for the `which` package from `$MP_ROOT/var/lib/mp/db` and `$MP_ROOT/var/lib/mp/files`

You can also remove multiple packages:
```
# mp-remove git curl
```

### Updating Packages
You can update all of the installed packages using the `mp-update` command.
After running this command, `mp` will:

1. Check if all the installed packages are up-to-date 
2. Find all the packages that are not up-to-date
3. Ask if you want to continue with the update
4. Act like the `mp-install` command, it will download, verify and install all 
the packages, overwriting old files 
5. Save new up-to-date package entries to the `$MP_ROOT/var/lib/mp/db` and `$MP_ROOT/var/lib/mp/files`,
overwriting the old entries.

### Searching Packages
You can search for packages in the repos using the `mp-search` command.
For example, this command will return a list of all the packages that contain `lib`
in their name:
```
# mp-search lib 
```
After running this command, `mp` will:

1. Check all the repos for any packages that contain `lib` in the name
2. Print the package 
3. Print the count of the results

### Querying packages
You can get more information about a package by querying it. For example to get 
more information about the `which` package:
```
# mp-query which
```
This will provide the following information about the package:

- Name of the package
- Repo that the package is in 
- Description
- Hash (sum)
- Version
- If the package is installed
- If the package is up-to-date

After running this command, `mp` will:

1. Check all the repos for the `which` package
2. Fetch information about the package from the `$MP_ROOT/var/lib/mp/repos/REPO_NAME` and `$MP_ROOT/var/lib/mp/db`. 

### List packages
You can list all the installed packages with the `mp-list` command.

After running this command, `mp` will:

1. Load the database (`$MP_ROOT/var/lib/mp/db`)
2. Loop over all the entries and print the package names and the versions


# Other Package Managers
It's a really bad idea to use multiple package managers as they may conflict and may result in an 
unstable system. However package managers such as flatpak and snap offer containerized packages,
making them possible to use with other package managers.

- Flatpak will be added to `desktop` repo in the future. Currently the only way to use flatpak is to 
install it from the source.
- Snap will never be added to any official repo, so only way to use it is to install it from the source.
