Debian Packaging: From Source to Repository
Rules and debhelper
A practical Debian packaging course lesson covering rules and debhelper.
Rules and debhelper
The debian/rules file tells Debian packaging tools how to build the package. Modern packages commonly use debhelper, which automates standard tasks such as preparing build directories, installing files, generating metadata, and assembling binary packages.
In this lesson, we will create a minimal rules file and understand how debhelper works.
Learning Objectives
- Understand the role of
debian/rules. - Use the debhelper sequencer.
- Understand the
dhcommand. - Add a custom build step for a Go application.
- Verify that the rules file is executable.
1. The Minimal debhelper Rules File
Open the file:
1
2
cd ~/packaging-lab/syspeek
nano debian/rules
For the first version, use:
1
2
3
4
#!/usr/bin/make -f
%:
dh $@
Make sure the command line is indented with a tab, not spaces.
Ensure the file is executable:
1
chmod +x debian/rules
The %: rule is a Make pattern rule. It forwards requested targets to dh.
2. What Does dh Do?
dh is the debhelper sequencer. It runs a sequence of helper commands for common package-building tasks.
Depending on the requested target and package configuration, the sequence may include steps such as:
- Cleaning generated files.
- Configuring the build.
- Compiling the application.
- Installing files into a temporary package directory.
- Generating dependency information.
- Creating the
.debarchive.
This avoids implementing the same packaging mechanics manually in every project.
3. Override a Build Step
Syspeek is a small Go application. We will use the Go compiler directly in a controlled build step.
Replace debian/rules with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/make -f
export DH_VERBOSE = 1
%:
dh $@
override_dh_auto_build:
CGO_ENABLED=0 go build -trimpath -buildvcs=false -o syspeek .
override_dh_auto_install:
install -D -m 0755 syspeek debian/syspeek/usr/bin/syspeek
override_dh_auto_clean:
rm -f syspeek
The override_dh_auto_build target replaces debhelper’s default build step with our Go build command.
CGO_ENABLED=0 asks Go to build without cgo. This is suitable for the standard-library-only version of Syspeek in this course.
-trimpath removes local filesystem paths from the compiled output, and -buildvcs=false avoids embedding local version-control metadata.
The install override places the executable at /usr/bin/syspeek inside the temporary package staging directory.
4. Understand the Staging Directory
Debian packages should not install files directly onto the host during a build.
Instead, debhelper stages files under a package-specific directory such as:
1
debian/syspeek/
Our command:
1
install -D -m 0755 syspeek debian/syspeek/usr/bin/syspeek
creates the destination directory and installs the executable with mode 0755.
The final package builder then archives this staged filesystem into the binary .deb.
5. Inspect the Rules File
Run:
1
cat debian/rules
Check its permissions:
1
stat -c '%A %n' debian/rules
The output should indicate that the file is executable.
Do not run the full package build yet if the changelog, control, or copyright files are incomplete. We will complete and build the package in the next lessons.
6. What We Learned
We created a debhelper-based rules file and added explicit Go build and installation steps. The build runs in the packaging workflow, while the executable is staged under debian/syspeek/ rather than installed onto the host.
Exercises
- Explain what
%:does in the rules file. - Find the debhelper documentation for
dh_auto_build. - Temporarily remove the executable bit from
debian/rulesand observe why packaging tools require it, then restore it. - Explain why installing directly into
/usr/binduring a package build would be wrong.
What’s Next?
Our packaging instructions are taking shape. Next, we will build the first .deb file and inspect the resulting package.