Skip to content

Instantly share code, notes, and snippets.

@xi
Last active July 6, 2026 19:07
Show Gist options
  • Select an option

  • Save xi/6bc37c57498ec649b2775647b63bd9e0 to your computer and use it in GitHub Desktop.

Select an option

Save xi/6bc37c57498ec649b2775647b63bd9e0 to your computer and use it in GitHub Desktop.
makepkg for debian
#!/bin/bash -e
# This script is a wrapper around dpkg-deb that supports a subset of arch
# linux' PKGBUILD format. This means that it can be used to build debian
# (meta-)packages from a single file.
#
# PKGBUILD files are written in bash and can define the following variables:
#
# - pkgname [required]
# - pkgver
# - pkgdesc
# - maintainer
# - arch [required]
# - url
# - depends [array]
# - optdepends [array]
# - provides [array]
# - conflicts [array]
# - makedepends [array]
#
# In addition to that, the file can define the functions `package` for
# preparing and installing the files. It has access to the variables
# `$srcdir` which is the path to a temporary directory and `$pkgdir`
# which is the path to which the files must be installed.
#
# Inspired by the following tools:
#
# - http://checkinstall.izto.org/
# - https://github.com/hoffa/debpack
# - https://wiki.archlinux.org/index.php/Pkgbuild
if [ "$(id -u)" -eq 0 ]; then
echo "This script should not be run as root!"
exit 1
fi
install=
builddeps=
PKGBUILD='PKGBUILD'
while [ $# -gt 0 ]; do
case "$1" in
-i) install=true; shift;;
-m) builddeps=true; shift;;
-h) echo "Usage: makepkg [-i] [-m] [PKGBUILD]"; exit 1;;
*) PKGBUILD="$1"; shift;;
esac
done
join() {
local IFS=",";
echo "$*";
}
# define defaults
pkgver='0.0.0'
pkgdesc=''
maintainer="$USER <$USER@$HOSTNAME>"
arch='all'
url=''
depends=()
optdepends=()
provides=()
conflicts=()
package() {
true
}
. "$PKGBUILD"
if [ "$builddeps" = true ]; then
if [ -z "$makedepends" ]; then
echo "no build dependencies necessary" >&2
exit 1
fi
depends=()
optdepends=()
provides=()
conflicts=()
pkgname="$pkgname-deps"
arch='all'
depends=("${makedepends[@]}")
package() {
true
}
fi
srcdir="$(mktemp -d)"
pkgdir="$(mktemp -d)"
trap "rm -rf -- '$srcdir' '$pkgdir'" EXIT
(package)
# https://www.debian.org/doc/debian-policy/ch-controlfields.html
mkdir -p "$pkgdir/DEBIAN"
cat << EOF | grep ': .' > "$pkgdir/DEBIAN/control"
Package: $pkgname
Priority: optional
Section: checkinstall
Installed-Size: $(du -s "$pkgdir" | awk '{print $1}')
Maintainer: $maintainer
Architecture: $arch
Version: $pkgver
Depends: $(join "${depends[@]}")
Recommends: $(join "${optdepends[@]}")
Provides: $(join "${provides[@]}")
Conflicts: $(join "${conflicts[@]}")
Description: $pkgdesc
Homepage: $url
EOF
if ! find "$pkgdir" -type f | sed "s|^$pkgdir||" | grep '^/etc/' > "$pkgdir/DEBIAN/conffiles"; then
rm "$pkgdir/DEBIAN/conffiles"
fi
cat "$pkgdir/DEBIAN/control"
fakeroot dpkg-deb --build "$pkgdir" "${pkgname}_${pkgver}_${arch}.deb"
if [ "$install" = true ]; then
sudo dpkg -i "${pkgname}_${pkgver}_${arch}.deb"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment