Fork me on GitHub
#graalvm
<
2021-06-19
>
heyarne16:06:12

Hey there 🙂 Is anybody here who could help me understand how graalvm and nix interact? I'm trying to build https://github.com/theiceshelf/firn/ from nix and it seems to succeed, but the resulting binary gives me an error when trying to execute it:

Error: Could not find or load main class firn.core
Caused by: java.lang.ClassNotFoundException: firn.core

heyarne16:06:21

i'd also gladyly use pre-built binaries, but executing any graalvm bin on nixos aborts with "file does not exist or is not an executable". if somebody knows how to fix that, that would be amazing as well

Karol Wójcik16:06:49

chmod +x <executable>?

heyarne16:06:40

Unfortunately it's not that simple

heyarne16:06:04

Meaning that I checked that already and I still get the same message

borkdude17:06:52

@U9N6R1DM4 This is probably because the executable you build/use is glibc-linked

heyarne17:06:54

The exact error is

Failed to execute process '../firn-test/firn'. Reason:
The file '../firn-test/firn' does not exist or could not be executed.
It's not limited to firn though, when trying out bootleg I hit the same error

borkdude17:06:19

Try the babashka static binary, it probably works

borkdude17:06:30

It is statically linked against musl

borkdude17:06:53

The Error: Could not find or load main class firn.core is probably due to dynamic requires or so

heyarne17:06:36

The same thing happens with the most recent prebuilt babashka bin

borkdude17:06:00

@U9N6R1DM4 The -> static <- pre-built bin?

heyarne17:06:22

Ah no, you're right 🙂

heyarne17:06:44

Can I see how exactly that's built?

borkdude17:06:52

sure, babashka is open source.

heyarne17:06:17

Found the install file already

heyarne17:06:30

And thanks for babashka of course

borkdude17:06:46

For static linking with musl, you also might want to read: https://github.com/lread/clj-graal-docs#static-linking-with-musl

heyarne17:06:52

Yes, that's exactly what I was looking for

borkdude17:06:35

Ah I see firn is already made to be compatible for graalvm

heyarne17:06:19

Yes, exactly. But the provided binary raises the error described above

heyarne17:06:27

Which I'm trying to wrap my head around

borkdude17:06:20

the executable not found error I already explained: this is because it's linked against glibc

borkdude17:06:33

and glibc isn't in a standard location in nixos

borkdude17:06:09

but perhaps it would work if you build it locally though, hmm

borkdude17:06:18

perhaps @UFDRD93RR can help you, he also uses nixos

kokada17:06:56

I am trying to understand the exactly problem here? Is babashka static binary also having this issue of "file not found" :thinking_face: ?

kokada17:06:22

BTW, if firn is having this issue it is probably using dynamic link

kokada17:06:27

You can use ldd to check this

borkdude17:06:42

the bb static should work.

ericdallo17:06:06

Graalvm non static binaries doesn't work on NixOS unless you compile the image on your machine

borkdude17:06:08

but if you build firn locally in nixos, wouldn't it work with glibc as well?

ericdallo17:06:19

The bb static works fine for me and @UFDRD93RR on nixos

kokada17:06:12

BTW

ldd firn
ldd: warning: you do not have execution permission for `./firn'
	linux-vdso.so.1 (0x00007ffe6a9cb000)
	libpthread.so.0 => /nix/store/ikl21vjfq900ccbqg1xasp83kadw6q8y-glibc-2.32-46/lib/libpthread.so.0 (0x00007f2fb26f4000)
	libdl.so.2 => /nix/store/ikl21vjfq900ccbqg1xasp83kadw6q8y-glibc-2.32-46/lib/libdl.so.2 (0x00007f2fb26ef000)
	libz.so.1 => not found
	libc.so.6 => /nix/store/ikl21vjfq900ccbqg1xasp83kadw6q8y-glibc-2.32-46/lib/libc.so.6 (0x00007f2fb252e000)
	/lib64/ld-linux-x86-64.so.2 => /nix/store/ikl21vjfq900ccbqg1xasp83kadw6q8y-glibc-2.32-46/lib64/ld-linux-x86-64.so.2 (0x00007f2fb63d2000)

kokada17:06:22

> but if you build firn locally in nixos, wouldn't it work with glibc as well? It works as long the PATH to glibc doesn't change (but it changes frequently, for example, if any library that glibc depends is updated, the hash on the path is updated)

ericdallo17:06:16

so, for graalvm works on nixos I always need to go into a shell with gcc, something like this should work:

nix-shell -p 'gcc'

ericdallo17:06:30

and then graalvm native-image compile works

ericdallo17:06:57

this is because the gcc build input is missing from the graalvm nixpkgs derivation

kokada17:06:10

> so, for graalvm works on nixos I always need to go into a shell with gcc, something like this should work: This may work with luck, but it is not necessary correct

kokada17:06:28

Like I said, a rebuild on gcc may broke the binary

kokada17:06:39

Anyway, the root cause of the issue seems to be the fact that firn is not statically compiled

kokada17:06:04

So maybe ask the maintainer if it could be static compiled :thinking_face: ? Or package it on nixpkgs

borkdude17:06:27

probably a PR which makes musl the default on linux is the easiest route

borkdude17:06:59

there's also this patch program for nixos right

borkdude17:06:08

to make it work with the local glibc

kokada17:06:07

Yeah, patchelf

kokada17:06:15

But looking at the ldd, it is actually missing libz

kokada17:06:33

$ ldd firn
ldd: warning: you do not have execution permission for `./firn'
	linux-vdso.so.1 (0x00007ffe6a9cb000)
	libpthread.so.0 => /nix/store/ikl21vjfq900ccbqg1xasp83kadw6q8y-glibc-2.32-46/lib/libpthread.so.0 (0x00007f2fb26f4000)
	libdl.so.2 => /nix/store/ikl21vjfq900ccbqg1xasp83kadw6q8y-glibc-2.32-46/lib/libdl.so.2 (0x00007f2fb26ef000)
	libz.so.1 => not found
	libc.so.6 => /nix/store/ikl21vjfq900ccbqg1xasp83kadw6q8y-glibc-2.32-46/lib/libc.so.6 (0x00007f2fb252e000)
	/lib64/ld-linux-x86-64.so.2 => /nix/store/ikl21vjfq900ccbqg1xasp83kadw6q8y-glibc-2.32-46/lib64/ld-linux-x86-64.so.2 (0x00007f2fb63d2000)

kokada17:06:42

Only libz.so.1 is not found for firn

heyarne18:06:47

Thanks for helping everyone 🙂 That was very informative

heyarne18:06:40

… static compilation with musl would still make the resulting binary significantly smaller, i guess?

borkdude18:06:27

with babashka it's only a 5mb difference or so

heyarne18:06:59

ah yes, i mean vs using the patchelf'd version

heyarne18:06:28

are you talking about dynamically linked babashka vs statically linked against musl?

borkdude18:06:57

yes. you can see the binary sizes by going into the #babashka-circleci-buildss channel