feat: system-agnostic package manager — 5 inits, 2 bootloaders, parallel scheduler

Complete rewrite of kappa from a sequential build tool into a
system-agnostic package manager with runtime init switching.

Core additions:
- 5 init system backends: systemd, openrc, s6, runit, dinit
  (service file generation, enable/disable, init_paths)
- 2 bootloader backends: grub, limine (config generation, fallback entries)
- Parallel scheduler with worker pool, depth-based priority (Beta/Alpha/Zeta),
  atomic claiming, dependency tracking, deduplication, and failure propagation
- Init-switch impact analysis: only rebuild packages using ${enabledinit}
- Init-agnostic service definitions: flat NamedService blocks replace
  per-init nesting
- Package conflicts: mutual incompatibility detection in resolver
- System groups: init-agnostic group creation in DSL
- Init-agnostic hostname/timezone: direct /etc/hostname and /etc/localtime writes
- Source tarball caching at /kappa/cache/ with atomic write-then-rename
- Package recipe caching with remote fetching and version comparison
- remotes = [...] block in system config for package repositories
- Auto-fetch: rebuild resolves missing packages from remotes
- uninstall phase in package definitions
- ${enabledinit} eval variable for init-conditional builds
- Shared util module (to_lower, shell_escape)
- 54 integration tests across two shell test suites
- Comprehensive README and CONTRIBUTING guide

Bug fixes from review:
- CRITICAL: Replace std::system() with fork+execvp (command injection)
- CRITICAL: Fix scheduler deadlock on successful completion
- CRITICAL: Fix rebuild init/kernel/bootloader change detection
- HIGH: Fix path traversal via unsanitized package names in cache
- HIGH: Fix TOCTOU race in cache write with atomic rename
- HIGH: Fix formatter dropping remotes/imports blocks
- HIGH: Fix formatter stripping empty-string assert values
- HIGH: Fix formatter non-idempotent output (sorted key iteration)
- HIGH: Populate ${enabledinit} from boot.init in BuildStep
- MEDIUM: Fix data race on non-atomic scheduler stop flag
- MEDIUM: Fix compute_depths() traversal direction
- MEDIUM: Add runit to doctor supported-init warning
- MEDIUM: Extract to_lower/shell_escape to shared kappa::util
- MEDIUM: Consolidate generator declarations in headers
This commit is contained in:
2026-07-31 05:32:57 -04:00
parent d6612d0a4a
commit dd984f96d4
40 changed files with 3047 additions and 117 deletions
+74
View File
@@ -0,0 +1,74 @@
#include "kappa/service/service.hpp"
#include <format>
#include <sstream>
namespace kappa::service {
std::string generate_dinit_service(const ServiceSpec& spec) {
std::ostringstream out;
// Header
out << std::format("# Generated by kappa — do not edit manually\n");
out << std::format("# dinit service: {}\n", spec.name);
out << "\n";
// Type mapping
std::string dinit_type;
if (spec.type == "simple" || spec.type == "notify" ||
spec.type == "longrun") {
dinit_type = "process";
} else if (spec.type == "forking") {
dinit_type = "bgprocess";
} else if (spec.type == "oneshot") {
dinit_type = "scripted";
} else {
dinit_type = "process";
}
out << std::format("type = {}\n", dinit_type);
out << std::format("command = {}\n", spec.exec);
// Restart policy
if (spec.restart_policy == "always") {
out << "restart = true\n";
} else if (spec.restart_policy == "on-failure") {
out << "restart = true\n";
} else if (spec.restart_policy == "never") {
out << "restart = false\n";
} else if (!spec.restart_policy.empty()) {
out << "restart = true\n";
}
// depends-on
if (!spec.after.empty()) {
out << std::format("depends-on = {}\n", spec.after);
}
// working-dir
if (!spec.working_dir.empty()) {
out << std::format("working-dir = {}\n", spec.working_dir);
}
// run-as
if (!spec.user.empty()) {
out << std::format("run-as = {}\n", spec.user);
}
// Environment variables (as comments — dinit doesn't support inline env)
if (!spec.env.empty()) {
out << "\n# Environment variables:\n";
for (const auto& [key, value] : spec.env) {
out << std::format("# {}={}\n", key, value);
}
}
// description
if (!spec.description.empty()) {
out << std::format("description = {}\n", spec.description);
}
return out.str();
}
} // namespace kappa::service
+202
View File
@@ -0,0 +1,202 @@
#include "kappa/service/service.hpp"
#include <filesystem>
#include <format>
#include <fstream>
namespace kappa::service {
// ---------------------------------------------------------------------------
// Backend service-file generators (defined in separate .cpp files)
// systemd_service and s6_service are declared in service.hpp
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ServiceSpec::from_service_init
// ---------------------------------------------------------------------------
ServiceSpec ServiceSpec::from_service_init(const dsl::NamedService& ns) {
ServiceSpec spec;
spec.name = ns.name;
spec.description = ns.description;
spec.exec = ns.exec;
spec.user = ns.user;
spec.ports = ns.ports;
spec.env = ns.env;
spec.after = ns.after;
spec.working_dir = ns.working_dir;
// Normalize type
if (ns.type == "forking" || ns.type == "longrun" ||
ns.type == "notify" || ns.type == "oneshot") {
spec.type = ns.type;
} else {
spec.type = "simple";
}
// Restart policy
spec.restart_policy = ns.restart;
if (spec.restart_policy.empty() && ns.type == "longrun") {
spec.restart_policy = "always";
}
return spec;
}
// ---------------------------------------------------------------------------
// generate_service_file — dispatch to the correct backend
// ---------------------------------------------------------------------------
std::string generate_service_file(InitSystem is, const ServiceSpec& spec) {
switch (is) {
case InitSystem::Systemd:
return generate_systemd_service(spec);
case InitSystem::OpenRC:
return generate_openrc_service(spec);
case InitSystem::S6:
return generate_s6_service(spec);
case InitSystem::Dinit:
return generate_dinit_service(spec);
case InitSystem::Runit:
return generate_runit_service(spec);
case InitSystem::Unknown:
default:
return {};
}
}
// ---------------------------------------------------------------------------
// install_service — write the generated file(s) to disk
// ---------------------------------------------------------------------------
ServiceInstallResult install_service(InitSystem is,
const ServiceSpec& spec,
std::string_view prefix) {
namespace fs = std::filesystem;
auto paths = init_paths(is, prefix);
if (paths.service_dir.empty()) {
return {false, {}, "Unknown init system"};
}
std::error_code ec;
// ---- S6: directory-based layout (type + run) ----
// S6: directory-based layout. Content generated inline rather than
// calling generate_s6_service() to avoid parsing the combined output.
if (is == InitSystem::S6) {
fs::path svc_dir = fs::path(paths.service_dir) / spec.name;
fs::create_directories(svc_dir, ec);
if (ec) {
return {false, {}, ec.message()};
}
// --- type file ---
{
fs::path type_path = svc_dir / "type";
std::ofstream out(type_path);
if (!out) {
return {false, {},
std::format("Failed to write {}", type_path.string())};
}
out << ((spec.type == "oneshot") ? "oneshot" : "longrun");
}
// --- run file ---
fs::path run_path = svc_dir / "run";
{
std::ofstream out(run_path);
if (!out) {
return {false, {},
std::format("Failed to write {}", run_path.string())};
}
out << "#!/bin/execlineb -P\n";
out << "# Generated by kappa — do not edit manually\n";
out << std::format("# s6 service: {}\n", spec.name);
if (!spec.working_dir.empty()) {
out << std::format("cd {}\n", spec.working_dir);
}
for (const auto& [key, value] : spec.env) {
out << std::format("export {} \"{}\"\n", key, value);
}
if (!spec.user.empty()) {
out << std::format("s6-setuidgid {}\n", spec.user);
}
out << spec.exec << "\n";
}
// Make run file executable
fs::permissions(run_path,
fs::perms::owner_exec | fs::perms::group_exec |
fs::perms::others_exec,
fs::perm_options::add, ec);
return {true, svc_dir.string(), {}};
}
// ---- Runit: directory-based layout (run) ----
if (is == InitSystem::Runit) {
fs::path svc_dir = fs::path(paths.service_dir) / spec.name;
fs::create_directories(svc_dir, ec);
if (ec) {
return {false, {}, ec.message()};
}
fs::path run_path = svc_dir / "run";
{
std::ofstream out(run_path);
if (!out) {
return {false, {},
std::format("Failed to write {}", run_path.string())};
}
out << generate_runit_service(spec);
}
// Make run executable
fs::permissions(run_path,
fs::perms::owner_exec | fs::perms::group_exec |
fs::perms::others_exec,
fs::perm_options::add, ec);
return {true, svc_dir.string(), {}};
}
// ---- Systemd / OpenRC / Dinit: single service file ----
std::string content = generate_service_file(is, spec);
if (content.empty()) {
return {false, {}, "Failed to generate service file for init system"};
}
// Determine filename
std::string filename;
switch (is) {
case InitSystem::Systemd:
filename = std::format("{}.service", spec.name);
break;
case InitSystem::OpenRC:
case InitSystem::Dinit:
filename = spec.name;
break;
default:
return {false, {}, "Unknown init system"};
}
fs::path file_path = fs::path(paths.service_dir) / filename;
fs::create_directories(file_path.parent_path(), ec);
if (ec) {
return {false, {}, ec.message()};
}
{
std::ofstream out(file_path);
if (!out) {
return {false, {},
std::format("Failed to write {}", file_path.string())};
}
out << content;
}
return {true, file_path.string(), {}};
}
} // namespace kappa::service
+81
View File
@@ -0,0 +1,81 @@
#include "kappa/service/service.hpp"
#include "kappa/util.hpp"
#include <format>
#include <sstream>
namespace kappa::service {
namespace {
bool is_background_type(std::string_view type) {
return type == "longrun" || type == "notify" || type == "forking";
}
} // namespace
std::string generate_openrc_service(const ServiceSpec& spec) {
std::ostringstream os;
// Shebang and header
os << "#!/sbin/openrc-run\n";
os << "# Generated by kappa — do not edit manually\n";
// Description
auto desc = spec.description.empty()
? spec.name + " service"
: spec.description;
os << std::format("description=\"{}\"\n", util::shell_escape(desc));
// Command
os << std::format("\ncommand=\"{}\"\n", util::shell_escape(spec.exec));
// User
if (!spec.user.empty()) {
os << std::format("command_user=\"{}\"\n", util::shell_escape(spec.user));
}
// command_background and command_args (type-specific)
if (spec.type == "oneshot") {
os << "command_background=false\n";
os << "command_args=\"\"\n";
} else if (is_background_type(spec.type)) {
os << "command_background=true\n";
} else if (!spec.working_dir.empty()) {
os << "command_background=true\n";
}
// Working directory
if (!spec.working_dir.empty()) {
os << std::format("directory=\"{}\"\n",
util::shell_escape(spec.working_dir));
}
// Depend block (if after or ports)
bool has_depend = !spec.after.empty() || !spec.ports.empty();
if (has_depend) {
os << "\ndepend() {\n";
if (!spec.after.empty()) {
os << std::format(" need {}\n", spec.after);
}
if (!spec.ports.empty()) {
os << " use net\n";
}
os << "}\n";
}
// Restart policy comment
if (!spec.restart_policy.empty()) {
os << std::format("# restart policy: {}\n", spec.restart_policy);
}
// Environment exports
for (auto& [key, value] : spec.env) {
os << std::format("export {}=\"{}\"\n",
key, util::shell_escape(value));
}
return os.str();
}
} // namespace kappa::service
+57
View File
@@ -0,0 +1,57 @@
#include "kappa/service/service.hpp"
#include "kappa/util.hpp"
#include <format>
#include <sstream>
namespace kappa::service {
std::string generate_runit_service(const ServiceSpec& spec) {
std::ostringstream out;
// Shebang
out << "#!/bin/sh\n";
// Header
out << "# Generated by kappa — do not edit manually\n";
out << std::format("# runit service: {}\n", spec.name);
out << std::format("# Type: {}\n", spec.type);
// Forking note
if (spec.type == "forking") {
out << "# NOTE: runit requires foreground execution.\n";
out << "# If the daemon forks, pass --foreground or equivalent"
" flag.\n";
}
// Oneshot note
if (spec.type == "oneshot") {
out << "# NOTE: runit has no native oneshot support. This service"
" will restart on exit.\n";
}
// Redirect stderr to stdout for logging
out << "exec 2>&1\n";
// Working directory
if (!spec.working_dir.empty()) {
out << std::format("cd \"{}\"\n", util::shell_escape(spec.working_dir));
}
// Environment variables
for (const auto& [key, value] : spec.env) {
out << std::format("export {}=\"{}\"\n", key, util::shell_escape(value));
}
// Final exec — replace the shell with the daemon
if (!spec.user.empty()) {
out << std::format("exec chpst -u {} {}\n",
util::shell_escape(spec.user), spec.exec);
} else {
out << "exec " << spec.exec << "\n";
}
return out.str();
}
} // namespace kappa::service
+47
View File
@@ -0,0 +1,47 @@
#include "kappa/service/service.hpp"
#include <format>
#include <sstream>
namespace kappa::service {
std::string generate_s6_service(const ServiceSpec& spec) {
std::ostringstream out;
// --- type file content ---
std::string type_content;
if (spec.type == "oneshot") {
type_content = "oneshot";
} else {
// "longrun", "notify", and anything else map to longrun in s6
type_content = "longrun";
}
// --- run file content ---
std::ostringstream run;
run << "#!/bin/execlineb -P\n";
run << "# Generated by kappa — do not edit manually\n";
run << std::format("# s6 service: {}\n", spec.name);
if (!spec.working_dir.empty()) {
run << std::format("cd {}\n", spec.working_dir);
}
for (const auto& [key, value] : spec.env) {
run << std::format("export {} \"{}\"\n", key, value);
}
if (!spec.user.empty()) {
run << std::format("s6-setuidgid {}\n", spec.user);
}
run << spec.exec << "\n";
// --- combined output ---
out << std::format("# --- s6 service directory: {} ---\n", spec.name);
out << "# file: type\n";
out << type_content << "\n";
out << "# file: run\n";
out << run.str();
return out.str();
}
} // namespace kappa::service
+62
View File
@@ -0,0 +1,62 @@
#include "kappa/service/service.hpp"
#include <format>
#include <sstream>
namespace kappa::service {
std::string generate_systemd_service(const ServiceSpec& spec) {
std::ostringstream out;
// --- [Unit] ---
out << "[Unit]\n";
out << std::format("Description={}\n",
spec.description.empty() ? spec.name : spec.description);
if (!spec.after.empty()) {
out << std::format("After={}\n", spec.after);
}
// --- [Service] ---
out << "\n[Service]\n";
out << std::format("ExecStart={}\n", spec.exec);
if (spec.type == "simple") {
out << "Type=simple\n";
} else if (spec.type == "forking") {
out << "Type=forking\n";
} else if (spec.type == "oneshot") {
out << "Type=oneshot\n";
} else if (spec.type == "notify") {
out << "Type=notify\n";
} else {
out << "Type=simple\n";
}
if (!spec.user.empty()) {
out << std::format("User={}\n", spec.user);
}
if (spec.restart_policy == "always") {
out << "Restart=always\n";
} else if (spec.restart_policy == "on-failure") {
out << "Restart=on-failure\n";
} else if (spec.restart_policy == "never") {
out << "Restart=no\n";
}
if (!spec.working_dir.empty()) {
out << std::format("WorkingDirectory={}\n", spec.working_dir);
}
for (const auto& [key, value] : spec.env) {
out << std::format("Environment=\"{0}={1}\"\n", key, value);
}
// --- [Install] ---
out << "\n[Install]\n";
out << "WantedBy=multi-user.target\n";
return out.str();
}
} // namespace kappa::service
+98
View File
@@ -0,0 +1,98 @@
#include "kappa/service/types.hpp"
#include "kappa/util.hpp"
#include <algorithm>
#include <cctype>
#include <format>
#include <ranges>
namespace kappa::service {
InitSystem parse_init_system(std::string_view name) {
auto lower = util::to_lower(name);
if (lower == "systemd") return InitSystem::Systemd;
if (lower == "openrc") return InitSystem::OpenRC;
if (lower == "s6") return InitSystem::S6;
if (lower == "runit") return InitSystem::Runit;
if (lower == "dinit") return InitSystem::Dinit;
return InitSystem::Unknown;
}
std::string_view to_string(InitSystem is) {
switch (is) {
case InitSystem::Systemd: return "systemd";
case InitSystem::OpenRC: return "openrc";
case InitSystem::S6: return "s6";
case InitSystem::Runit: return "runit";
case InitSystem::Dinit: return "dinit";
case InitSystem::Unknown: return "unknown";
}
return "unknown";
}
bool is_supported(std::string_view name) {
return parse_init_system(name) != InitSystem::Unknown;
}
std::vector<InitSystem> all_systems() {
return {InitSystem::Systemd, InitSystem::OpenRC,
InitSystem::S6, InitSystem::Runit, InitSystem::Dinit};
}
std::string_view init_description(InitSystem is) {
switch (is) {
case InitSystem::Systemd:
return "systemd — system and service manager";
case InitSystem::OpenRC:
return "OpenRC — dependency-based init system";
case InitSystem::S6:
return "s6 — s6 supervision suite";
case InitSystem::Runit:
return "runit — supervision suite";
case InitSystem::Dinit:
return "dinit — service manager / init system";
case InitSystem::Unknown:
return "unknown init system";
}
return "unknown init system";
}
InitPaths init_paths(InitSystem is, std::string_view prefix) {
switch (is) {
case InitSystem::Systemd:
return {
.service_dir = std::format("{}etc/systemd/system", prefix),
.enable_cmd = "systemctl enable",
.disable_cmd = "systemctl disable",
};
case InitSystem::OpenRC:
return {
.service_dir = std::format("{}etc/init.d", prefix),
.enable_cmd = "rc-update add",
.disable_cmd = "rc-update del",
};
case InitSystem::S6:
return {
.service_dir = std::format("{}etc/s6/sv", prefix),
.enable_cmd = "s6-rc-bundle-update",
.disable_cmd = "s6-rc-bundle-update",
};
case InitSystem::Runit:
return {
.service_dir = std::format("{}etc/sv", prefix),
.enable_cmd = "ln -sf /etc/sv",
.disable_cmd = "rm -f /var/service",
};
case InitSystem::Dinit:
return {
.service_dir = std::format("{}etc/dinit.d", prefix),
.enable_cmd = "dinitctl enable",
.disable_cmd = "dinitctl disable",
};
case InitSystem::Unknown:
return {};
}
return {};
}
} // namespace kappa::service