Refactor multi-distro Dockerfile and Makefile:

- Add dynamic BASE_IMAGE selection via ARG for Arch, Debian, Ubuntu, Fedora, CentOS
- Introduce conditional package installation and unified Nix installer logic
- Implement single-user Nix install without build-users-group for Debian/Ubuntu/Fedora/CentOS
- Add Nix environment preparation and PATH fixes
- Restore Arch-only makepkg build stage
- Extend Makefile with multi-distro build/test workflow
- Add base image mappings (arch/debian/ubuntu/fedora/centos)
- Add test runner logic for each distro, including Nix devShell fallback scanning
- Improve structure with clear section headers and comments

https://chatgpt.com/share/69360c94-ef04-800f-afd2-75bc6cd33d4d
This commit is contained in:
Kevin Veen-Birkenbach
2025-12-08 00:24:22 +01:00
parent 87b806d1b9
commit 0286cb44e0
2 changed files with 297 additions and 53 deletions

181
Makefile
View File

@@ -1,9 +1,31 @@
.PHONY: install setup uninstall aur_builder_setup test
.PHONY: install setup uninstall aur_builder_setup \
test build build-no-cache
# ------------------------------------------------------------
# Local Nix cache directories in the repo
# ------------------------------------------------------------
NIX_STORE_VOLUME := pkgmgr_nix_store
NIX_CACHE_VOLUME := pkgmgr_nix_cache
# ------------------------------------------------------------
# Distro list and base images
# ------------------------------------------------------------
DISTROS := arch #debian ubuntu fedora centos
BASE_IMAGE_arch := archlinux:latest
BASE_IMAGE_debian := debian:stable-slim
BASE_IMAGE_ubuntu := ubuntu:latest
BASE_IMAGE_fedora := fedora:latest
BASE_IMAGE_centos := quay.io/centos/centos:stream9
# Helper to echo which image is used for which distro (purely informational)
define echo_build_info
@echo "Building image for distro '$(1)' with base image '$(2)'..."
endef
# ------------------------------------------------------------
# PKGMGR setup (wrapper)
# ------------------------------------------------------------
setup: install
@echo "Running pkgmgr setup via main.py..."
@if [ -x "$$HOME/.venvs/pkgmgr/bin/python" ]; then \
@@ -14,44 +36,129 @@ setup: install
python3 main.py install; \
fi
# ------------------------------------------------------------
# Docker build targets: build all images
# ------------------------------------------------------------
build-no-cache:
@echo "Building test image 'package-manager-test' with no cache..."
docker build --no-cache -t package-manager-test .
@for distro in $(DISTROS); do \
case "$$distro" in \
arch) base_image="$(BASE_IMAGE_arch)" ;; \
debian) base_image="$(BASE_IMAGE_debian)" ;; \
ubuntu) base_image="$(BASE_IMAGE_ubuntu)" ;; \
fedora) base_image="$(BASE_IMAGE_fedora)" ;; \
centos) base_image="$(BASE_IMAGE_centos)" ;; \
*) echo "Unknown distro '$$distro'" >&2; exit 1 ;; \
esac; \
echo "Building test image 'package-manager-test-$$distro' with no cache (BASE_IMAGE=$$base_image)..."; \
docker build --no-cache \
--build-arg BASE_IMAGE="$$base_image" \
-t "package-manager-test-$$distro" . || exit $$?; \
done
build:
@echo "Building test image 'package-manager-test'..."
docker build -t package-manager-test .
@for distro in $(DISTROS); do \
case "$$distro" in \
arch) base_image="$(BASE_IMAGE_arch)" ;; \
debian) base_image="$(BASE_IMAGE_debian)" ;; \
ubuntu) base_image="$(BASE_IMAGE_ubuntu)" ;; \
fedora) base_image="$(BASE_IMAGE_fedora)" ;; \
centos) base_image="$(BASE_IMAGE_centos)" ;; \
*) echo "Unknown distro '$$distro'" >&2; exit 1 ;; \
esac; \
echo "Building test image 'package-manager-test-$$distro' (BASE_IMAGE=$$base_image)..."; \
docker build \
--build-arg BASE_IMAGE="$$base_image" \
-t "package-manager-test-$$distro" . || exit $$?; \
done
# ------------------------------------------------------------
# Test target: run tests in all three images
# ------------------------------------------------------------
test: build
@echo "Ensuring Docker Nix volumes exist (auto-created if missing)..."
@echo "Running tests inside Nix devShell with cached store..."
docker run --rm \
-v "$$(pwd):/src" \
-v "$(NIX_STORE_VOLUME):/nix" \
-v "$(NIX_CACHE_VOLUME):/root/.cache/nix" \
--workdir /src \
--entrypoint bash \
package-manager-test \
-c '\
set -e; \
echo "Remove existing Arch package-manager (if any)..."; \
pacman -Rns --noconfirm package-manager || true; \
echo "Rebuild Arch package from /src..."; \
rm -f /src/package-manager-*.pkg.tar.* || true; \
chown -R builder:builder /src; \
su builder -c "cd /src && makepkg -sf --noconfirm --clean"; \
pacman -U --noconfirm /src/package-manager-*.pkg.tar.*; \
echo "Run tests inside Nix devShell..."; \
git config --global --add safe.directory /src && \
cd /src && \
nix develop .#default --no-write-lock-file -c \
python3 -m unittest discover \
-s /src/tests \
-p "test_*.py" \
'
@echo "Running tests inside Nix devShell with cached store for all distros: $(DISTROS)"
@for distro in $(DISTROS); do \
echo "============================================================"; \
echo ">>> Running tests in container for distro: $$distro"; \
echo "============================================================"; \
docker run --rm \
-v "$$(pwd):/src" \
-v "$(NIX_STORE_VOLUME):/nix" \
-v "$(NIX_CACHE_VOLUME):/root/.cache/nix" \
--workdir /src \
--entrypoint bash \
"package-manager-test-$$distro" \
-c '\
set -e; \
if [ -f /etc/os-release ]; then . /etc/os-release; fi; \
echo "Detected container distro: $${ID:-unknown} (like: $${ID_LIKE:-})"; \
\
# Arch-only: rebuild Arch package inside the container \
if [ "$${ID}" = "arch" ]; then \
echo "Remove existing Arch package-manager (if any)..."; \
pacman -Rns --noconfirm package-manager || true; \
echo "Rebuild Arch package from /src..."; \
rm -f /src/package-manager-*.pkg.tar.* || true; \
chown -R builder:builder /src; \
su builder -c "cd /src && makepkg -sf --noconfirm --clean"; \
pacman -U --noconfirm /src/package-manager-*.pkg.tar.*; \
else \
echo "Non-Arch distro skipping Arch package rebuild."; \
fi; \
\
echo "Preparing Nix environment..."; \
# Try to source typical Nix profile scripts (if they exist) \
if [ -f "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" ]; then \
. "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh"; \
fi; \
if [ -f "$$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then \
. "$$HOME/.nix-profile/etc/profile.d/nix.sh"; \
fi; \
# Hard-extend PATH for common Nix locations \
PATH="/nix/var/nix/profiles/default/bin:$$HOME/.nix-profile/bin:$$PATH"; \
export PATH; \
echo "PATH is now:"; \
echo "$$PATH"; \
\
# Determine which Nix binary to use \
NIX_CMD=""; \
if command -v nix >/dev/null 2>&1; then \
echo "Found nix on PATH:"; \
command -v nix; \
NIX_CMD="nix"; \
else \
echo "nix not found on PATH, scanning /nix/store for a nix binary..."; \
for path in /nix/store/*-nix-*/bin/nix; do \
if [ -x "$$path" ]; then \
echo "Found nix binary at $$path"; \
NIX_CMD="$$path"; \
break; \
fi; \
done; \
fi; \
\
if [ -z "$$NIX_CMD" ]; then \
echo "ERROR: nix binary not found anywhere cannot run devShell"; \
exit 1; \
fi; \
\
echo "Using Nix command: $$NIX_CMD"; \
echo "Run tests inside Nix devShell..."; \
git config --global --add safe.directory /src; \
cd /src; \
"$$NIX_CMD" develop .#default --no-write-lock-file -c \
python3 -m unittest discover \
-s /src/tests \
-p "test_*.py"; \
' || exit $$?; \
done
# ------------------------------------------------------------
# Installer for host systems (your original logic)
# ------------------------------------------------------------
install:
@if [ -n "$$IN_NIX_SHELL" ]; then \
echo "Nix shell detected (IN_NIX_SHELL=1). Skipping venv/pip install handled by Nix flake."; \
@@ -93,18 +200,17 @@ install:
echo "Installation complete. Please restart your shell (or 'exec bash' or 'exec zsh') for the changes to take effect."; \
fi
# Only runs on Arch/Manjaro
# ------------------------------------------------------------
# AUR builder setup — only on Arch/Manjaro
# ------------------------------------------------------------
aur_builder_setup:
@echo "Setting up aur_builder and yay (Arch/Manjaro)..."
@sudo pacman -Syu --noconfirm
@sudo pacman -S --needed --noconfirm base-devel git sudo
@# group & user
@if ! getent group aur_builder >/dev/null; then sudo groupadd -r aur_builder; fi
@if ! id -u aur_builder >/dev/null 2>&1; then sudo useradd -m -r -g aur_builder -s /bin/bash aur_builder; fi
@# sudoers rule for pacman
@echo '%aur_builder ALL=(ALL) NOPASSWD: /usr/bin/pacman' | sudo tee /etc/sudoers.d/aur_builder >/dev/null
@sudo chmod 0440 /etc/sudoers.d/aur_builder
@# yay install (if missing)
@if ! sudo -u aur_builder bash -lc 'command -v yay >/dev/null'; then \
sudo -u aur_builder bash -lc 'cd ~ && rm -rf yay && git clone https://aur.archlinux.org/yay.git && cd yay && makepkg -si --noconfirm'; \
else \
@@ -112,6 +218,9 @@ aur_builder_setup:
fi
@echo "aur_builder/yay setup complete."
# ------------------------------------------------------------
# Uninstall target
# ------------------------------------------------------------
uninstall:
@echo "Removing global user virtual environment if it exists..."
@rm -rf "$$HOME/.venvs/pkgmgr"