Files
huntedbytheirs d02ab4b7b6 docs: update examples with += env operator and env usage docs
- foo.kap: document all three env operators (=, ?=, +=) in header,
  demonstrate += in env block with comments

- postgres.kap: demonstrate += for appending security hardening flags

- config.kap: demonstrate system-level += for inherited flags
2026-08-04 13:19:59 -04:00

151 lines
5.2 KiB
Plaintext

/*
* postgresql — a database server shipping multiple services.
*
* MULTI-SERVICE PACKAGES
* ======================
* Packages that install more than one long-running process can declare
* multiple named `service` blocks. Each has its own exec, type, ports,
* and lifecycle config. The system config enables them individually using
* dot-notation (see config.kap):
*
* services {
* postgresql.main { enable = true }
* postgresql.checkpointer { enable = true }
* postgresql.walwriter { enable = true }
* }
*
* Omitting the dot selects the service named "main".
*
*
* INIT-CONDITIONAL BUILDS
* =======================
* The variable ${enabledinit} exposes the configured init system name
* (from boot.init in config.kap) during the build phase. Use shell
* conditionals — no DSL if/else needed:
*
* build {
* case ${enabledinit} in
* systemd) ./configure --with-systemd --prefix=${prefix} ;;
* openrc) ./configure --with-openrc --prefix=${prefix} ;;
* s6|dinit) ./configure --prefix=${prefix} ;;
* esac
* make -j${jobs}
* }
*
* ${enabledinit} is interpolated to the literal init system name
* ("systemd", "openrc", "s6", or "dinit") before the shell executes the
* block. No DSL context-sensitive parsing required.
*
*
* SERVICE TYPE TRANSLATION (for reference)
* ========================================
* semantic │ systemd │ openrc │ s6 │ dinit
* ──────────┼────────────┼─────────────────────┼──────────┼──────────
* simple │ Type=simple│ bg=false │ longrun │ process
* forking │ Type=fork │ bg=true │ longrun │ bgprocess
* notify │ Type=notify│ bg=true │ longrun │ process
* oneshot │ Type=one │ bg=false, args="" │ oneshot │ scripted
* longrun │ Type=simple│ bg=true │ longrun │ process
*/
package "postgresql" {
const version = "16.3"
const source = "https://ftp.postgresql.org/pub/source/v${version}/postgresql-${version}.tar.gz"
sha256 = "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2"
license = "PostgreSQL"
depends = [
{ name = "readline", version = ">=8" },
{ name = "zlib", version = ">=1.2" },
{ name = "openssl" },
]
conflicts = [] // mutually exclusive packages (e.g. systemd vs eudev)
features {
ssl = { enabled = true, flag = "--with-ssl=openssl" }
nls = { enabled = true, flag = "--enable-nls" }
systemd = { enabled = false, flag = "--with-systemd" }
}
config {
file "etc/postgresql/data/postgresql.conf" mode = "default" {
port = ${cfg.port ? 5432}
max_connections = ${cfg.max_conn ? 100}
shared_buffers = ${cfg.shared_buf ? 128MB}
}
}
env {
CFLAGS = "-O2"
// append security hardening flags
CFLAGS += "-D_FORTIFY_SOURCE=2"
LDFLAGS = "-Wl,--as-needed"
}
// --- services ---------------------------------------------------------
// PostgreSQL ships the main server plus several auxiliary processes.
// Each runs as a separate service under the init system.
// Default service (name = "main"). Enabled via: postgresql { enable = true }
service main {
exec = "/usr/bin/postgres -D /var/lib/postgresql/data"
type = "forking" // postmaster daemonises itself
user = "postgres"
ports = [5432]
description = "PostgreSQL database server"
after = "network"
restart = "always"
working_dir = "/var/lib/postgresql"
}
// Background writer — handles checkpoint I/O.
service checkpointer {
exec = "/usr/bin/postgres-checkpointer"
type = "longrun"
user = "postgres"
description = "PostgreSQL checkpointer process"
restart = "always"
}
// WAL writer — flushes write-ahead log to disk.
service walwriter {
exec = "/usr/bin/postgres-walwriter"
type = "longrun"
user = "postgres"
restart = "always"
}
// --- build phases ------------------------------------------------------
prepare {
tar xf postgresql-${version}.tar.gz
}
// init-conditional build: PostgreSQL optionally links against systemd
// for socket activation and service notification. Use ${enabledinit}
// to decide configure flags without per-init service blocks.
build {
case ${enabledinit} in
systemd) ./configure --with-systemd --with-ssl=openssl --prefix=${prefix} ;;
*) ./configure --with-ssl=openssl --prefix=${prefix} ;;
esac
make -j${jobs} world
}
check {
make check
}
install {
make DESTDIR=${destdir} install-world
}
uninstall {
make DESTDIR=${destdir} uninstall-world
}
assert {
"data directory must exist" : system.config.data_dir != ""
}
}