Refactor Docker-based packaging and delegate Nix installation to system hooks (see ChatGPT conversation: https://chatgpt.com/share/6936db94-ff30-800f-9b50-86a4b45c44bf)

This commit is contained in:
Kevin Veen-Birkenbach
2025-12-08 15:08:42 +01:00
parent ef23e14ae4
commit a5aaacc8d0
4 changed files with 153 additions and 102 deletions

View File

@@ -5,150 +5,175 @@ ARG BASE_IMAGE=archlinux:latest
FROM ${BASE_IMAGE} FROM ${BASE_IMAGE}
# ------------------------------------------------------------ # ------------------------------------------------------------
# System base + conditional package installation # System base + conditional package tool installation
#
# Important:
# - We do NOT install Nix directly here via curl.
# - Nix is installed/initialized by init-nix.sh, which is invoked
# from the system packaging hooks (Arch .install, Debian postinst,
# RPM %post).
# ------------------------------------------------------------ # ------------------------------------------------------------
RUN set -e; \ RUN set -e; \
if [ -f /etc/os-release ]; then . /etc/os-release; else echo "No /etc/os-release found" && exit 1; fi; \ if [ -f /etc/os-release ]; then . /etc/os-release; else echo "No /etc/os-release found" && exit 1; fi; \
echo "Detected base image: ${ID:-unknown} (like: ${ID_LIKE:-})"; \ echo "Detected base image: ${ID:-unknown} (like: ${ID_LIKE:-})"; \
\ \
# --------------------------------------------------------
# Archlinux: Nix via pacman
# --------------------------------------------------------
if [ "$ID" = "arch" ]; then \ if [ "$ID" = "arch" ]; then \
pacman -Syu --noconfirm && \ pacman -Syu --noconfirm && \
pacman -S --noconfirm --needed \ pacman -S --noconfirm --needed \
base-devel \ base-devel \
git \ git \
nix \
rsync \ rsync \
python && \ curl \
ca-certificates \
xz && \
pacman -Scc --noconfirm; \ pacman -Scc --noconfirm; \
\
# --------------------------------------------------------
# Debian: Nix installer (single-user, root, no build-users-group)
# --------------------------------------------------------
elif [ "$ID" = "debian" ]; then \ elif [ "$ID" = "debian" ]; then \
apt-get update && \ apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
ca-certificates \ build-essential \
curl \ debhelper \
dpkg-dev \
git \ git \
python3 \
python3-venv \
rsync \ rsync \
bash \ bash \
curl \
ca-certificates \
xz-utils && \ xz-utils && \
rm -rf /var/lib/apt/lists/* && \ rm -rf /var/lib/apt/lists/*; \
echo "Preparing /nix + /etc/nix/nix.conf on Debian..." && \
mkdir -p /nix && chmod 0755 /nix && chown root:root /nix && \
mkdir -p /etc/nix && printf 'build-users-group =\n' > /etc/nix/nix.conf && \
echo "Downloading Nix installer on Debian..." && \
curl -L https://nixos.org/nix/install -o /tmp/nix-install && \
echo "Installing Nix on Debian (single-user, as root, no build-users-group)..." && \
HOME=/root NIX_INSTALLER_NO_MODIFY_PROFILE=1 sh /tmp/nix-install --no-daemon && \
rm -f /tmp/nix-install; \
\
# --------------------------------------------------------
# Ubuntu: Nix installer (single-user, root, no build-users-group)
# --------------------------------------------------------
elif [ "$ID" = "ubuntu" ]; then \ elif [ "$ID" = "ubuntu" ]; then \
apt-get update && \ apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
ca-certificates \ build-essential \
curl \ debhelper \
dpkg-dev \
git \ git \
tzdata \ tzdata \
lsb-release \ lsb-release \
python3 \
python3-venv \
rsync \ rsync \
bash \ bash \
curl \
ca-certificates \
xz-utils && \ xz-utils && \
rm -rf /var/lib/apt/lists/* && \ rm -rf /var/lib/apt/lists/*; \
echo "Preparing /nix + /etc/nix/nix.conf on Ubuntu..." && \
mkdir -p /nix && chmod 0755 /nix && chown root:root /nix && \
mkdir -p /etc/nix && printf 'build-users-group =\n' > /etc/nix/nix.conf && \
echo "Downloading Nix installer on Ubuntu..." && \
curl -L https://nixos.org/nix/install -o /tmp/nix-install && \
echo "Installing Nix on Ubuntu (single-user, as root, no build-users-group)..." && \
HOME=/root NIX_INSTALLER_NO_MODIFY_PROFILE=1 sh /tmp/nix-install --no-daemon && \
rm -f /tmp/nix-install; \
\
# --------------------------------------------------------
# Fedora: Nix installer (single-user, root, no build-users-group)
# --------------------------------------------------------
elif [ "$ID" = "fedora" ]; then \ elif [ "$ID" = "fedora" ]; then \
dnf -y update && \ dnf -y update && \
dnf -y install \ dnf -y install \
ca-certificates \
curl \
git \ git \
python3 \
rsync \ rsync \
rpm-build \
make \
gcc \
bash \ bash \
curl \
ca-certificates \
xz && \ xz && \
dnf clean all && \ dnf clean all; \
echo "Preparing /nix + /etc/nix/nix.conf on Fedora..." && \
mkdir -p /nix && chmod 0755 /nix && chown root:root /nix && \
mkdir -p /etc/nix && printf 'build-users-group =\n' > /etc/nix/nix.conf && \
echo "Downloading Nix installer on Fedora..." && \
curl -L https://nixos.org/nix/install -o /tmp/nix-install && \
echo "Installing Nix on Fedora (single-user, as root, no build-users-group)..." && \
HOME=/root NIX_INSTALLER_NO_MODIFY_PROFILE=1 sh /tmp/nix-install --no-daemon && \
rm -f /tmp/nix-install; \
\
# --------------------------------------------------------
# CentOS Stream: Nix installer (single-user, root, no build-users-group)
# --------------------------------------------------------
elif [ "$ID" = "centos" ]; then \ elif [ "$ID" = "centos" ]; then \
dnf -y update && \ dnf -y update && \
dnf -y install \ dnf -y install \
ca-certificates \
curl-minimal \
git \ git \
python3 \
rsync \ rsync \
rpm-build \
make \
gcc \
bash \ bash \
curl-minimal \
ca-certificates \
xz && \ xz && \
dnf clean all && \ dnf clean all; \
echo "Preparing /nix + /etc/nix/nix.conf on CentOS..." && \
mkdir -p /nix && chmod 0755 /nix && chown root:root /nix && \
mkdir -p /etc/nix && printf 'build-users-group =\n' > /etc/nix/nix.conf && \
echo "Downloading Nix installer on CentOS..." && \
curl -L https://nixos.org/nix/install -o /tmp/nix-install && \
echo "Installing Nix on CentOS (single-user, as root, no build-users-group)..." && \
HOME=/root NIX_INSTALLER_NO_MODIFY_PROFILE=1 sh /tmp/nix-install --no-daemon && \
rm -f /tmp/nix-install; \
else \ else \
echo "Unsupported base image: ${ID}" && exit 1; \ echo "Unsupported base image: ${ID}" && exit 1; \
fi fi
# ------------------------------------------------------------ # ------------------------------------------------------------
# Nix environment defaults # Nix environment defaults
#
# Nix itself is installed by your system packages (via init-nix.sh).
# Here we only define default configuration options.
# ------------------------------------------------------------ # ------------------------------------------------------------
ENV NIX_CONFIG="experimental-features = nix-command flakes" ENV NIX_CONFIG="experimental-features = nix-command flakes"
ENV PATH="/root/.nix-profile/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# ------------------------------------------------------------ # ------------------------------------------------------------
# Unprivileged user for building Arch packages # Unprivileged user for Arch package build (makepkg)
# ------------------------------------------------------------ # ------------------------------------------------------------
RUN useradd -m builder RUN useradd -m builder || true
# ------------------------------------------------------------ # ------------------------------------------------------------
# Build stage (for Arch) — optional, installs package-manager inside image # Build and install distro-native package-manager package
#
# - Arch: PKGBUILD -> pacman -U
# - Debian: debhelper -> dpkg-buildpackage -> apt install ./package-manager_*.deb
# - Ubuntu: same as Debian
# - Fedora: rpmbuild -> dnf/dnf5/yum install package-manager-*.rpm
# - CentOS: rpmbuild -> dnf/yum install package-manager-*.rpm
#
# Nix is NOT manually installed here; it is handled by init-nix.sh.
# ------------------------------------------------------------ # ------------------------------------------------------------
WORKDIR /build WORKDIR /build
COPY . . COPY . .
RUN set -e; \ RUN set -e; \
if [ -f /etc/os-release ]; then . /etc/os-release; fi; \ . /etc/os-release; \
if [ "$ID" = "arch" ]; then \ if [ "$ID" = "arch" ]; then \
echo "Running Arch build stage (makepkg)..."; \ echo 'Building Arch package (makepkg --nodeps)...'; \
chown -R builder:builder /build && \ chown -R builder:builder /build; \
su builder -c "cd /build && rm -f package-manager-*.pkg.tar.* && makepkg -sf --noconfirm --clean"; \ su builder -c "cd /build && rm -f package-manager-*.pkg.tar.* && makepkg --noconfirm --clean --nodeps"; \
\
echo 'Installing generated Arch package...'; \
pacman -U --noconfirm package-manager-*.pkg.tar.*; \ pacman -U --noconfirm package-manager-*.pkg.tar.*; \
elif [ "$ID" = "debian" ] || [ "$ID" = "ubuntu" ]; then \
echo 'Building Debian/Ubuntu package...'; \
dpkg-buildpackage -us -uc -b; \
\
echo 'Installing generated DEB package...'; \
apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y ./../package-manager_*.deb && \
rm -rf /var/lib/apt/lists/*; \
elif [ "$ID" = "fedora" ] || [ "$ID" = "centos" ]; then \
echo 'Setting up rpmbuild dirs...'; \
mkdir -p /root/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}; \
\
echo "Extracting version from package-manager.spec..."; \
version=$(grep -E '^Version:' /build/package-manager.spec | awk '{print $2}'); \
if [ -z "$version" ]; then echo 'ERROR: Version missing!' && exit 1; fi; \
srcdir="package-manager-${version}"; \
\
echo "Preparing source tree for RPM: $srcdir"; \
rm -rf "/tmp/$srcdir"; \
mkdir -p "/tmp/$srcdir"; \
cp -a /build/. "/tmp/$srcdir/"; \
\
echo "Creating source tarball: /root/rpmbuild/SOURCES/$srcdir.tar.gz"; \
tar czf "/root/rpmbuild/SOURCES/$srcdir.tar.gz" -C /tmp "$srcdir"; \
\
echo 'Copying SPEC...'; \
cp /build/package-manager.spec /root/rpmbuild/SPECS/; \
\
echo 'Running rpmbuild...'; \
cd /root/rpmbuild/SPECS && rpmbuild -bb package-manager.spec; \
\
echo 'Installing generated RPM...'; \
rpm_path=$(find /root/rpmbuild/RPMS -name "package-manager-*.rpm" | head -n1); \
if [ -z "$rpm_path" ]; then echo 'ERROR: RPM not found!' && exit 1; fi; \
\
if command -v dnf5 >/dev/null 2>&1; then \
echo 'Using dnf5 to install local RPM...'; \
dnf5 install -y "$rpm_path"; \
elif command -v dnf >/dev/null 2>&1; then \
echo 'Using dnf to install local RPM...'; \
dnf install -y "$rpm_path"; \
elif command -v yum >/dev/null 2>&1; then \
echo 'Using yum to install local RPM...'; \
yum localinstall -y "$rpm_path"; \
else \
echo 'No dnf/dnf5/yum found, falling back to rpm -i (no deps)...'; \
rpm -i "$rpm_path"; \
fi; \
\
rm -rf "/tmp/$srcdir"; \
else \ else \
echo "Non-Arch base detected — skipping Arch package build."; \ echo "Unsupported distro: ${ID}"; \
exit 1; \
fi; \ fi; \
rm -rf /build rm -rf /build

View File

@@ -82,10 +82,10 @@ build-arch:
# Test targets # Test targets
# ------------------------------------------------------------ # ------------------------------------------------------------
# Unit tests: only in Arch container (fastest feedback) # Unit tests: only in Arch container (fastest feedback), via Nix devShell
test-unit: build-arch test-unit: build-arch
@echo "============================================================" @echo "============================================================"
@echo ">>> Running UNIT tests in Arch container" @echo ">>> Running UNIT tests in Arch container (via Nix devShell)"
@echo "============================================================" @echo "============================================================"
docker run --rm \ docker run --rm \
-v "$$(pwd):/src" \ -v "$$(pwd):/src" \
@@ -96,20 +96,20 @@ test-unit: build-arch
set -e; \ set -e; \
if [ -f /etc/os-release ]; then . /etc/os-release; fi; \ if [ -f /etc/os-release ]; then . /etc/os-release; fi; \
echo "Detected container distro: $${ID:-unknown} (like: $${ID_LIKE:-})"; \ echo "Detected container distro: $${ID:-unknown} (like: $${ID_LIKE:-})"; \
echo "Running Python unit tests (tests/unit)..."; \ echo "Running Python unit tests (tests/unit) via nix develop..."; \
git config --global --add safe.directory /src || true; \ git config --global --add safe.directory /src || true; \
cd /src; \ cd /src; \
export PYTHONPATH=/src:$${PYTHONPATH}; \ nix develop .#default --no-write-lock-file -c \
python -m unittest discover \ python -m unittest discover \
-s tests/unit \ -s tests/unit \
-t /src \ -t /src \
-p "test_*.py"; \ -p "test_*.py"; \
' '
# Integration tests: also in Arch container, but using tests/integration # Integration tests: also in Arch container, via Nix devShell
test-integration: build test-integration: build-arch
@echo "============================================================" @echo "============================================================"
@echo ">>> Running INTEGRATION tests in Arch container" @echo ">>> Running INTEGRATION tests in Arch container (via Nix devShell)"
@echo "============================================================" @echo "============================================================"
docker run --rm \ docker run --rm \
-v "$$(pwd):/src" \ -v "$$(pwd):/src" \
@@ -120,14 +120,14 @@ test-integration: build
set -e; \ set -e; \
if [ -f /etc/os-release ]; then . /etc/os-release; fi; \ if [ -f /etc/os-release ]; then . /etc/os-release; fi; \
echo "Detected container distro: $${ID:-unknown} (like: $${ID_LIKE:-})"; \ echo "Detected container distro: $${ID:-unknown} (like: $${ID_LIKE:-})"; \
echo "Running Python integration tests (tests/integration)..."; \ echo "Running Python integration tests (tests/integration) via nix develop..."; \
git config --global --add safe.directory /src || true; \ git config --global --add safe.directory /src || true; \
cd /src; \ cd /src; \
export PYTHONPATH=/src:$${PYTHONPATH}; \ nix develop .#default --no-write-lock-file -c \
python -m unittest discover \ python -m unittest discover \
-s tests/integration \ -s tests/integration \
-t /src \ -t /src \
-p "test_*.py"; \ -p "test_*.py"; \
' '
# End-to-end tests: run in all distros via Nix devShell (tests/e2e) # End-to-end tests: run in all distros via Nix devShell (tests/e2e)

19
debian/rules vendored
View File

@@ -3,6 +3,25 @@
%: %:
dh $@ dh $@
# ---------------------------------------------------------------------------
# Disable automatic build + tests for Debian package
#
# The package is effectively a wrapper around the project source and the
# Nix flake. We do not need dh_auto_build to invoke the top-level Makefile,
# because that Makefile is meant for developer workflows (venv, setup, etc.)
# and would fail in the minimal build environment.
# ---------------------------------------------------------------------------
override_dh_auto_build:
# No build step required for this package; skip calling the top-level Makefile.
:
override_dh_auto_test:
# We do not run tests as part of the Debian package build.
:
# ---------------------------------------------------------------------------
# Install phase: copy wrapper + init script + full project source
# ---------------------------------------------------------------------------
override_dh_auto_install: override_dh_auto_install:
# Create target directories # Create target directories
install -d debian/package-manager/usr/bin install -d debian/package-manager/usr/bin

View File

@@ -8,7 +8,12 @@ URL: https://github.com/kevinveenbirkenbach/package-manager
Source0: %{name}-%{version}.tar.gz Source0: %{name}-%{version}.tar.gz
BuildArch: noarch BuildArch: noarch
Requires: nix
# NOTE:
# Nix is a runtime requirement, but it is *not* declared here as a hard
# RPM dependency, because many distributions do not ship a "nix" RPM.
# Instead, Nix is installed and initialized by init-nix.sh, which is
# called in the %post scriptlet below.
%description %description
This package provides the `pkgmgr` command, which runs Kevin's package This package provides the `pkgmgr` command, which runs Kevin's package
@@ -16,14 +21,15 @@ manager via a local Nix flake:
nix run /usr/lib/package-manager#pkgmgr -- ... nix run /usr/lib/package-manager#pkgmgr -- ...
Nix is the only runtime dependency and must be initialized on the Nix is a runtime requirement and is installed/initialized by the
system to work correctly. init-nix.sh helper during package installation if it is not yet
available on the system.
%prep %prep
%setup -q %setup -q
%build %build
# No build step required # No build step required; we ship the project tree as-is.
: :
%install %install
@@ -53,6 +59,7 @@ rm -rf \
%{buildroot}%{_libdir}/package-manager/.gitkeep || true %{buildroot}%{_libdir}/package-manager/.gitkeep || true
%post %post
# Initialize Nix (if needed) after installing the package-manager files.
if [ -x %{_libdir}/package-manager/init-nix.sh ]; then if [ -x %{_libdir}/package-manager/init-nix.sh ]; then
%{_libdir}/package-manager/init-nix.sh || true %{_libdir}/package-manager/init-nix.sh || true
else else