Files
komp_ac/packaging/musl/default.nix
2026-08-14 18:10:46 +02:00

177 lines
4.7 KiB
Nix

{ pkgs, source, version }:
let
muslPkgs = pkgs.pkgsMusl;
postgresqlVersion = "17.10";
postgresqlArchiveVersion = "17.10.0";
archiveName = "postgresql-${postgresqlArchiveVersion}-x86_64-unknown-linux-musl.tar.gz";
bootstrapInput = pname: inputs:
pkgs.lib.findFirst
(input: (input.pname or "") == pname)
(throw "Nixpkgs does not expose ${pname} for the musl toolchain")
inputs;
bootstrapRustPlatform = muslPkgs.makeRustPlatform {
rustc = bootstrapInput
"rustc-bootstrap"
muslPkgs.rustc-unwrapped.nativeBuildInputs;
cargo = bootstrapInput
"cargo-bootstrap"
muslPkgs.cargo.nativeBuildInputs;
};
staticOpenSSL = muslPkgs.openssl.overrideAttrs (old: {
configureFlags =
builtins.filter (flag: flag != "shared") old.configureFlags
++ [ "no-shared" ];
doCheck = false;
});
postgresqlSource = pkgs.fetchurl {
url = "https://ftp.postgresql.org/pub/source/v${postgresqlVersion}/postgresql-${postgresqlVersion}.tar.bz2";
hash = "sha256-B4oDUW3NvbcF/sr0Feo9E6lWxYnkbwn+1ooG+wBZjJA=";
};
postgresqlArchive = muslPkgs.stdenv.mkDerivation {
pname = "komp-ac-postgresql-musl";
version = postgresqlArchiveVersion;
src = postgresqlSource;
nativeBuildInputs = [
pkgs.bison
pkgs.file
pkgs.flex
pkgs.gnutar
pkgs.gzip
pkgs.perl
];
configurePhase = ''
runHook preConfigure
./configure \
--prefix="$out/postgresql" \
--disable-nls \
--without-gssapi \
--without-icu \
--without-ldap \
--without-libxml \
--without-libxslt \
--without-llvm \
--without-lz4 \
--without-openssl \
--without-pam \
--without-readline \
--without-systemd \
--without-zlib \
--without-zstd
runHook postConfigure
'';
buildPhase = ''
runHook preBuild
make -j"$NIX_BUILD_CORES"
make -C contrib/btree_gist -j"$NIX_BUILD_CORES"
runHook postBuild
'';
installPhase = ''
runHook preInstall
make install-strip
make -C contrib/btree_gist install
"$CC" -static -Os -s -Wall -Wextra -Werror \
-o postgresql-launcher \
${./postgresql-launcher.c}
mkdir -p "$out/postgresql/libexec"
for tool in "$out"/postgresql/bin/*; do
toolName="''${tool##*/}"
mv "$tool" "$out/postgresql/libexec/$toolName"
cp postgresql-launcher "$out/postgresql/bin/$toolName"
done
cp ${muslPkgs.musl}/lib/ld-musl-x86_64.so.1 \
"$out/postgresql/lib/ld-musl-x86_64.so.1"
file "$out/postgresql/bin/postgres" | grep -q 'statically linked'
"$out/postgresql/bin/postgres" --version
mkdir postgresql-data postgresql-socket
"$out/postgresql/bin/initdb" \
--auth=trust \
--no-locale \
-D "$PWD/postgresql-data"
"$out/postgresql/bin/pg_ctl" \
-D "$PWD/postgresql-data" \
-l "$PWD/postgresql.log" \
-o "-k $PWD/postgresql-socket -p 55432" \
-w start
"$out/postgresql/bin/psql" \
-h "$PWD/postgresql-socket" \
-p 55432 \
-d postgres \
-c "CREATE EXTENSION btree_gist; SET TIME ZONE 'UTC';"
"$out/postgresql/bin/pg_ctl" \
-D "$PWD/postgresql-data" \
-m fast \
-w stop
tar -C "$out" -czf "$out/${archiveName}" postgresql
runHook postInstall
'';
dontPatchELF = true;
dontStrip = true;
};
in
bootstrapRustPlatform.buildRustPackage {
pname = "komp-ac-server-portable";
inherit version source;
src = source;
cargoLock.lockFile = ../../Cargo.lock;
cargoBuildFlags = [
"--package"
"server"
"--no-default-features"
"--features"
"portable-postgres"
];
auditable = false;
doCheck = false;
nativeBuildInputs = [
pkgs.binutils
pkgs.cmake
pkgs.file
pkgs.pkg-config
pkgs.protobuf
];
buildInputs = [ staticOpenSSL ];
OPENSSL_STATIC = "1";
OPENSSL_LIB_DIR = "${staticOpenSSL.out}/lib";
OPENSSL_INCLUDE_DIR = "${staticOpenSSL.dev}/include";
POSTGRESQL_VERSION = "=${postgresqlArchiveVersion}";
KOMP_AC_POSTGRESQL_ARCHIVE = "${postgresqlArchive}/${archiveName}";
KOMP_AC_POSTGRESQL_VERSION = postgresqlArchiveVersion;
SQLX_OFFLINE = "true";
RUSTFLAGS = "-Ctarget-feature=+crt-static";
preBuild = ''
cp -r ${source}/.sqlx .sqlx
'';
postInstall = ''
test -x "$out/bin/server"
! readelf -l "$out/bin/server" | grep -q 'Requesting program interpreter'
! readelf -d "$out/bin/server" | grep -q '(NEEDED)'
'';
meta = {
description = "Portable komp_ac server with embedded musl PostgreSQL";
license = pkgs.lib.licenses.agpl3Plus;
platforms = [ "x86_64-linux" ];
mainProgram = "server";
};
}