Files
jlibghostty/flake.nix
Gregor Lohaus d558d554b3 bytecode v25
2026-05-29 12:29:36 +02:00

172 lines
6.1 KiB
Nix

{
description = "Java FFM bindings for libghostty-vt";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
ghostty = {
url = "github:ghostty-org/ghostty";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, ghostty }:
let
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
forAllSystems = nixpkgs.lib.genAttrs systems;
in
{
packages = forAllSystems (system:
let
pkgs = import nixpkgs { inherit system; };
jdk =
if pkgs ? jdk25_headless then pkgs.jdk25_headless
else if pkgs ? jdk25 then pkgs.jdk25
else if pkgs ? jdk24_headless then pkgs.jdk24_headless
else if pkgs ? jdk24 then pkgs.jdk24
else pkgs.jdk;
graalvm = pkgs.graalvmPackages.graalvm-ce;
version = "0.1.0-SNAPSHOT";
groupPath = "dev/jlibghostty";
artifactId = "jlibghostty";
ghosttyVt = ghostty.packages.${system}.libghostty-vt;
platformName =
if system == "x86_64-linux" then "linux-x86_64"
else if system == "aarch64-linux" then "linux-aarch64"
else if system == "x86_64-darwin" then "macos-x86_64"
else if system == "aarch64-darwin" then "macos-aarch64"
else throw "unsupported system: ${system}";
bundledLibraryName =
if pkgs.stdenv.hostPlatform.isDarwin then "libghostty-vt.dylib"
else "libghostty-vt.so";
sharedLibraryPattern =
if pkgs.stdenv.hostPlatform.isDarwin then "libghostty-vt*.dylib"
else "libghostty-vt.so*";
package = pkgs.stdenvNoCC.mkDerivation {
pname = artifactId;
inherit version;
src = ./.;
nativeBuildInputs = [ jdk ];
dontConfigure = true;
buildPhase = ''
runHook preBuild
mkdir -p build/classes build/test-classes build/resources/${groupPath}/native/${platformName}
if [ -d src/main/resources ]; then
cp -R src/main/resources/. build/resources/
fi
ghostty_lib="$(find ${ghosttyVt} -type f -name '${sharedLibraryPattern}' -print -quit)"
if [ -z "$ghostty_lib" ]; then
echo "Could not find ${sharedLibraryPattern} in ${ghosttyVt}" >&2
find ${ghosttyVt} -maxdepth 4 -type f >&2
exit 1
fi
bundled_lib="build/resources/${groupPath}/native/${platformName}/${bundledLibraryName}"
cp "$ghostty_lib" "$bundled_lib"
find src/main/java -name '*.java' | sort > build/sources.txt
javac --release 25 --module-path ${graalvm}/jmods -d build/classes @build/sources.txt
jar --create \
--file build/${artifactId}-${version}.jar \
-C build/classes . \
-C build/resources .
jar --create \
--file build/${artifactId}-${version}-sources.jar \
-C src/main/java .
find src/test/java -name '*.java' | sort > build/test-sources.txt
if [ -s build/test-sources.txt ]; then
javac --release 25 -cp build/classes -d build/test-classes @build/test-sources.txt
java \
--enable-native-access=ALL-UNNAMED \
-Djlibghostty.library.path="$bundled_lib" \
-cp build/classes:build/test-classes \
dev.jlibghostty.GhosttySmokeTest
fi
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p "$out/share/java" "$out/maven/${groupPath}/${artifactId}/${version}"
cp build/${artifactId}-${version}.jar "$out/share/java/"
cp build/${artifactId}-${version}.jar "$out/maven/${groupPath}/${artifactId}/${version}/"
cp build/${artifactId}-${version}-sources.jar "$out/maven/${groupPath}/${artifactId}/${version}/"
cat > "$out/maven/${groupPath}/${artifactId}/${version}/${artifactId}-${version}.pom" <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev.jlibghostty</groupId>
<artifactId>jlibghostty</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>jlibghostty</name>
<description>Java FFM bindings for libghostty-vt</description>
</project>
POM
runHook postInstall
'';
meta = {
description = "Java FFM bindings for libghostty-vt";
platforms = systems;
};
};
in
{
default = package;
jlibghostty = package;
mavenRepository = package;
});
devShells = forAllSystems (system:
let
pkgs = import nixpkgs { inherit system; };
jdk =
if pkgs ? jdk25_headless then pkgs.jdk25_headless
else if pkgs ? jdk25 then pkgs.jdk25
else if pkgs ? jdk24_headless then pkgs.jdk24_headless
else if pkgs ? jdk24 then pkgs.jdk24
else pkgs.jdk;
graalvm = pkgs.graalvmPackages.graalvm-ce;
ghosttyVt = ghostty.packages.${system}.libghostty-vt;
in
{
default = pkgs.mkShell {
packages =
[ jdk pkgs.gradle graalvm ]
++ pkgs.lib.optional (pkgs ? jextract) pkgs.jextract;
GRAALVM_HOME = "${graalvm}";
JLIBGHOSTTY_LIBRARY =
if pkgs.stdenv.hostPlatform.isDarwin
then "${ghosttyVt}/lib/libghostty-vt.dylib"
else "${ghosttyVt}/lib/libghostty-vt.so";
};
});
};
}