Skip to content

Instantly share code, notes, and snippets.

@joske
Last active July 1, 2025 08:57
Show Gist options
  • Select an option

  • Save joske/0175830e47c9d1ea1c87a461fb1f66f1 to your computer and use it in GitHub Desktop.

Select an option

Save joske/0175830e47c9d1ea1c87a461fb1f66f1 to your computer and use it in GitHub Desktop.
convert an Arch ALPM package to a debian package
#!/usr/bin/env bash
set -euo pipefail
ORIG=$(pwd)
TEMP=$(mktemp -d)
cleanup() {
local status=$?
cd "$ORIG" || true
rm -rf "$TEMP" || true
exit "$status"
}
trap cleanup EXIT INT TERM ERR
if [ $# -ne 1 ]; then
echo "usage: $0 <alpm package>" >&2
exit 1
fi
ALPM=$(realpath -- "$1")
if [ ! -f "$ALPM" ]; then
echo "$ALPM does not exist" >&2
exit 2
fi
tar -xf "$ALPM" -C "$TEMP"
cd "$TEMP"
NAME=$(sed -n 's/^pkgname = //p' .PKGINFO)
mkdir "$NAME"
find "$TEMP" -mindepth 1 -maxdepth 1 ! -name "$NAME" -exec mv -t "$TEMP/$NAME" -- {} +
cd "$NAME"
mkdir -p DEBIAN
NAME=$(sed -n 's/^pkgname = //p' .PKGINFO)
PKGVER=$(sed -n 's/^pkgver = //p' .PKGINFO)
PKGDESC=$(sed -n 's/^pkgdesc = //p' .PKGINFO)
ARCH_RAW=$(sed -n 's/^arch = //p' .PKGINFO)
MAINT=$(sed -n 's/^packager = //p' .PKGINFO)
case "$ARCH_RAW" in
x86_64) ARCH=amd64 ;;
aarch64) ARCH=arm64 ;;
*) ARCH="$ARCH_RAW" ;; # fall-back: keep original if unknown
esac
docdir="usr/share/doc/$NAME"
mkdir -p "$docdir"
# store the original metadata files in /usr/share/doc/$NAME
# if they are missing after installation, it's because of /etc/dpkg/dpkg.cfg.d/excludes
for src in .BUILDINFO .MTREE .PKGINFO .INSTALL; do
[ -f "$src" ] || continue
dst=${src#.} # strip off the .
mv -- "$src" "$docdir/$dst"
done
echo -e "$NAME ($PKGVER) unstable; urgency=low\n\n * ALPM→DEB auto-convert\n\n -- $MAINT $(date -R)" \
| gzip -n9 > "usr/share/doc/$NAME/changelog.Debian.gz"
echo "building $NAME"
cat > DEBIAN/control <<EOF
Package: $NAME
Version: $PKGVER
Description: $PKGDESC
Architecture: $ARCH
Maintainer: $MAINT
Section: misc
Priority: optional
EOF
cd ..
DEBFILE="${NAME}_${PKGVER}_${ARCH}.deb"
fakeroot dpkg-deb --build "$NAME" "$TEMP/$DEBFILE" >&2 > /dev/null
mv "$TEMP/$DEBFILE" "$ORIG"
cd "$ORIG"
echo "created $DEBFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment