*Disclaimer: I'm not a linux packaging expert, some parts could be considered as bad practises. Moreover, there are billions different ways of building packages. I am proposing only one in this article.*
RPM is a bit special, as it needs its own folder hierarchy (stored by default in `~/rpmbuild`) and can't build without this hierarchy. It's a bit annoying when you manage your packages in different repositories. We will see one way to get around this problem.
We will create one folder for each RPM and initialize with the essence of a RPM: a spec file. For now, let's start with only one, `chez-scheme` which is a real example:
* Sun May 20 2018 Quentin Dufour <quentin@dufour.io>
-
```
So, now we must fill this file. You can use the Fedora Packaging Guidelines guide to find help for the different macro (lines starting with a percent). You can also take some inspiration from existing `.spec` files like [meshlab.spec](https://src.fedoraproject.org/rpms/meshlab/blob/master/f/meshlab.spec) or [chez-scheme.spec](https://github.com/superboum/rpm/blob/master/chez-scheme/chez-scheme.spec).
By templating the link, it will be easier to upgrade the package.
To create the patches, you have different options. You can either clone the repository, do the modification in a specific branch or commit, then generate a patch:
```bash
git checkout -b v9.5-patches
git commit -m "A first patch"
git commit -m "A second patch"
git format-patch v9.5
```
Another way is to download and extract the tarball, create a copy and use diff:
Applying patches and extracting tarball can lead to some problems.
First, patches can have different "roots". We will use `patch -p0` to apply git patches as they don't include the main folder and `patch -p1` for diff patches as they include the main folder.
If you use the `autosetup` macro in your `.spec` file, you will put (for diff patches):
```
%autosetup -p1
```
Second, once downloaded, your tarball doesn't necessarily extract in a folder of the name `<package>-<version>`. If it's not the case, you must precise it too:
```
%autosetup -p1 -n ChezScheme-%{version}
```
Now, if we want to build the RPM, we will have to type the following commands:
There are some tools like rpkg, fedpkg or tito that can do most of the steps for you.
Unfortunately, they are not very flexible. rpkg and fedpkg never download the source directly, but from a mirror, and fails with a 404 as they don't find the file. If I want to push my source to this mirror, it fails with an unauthorized error. These packages seem to be aimed at distribution maintainers. tito, for its part, doesn't handle correctly patches and sources. It consider that everything should be embedded in a single git repository.
Fortunately, copr propose different solutions. One is to build your `.srpm` locally and send it to copr, but we can even avoid this step and only give a git repository to copr: we will build our own builder with a Makefile.
Now you just need to commit + push your repository and use the [Copr web interface](http://copr.fedorainfracloud.org/) to create a new project, add a package and trigger a build !
After that, your users can just enable the copr repository on their system and install your package:
```
sudo dnf copr enable superboum/chez-scheme
sudo dnf install chez-scheme
```
You can configure some webhooks to rebuild your packages for each git commit.