Use pyproject-based Nix flake build and fix install logic for pkgmgr
- Add pyproject.toml to define package-manager as a Python application
- Declare setuptools build backend (setuptools.build_meta) and wheel
- Expose CLI entrypoint via [project.scripts] as `pkgmgr = pkgmgr.cli:main`
- Define PyYAML as a base runtime dependency
- Update flake.nix to build pkgmgr via python311Packages.buildPythonApplication
- Use format = "pyproject" and src = ./. (current git checkout)
- Add setuptools and wheel to nativeBuildInputs for the backend
- Add pyyaml to propagatedBuildInputs to match pyproject dependencies
- Provide a devShell that includes the built pkgmgr, git, and Ansible
- Expose `nix run .#pkgmgr` and `nix run .#default` as flake apps
- Fix Makefile install target for Nix shells
- Treat install as a no-op when IN_NIX_SHELL is set (skip venv / pip)
- In non-Nix environments, create ~/.venvs/pkgmgr, install deps, and
wire automatic activation into shell rc files
- Keep Arch/Manjaro-specific aur_builder/yay setup behind pacman check
- Adjust PKGBUILD prepare() to correctly copy the full project tree
- Stop excluding the top-level src directory from rsync
- Still exclude .git, .github, pkg, and srcpkg
This unifies the installation workflow across Arch, Nix, and local venvs,
and ensures pkgmgr builds cleanly inside the Docker-based Nix devShell tests.
Reference: ChatGPT-assisted refactor & debugging session on 2025-12-07.
https://chatgpt.com/share/6935ee1f-6c0c-800f-bb32-434c4051bd1e
This commit is contained in:
48
Makefile
48
Makefile
@@ -55,43 +55,43 @@ test: build
|
|||||||
install:
|
install:
|
||||||
@if [ -n "$$IN_NIX_SHELL" ]; then \
|
@if [ -n "$$IN_NIX_SHELL" ]; then \
|
||||||
echo "Nix shell detected (IN_NIX_SHELL=1). Skipping venv/pip install – handled by Nix flake."; \
|
echo "Nix shell detected (IN_NIX_SHELL=1). Skipping venv/pip install – handled by Nix flake."; \
|
||||||
exit 0; \
|
else \
|
||||||
fi
|
echo "Making 'main.py' executable..."; \
|
||||||
@echo "Making 'main.py' executable..."
|
chmod +x main.py; \
|
||||||
@chmod +x main.py
|
echo "Checking if global user virtual environment exists..."; \
|
||||||
@echo "Checking if global user virtual environment exists..."
|
mkdir -p "$$HOME/.venvs"; \
|
||||||
@mkdir -p "$$HOME/.venvs"
|
if [ ! -d "$$HOME/.venvs/pkgmgr" ]; then \
|
||||||
@if [ ! -d "$$HOME/.venvs/pkgmgr" ]; then \
|
|
||||||
echo "Creating global venv at $$HOME/.venvs/pkgmgr..."; \
|
echo "Creating global venv at $$HOME/.venvs/pkgmgr..."; \
|
||||||
python3 -m venv "$$HOME/.venvs/pkgmgr"; \
|
python3 -m venv "$$HOME/.venvs/pkgmgr"; \
|
||||||
fi
|
fi; \
|
||||||
@echo "Installing required Python packages into $$HOME/.venvs/pkgmgr..."
|
echo "Installing required Python packages into $$HOME/.venvs/pkgmgr..."; \
|
||||||
@$$HOME/.venvs/pkgmgr/bin/python -m ensurepip --upgrade
|
"$$HOME/.venvs/pkgmgr/bin/python" -m ensurepip --upgrade; \
|
||||||
@$$HOME/.venvs/pkgmgr/bin/pip install --upgrade pip setuptools wheel
|
"$$HOME/.venvs/pkgmgr/bin/pip" install --upgrade pip setuptools wheel; \
|
||||||
@echo "Looking for requirements.txt / _requirements.txt..."
|
echo "Looking for requirements.txt / _requirements.txt..."; \
|
||||||
@if [ -f requirements.txt ]; then \
|
if [ -f requirements.txt ]; then \
|
||||||
echo "Installing Python packages from requirements.txt..."; \
|
echo "Installing Python packages from requirements.txt..."; \
|
||||||
$$HOME/.venvs/pkgmgr/bin/pip install -r requirements.txt; \
|
"$$HOME/.venvs/pkgmgr/bin/pip" install -r requirements.txt; \
|
||||||
elif [ -f _requirements.txt ]; then \
|
elif [ -f _requirements.txt ]; then \
|
||||||
echo "Installing Python packages from _requirements.txt..."; \
|
echo "Installing Python packages from _requirements.txt..."; \
|
||||||
$$HOME/.venvs/pkgmgr/bin/pip install -r _requirements.txt; \
|
"$$HOME/.venvs/pkgmgr/bin/pip" install -r _requirements.txt; \
|
||||||
else \
|
else \
|
||||||
echo "No requirements.txt or _requirements.txt found, skipping dependency installation."; \
|
echo "No requirements.txt or _requirements.txt found, skipping dependency installation."; \
|
||||||
fi
|
fi; \
|
||||||
@echo "Ensuring $$HOME/.bashrc and $$HOME/.zshrc exist..."
|
echo "Ensuring $$HOME/.bashrc and $$HOME/.zshrc exist..."; \
|
||||||
@touch "$$HOME/.bashrc" "$$HOME/.zshrc"
|
touch "$$HOME/.bashrc" "$$HOME/.zshrc"; \
|
||||||
@echo "Ensuring automatic activation of $$HOME/.venvs/pkgmgr for this user..."
|
echo "Ensuring automatic activation of $$HOME/.venvs/pkgmgr for this user..."; \
|
||||||
@for rc in "$$HOME/.bashrc" "$$HOME/.zshrc"; do \
|
for rc in "$$HOME/.bashrc" "$$HOME/.zshrc"; do \
|
||||||
rc_line='if [ -d "$${HOME}/.venvs/pkgmgr" ]; then . "$${HOME}/.venvs/pkgmgr/bin/activate"; if [ -n "$${PS1:-}" ]; then echo "Global Python virtual environment '\''~/.venvs/pkgmgr'\'' activated."; fi; fi'; \
|
rc_line='if [ -d "$${HOME}/.venvs/pkgmgr" ]; then . "$${HOME}/.venvs/pkgmgr/bin/activate"; if [ -n "$${PS1:-}" ]; then echo "Global Python virtual environment '\''~/.venvs/pkgmgr'\'' activated."; fi; fi'; \
|
||||||
grep -qxF "$${rc_line}" "$$rc" || echo "$${rc_line}" >> "$$rc"; \
|
grep -qxF "$${rc_line}" "$$rc" || echo "$${rc_line}" >> "$$rc"; \
|
||||||
done
|
done; \
|
||||||
@echo "Arch/Manjaro detection and optional AUR setup..."
|
echo "Arch/Manjaro detection and optional AUR setup..."; \
|
||||||
@if command -v pacman >/dev/null 2>&1; then \
|
if command -v pacman >/dev/null 2>&1; then \
|
||||||
$(MAKE) aur_builder_setup; \
|
$(MAKE) aur_builder_setup; \
|
||||||
else \
|
else \
|
||||||
echo "Not Arch-based (no pacman). Skipping aur_builder/yay setup."; \
|
echo "Not Arch-based (no pacman). Skipping aur_builder/yay setup."; \
|
||||||
|
fi; \
|
||||||
|
echo "Installation complete. Please restart your shell (or 'exec bash' or 'exec zsh') for the changes to take effect."; \
|
||||||
fi
|
fi
|
||||||
@echo "Installation complete. Please restart your shell (or 'exec bash' or 'exec zsh') for the changes to take effect."
|
|
||||||
|
|
||||||
# Only runs on Arch/Manjaro
|
# Only runs on Arch/Manjaro
|
||||||
aur_builder_setup:
|
aur_builder_setup:
|
||||||
|
|||||||
8
PKGBUILD
8
PKGBUILD
@@ -24,13 +24,11 @@ _srcdir_name="source"
|
|||||||
|
|
||||||
prepare() {
|
prepare() {
|
||||||
mkdir -p "$srcdir/$_srcdir_name"
|
mkdir -p "$srcdir/$_srcdir_name"
|
||||||
|
|
||||||
# Copy the full local tree into $srcdir/source,
|
|
||||||
# but avoid makepkg's own directories and the VCS metadata.
|
|
||||||
rsync -a \
|
rsync -a \
|
||||||
--exclude="src" \
|
|
||||||
--exclude="pkg" \
|
|
||||||
--exclude=".git" \
|
--exclude=".git" \
|
||||||
|
--exclude=".github" \
|
||||||
|
--exclude="pkg" \
|
||||||
|
--exclude="srcpkg" \
|
||||||
"$startdir/" "$srcdir/$_srcdir_name/"
|
"$startdir/" "$srcdir/$_srcdir_name/"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
137
flake.nix
137
flake.nix
@@ -13,118 +13,95 @@
|
|||||||
let
|
let
|
||||||
systems = [ "x86_64-linux" "aarch64-linux" ];
|
systems = [ "x86_64-linux" "aarch64-linux" ];
|
||||||
|
|
||||||
# Helper to build an attribute set for all target systems
|
|
||||||
forAllSystems = f:
|
forAllSystems = f:
|
||||||
builtins.listToAttrs (map (system: {
|
builtins.listToAttrs (map (system: {
|
||||||
name = system;
|
name = system;
|
||||||
value = f system;
|
value = f system;
|
||||||
}) systems);
|
}) systems);
|
||||||
in {
|
in
|
||||||
# Development shells: `nix develop .#default`
|
{
|
||||||
devShells = forAllSystems (system:
|
##########################################################################
|
||||||
let
|
# PACKAGES
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
##########################################################################
|
||||||
|
|
||||||
# Base Python interpreter
|
|
||||||
python = pkgs.python311;
|
|
||||||
|
|
||||||
# Python environment with pip + PyYAML so `python -m pip` works
|
|
||||||
pythonEnv = python.withPackages (ps: with ps; [
|
|
||||||
pip
|
|
||||||
pyyaml
|
|
||||||
]);
|
|
||||||
|
|
||||||
# Be robust: use ansible-core if available, otherwise ansible
|
|
||||||
ansiblePkg =
|
|
||||||
if pkgs ? ansible-core then pkgs.ansible-core
|
|
||||||
else pkgs.ansible;
|
|
||||||
in {
|
|
||||||
default = pkgs.mkShell {
|
|
||||||
buildInputs = [
|
|
||||||
pythonEnv
|
|
||||||
pkgs.git
|
|
||||||
ansiblePkg
|
|
||||||
];
|
|
||||||
shellHook = ''
|
|
||||||
echo "Entered pkgmgr development environment for ${system}";
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
# Packages: `nix build .#pkgmgr` or `nix build .#default`
|
|
||||||
packages = forAllSystems (system:
|
packages = forAllSystems (system:
|
||||||
let
|
let
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
python = pkgs.python311;
|
pyPkgs = pkgs.python311Packages;
|
||||||
|
|
||||||
# Runtime Python for pkgmgr (with pip + PyYAML)
|
|
||||||
pythonEnv = python.withPackages (ps: with ps; [
|
|
||||||
pip
|
|
||||||
pyyaml
|
|
||||||
]);
|
|
||||||
|
|
||||||
# Optional: include Ansible in the runtime closure
|
|
||||||
ansiblePkg =
|
|
||||||
if pkgs ? ansible-core then pkgs.ansible-core
|
|
||||||
else pkgs.ansible;
|
|
||||||
in
|
in
|
||||||
rec {
|
rec {
|
||||||
pkgmgr = pkgs.stdenv.mkDerivation {
|
pkgmgr = pyPkgs.buildPythonApplication {
|
||||||
pname = "package-manager";
|
pname = "package-manager";
|
||||||
version = "0.1.1";
|
version = "0.1.1";
|
||||||
|
|
||||||
# Use the current repository as the source
|
# Use the git repo as source
|
||||||
src = ./.;
|
src = ./.;
|
||||||
|
|
||||||
# No traditional configure/build steps
|
# Build using pyproject.toml
|
||||||
dontConfigure = true;
|
format = "pyproject";
|
||||||
dontBuild = true;
|
|
||||||
|
|
||||||
# Runtime dependencies: Python (with pip + PyYAML) + Ansible
|
# Build backend requirements from [build-system]
|
||||||
buildInputs = [
|
nativeBuildInputs = [
|
||||||
pythonEnv
|
pyPkgs.setuptools
|
||||||
ansiblePkg
|
pyPkgs.wheel
|
||||||
];
|
];
|
||||||
|
|
||||||
installPhase = ''
|
# Runtime dependencies (matches [project.dependencies])
|
||||||
mkdir -p "$out/bin" "$out/lib/package-manager"
|
propagatedBuildInputs = [
|
||||||
|
pyPkgs.pyyaml
|
||||||
|
# Add more here if needed, e.g.:
|
||||||
|
# pyPkgs.click
|
||||||
|
# pyPkgs.rich
|
||||||
|
];
|
||||||
|
|
||||||
# Copy the full project tree into the runtime closure
|
doCheck = false;
|
||||||
cp -a . "$out/lib/package-manager/"
|
|
||||||
|
|
||||||
# Wrapper that runs main.py from the copied tree,
|
pythonImportsCheck = [ "pkgmgr" ];
|
||||||
# using the pythonEnv interpreter.
|
|
||||||
cat > "$out/bin/pkgmgr" << 'EOF'
|
|
||||||
#!${pythonEnv}/bin/python3
|
|
||||||
import os
|
|
||||||
import runpy
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
base_dir = os.path.join(os.path.dirname(__file__), "..", "lib", "package-manager")
|
|
||||||
main_path = os.path.join(base_dir, "main.py")
|
|
||||||
os.chdir(base_dir)
|
|
||||||
runpy.run_path(main_path, run_name="__main__")
|
|
||||||
EOF
|
|
||||||
|
|
||||||
chmod +x "$out/bin/pkgmgr"
|
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
|
|
||||||
# Default package points to pkgmgr
|
|
||||||
default = pkgmgr;
|
default = pkgmgr;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
# Apps: `nix run .#pkgmgr` or `nix run .#default`
|
##########################################################################
|
||||||
|
# DEVELOPMENT SHELL
|
||||||
|
##########################################################################
|
||||||
|
devShells = forAllSystems (system:
|
||||||
|
let
|
||||||
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
pkgmgrPkg = self.packages.${system}.pkgmgr;
|
||||||
|
|
||||||
|
ansiblePkg =
|
||||||
|
if pkgs ? ansible-core then pkgs.ansible-core
|
||||||
|
else pkgs.ansible;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
default = pkgs.mkShell {
|
||||||
|
buildInputs = [
|
||||||
|
pkgmgrPkg
|
||||||
|
pkgs.git
|
||||||
|
ansiblePkg
|
||||||
|
];
|
||||||
|
|
||||||
|
shellHook = ''
|
||||||
|
echo "Entered pkgmgr development shell for ${system}"
|
||||||
|
echo "pkgmgr CLI is available via the flake build"
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
##########################################################################
|
||||||
|
# nix run .#pkgmgr
|
||||||
|
##########################################################################
|
||||||
apps = forAllSystems (system:
|
apps = forAllSystems (system:
|
||||||
let
|
let
|
||||||
pkgmgrPkg = self.packages.${system}.pkgmgr;
|
pkgmgrPkg = self.packages.${system}.pkgmgr;
|
||||||
in {
|
in
|
||||||
|
{
|
||||||
pkgmgr = {
|
pkgmgr = {
|
||||||
type = "app";
|
type = "app";
|
||||||
program = "${pkgmgrPkg}/bin/pkgmgr";
|
program = "${pkgmgrPkg}/bin/pkgmgr";
|
||||||
};
|
};
|
||||||
|
|
||||||
default = self.apps.${system}.pkgmgr;
|
default = self.apps.${system}.pkgmgr;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
42
pyproject.toml
Normal file
42
pyproject.toml
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = [
|
||||||
|
"setuptools>=68",
|
||||||
|
"wheel"
|
||||||
|
]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "package-manager"
|
||||||
|
version = "0.1.1"
|
||||||
|
description = "Kevin's package-manager tool (pkgmgr)"
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.11"
|
||||||
|
license = { text = "MIT" }
|
||||||
|
|
||||||
|
authors = [
|
||||||
|
{ name = "Kevin Veen-Birkenbach", email = "info@veen.world" }
|
||||||
|
]
|
||||||
|
|
||||||
|
# Base runtime dependencies
|
||||||
|
dependencies = [
|
||||||
|
"PyYAML>=6.0"
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.urls]
|
||||||
|
Homepage = "https://github.com/kevinveenbirkenbach/package-manager"
|
||||||
|
Source = "https://github.com/kevinveenbirkenbach/package-manager"
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
dev = [
|
||||||
|
"pytest",
|
||||||
|
"mypy"
|
||||||
|
]
|
||||||
|
|
||||||
|
# CLI entrypoint: this is the "pkgmgr" command
|
||||||
|
[project.scripts]
|
||||||
|
pkgmgr = "pkgmgr.cli:main"
|
||||||
|
|
||||||
|
# Tell setuptools to find the pkgmgr package
|
||||||
|
[tool.setuptools.packages.find]
|
||||||
|
where = ["."]
|
||||||
|
include = ["pkgmgr*"]
|
||||||
Reference in New Issue
Block a user