I am trying to package hover. It is a program that allows you to build Flutter programs for desktop, and it uses Go for it.
The issue is, this program and the programs that it compiles depends on CGO dependencies (so it needs to link with C libraries, basically X11 and OpenGL). Those libraries are linked dynamically (so you can’t use CGO_ENABLED=0 to create static binaries).
The hover comand itself works fine, but when you try to use hover run or hover build linux, you get:
$ hover build linux nix-shell
hover: Cleaning the build directory
hover: Missing config: file hover.yaml not found: open go/hover.yaml: no such file or directory
hover: ⚠ The go-flutter project tries to stay compatible with the beta channel of Flutter.
hover: ⚠ It's advised to use the beta channel: `flutter channel beta`
hover: Building flutter bundleFont subetting is not supported in debug mode. The --tree-shake-icons flag will be ignored.
hover: Using engine from cache
hover: Checking available release on Github
hover: Compiling 'go-flutter' and plugins
go: downloading github.com/pkg/errors v0.9.1
go: downloading github.com/go-flutter-desktop/plugins/shared_preferences v0.4.0
go: downloading github.com/go-flutter-desktop/go-flutter v0.41.2
go: downloading github.com/lucasmafra/go-flutter-share v0.0.0-20200213183323-6ca7e6570bca
go: downloading github.com/syndtr/goleveldb v1.0.0
go: downloading github.com/mitchellh/go-homedir v1.1.0
go: downloading golang.org/x/text v0.3.3
go: downloading github.com/go-gl/glfw v0.0.0-20191125211704-12ad95a8df72
go: downloading github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7
go: downloading github.com/Xuanwo/go-locale v0.3.0
go: downloading github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db
go: downloading github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2
# github.com/app/app/desktop/cmd
/nix/store/flb7pvdk13rm0aqz9ak5naxp4hwbvwsj-go-1.14.4/share/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/nix/store/hrkc2sf2883l16d5yq3zg0y339kfw4xv-binutils-2.31.1/bin/ld: cannot find -lGL
/nix/store/hrkc2sf2883l16d5yq3zg0y339kfw4xv-binutils-2.31.1/bin/ld: cannot find -lX11
/nix/store/hrkc2sf2883l16d5yq3zg0y339kfw4xv-binutils-2.31.1/bin/ld: cannot find -lXrandr
/nix/store/hrkc2sf2883l16d5yq3zg0y339kfw4xv-binutils-2.31.1/bin/ld: cannot find -lXxf86vm
/nix/store/hrkc2sf2883l16d5yq3zg0y339kfw4xv-binutils-2.31.1/bin/ld: cannot find -lXi
/nix/store/hrkc2sf2883l16d5yq3zg0y339kfw4xv-binutils-2.31.1/bin/ld: cannot find -lXcursor
collect2: error: ld returned 1 exit status
However, this works:
$ nix-shell -p libGL pkgconfig glfw xorg.libX11 xorg.libXrandr xorg.libXinerama xorg.libXxf86vm xorg.libXi xorg.libXcursor xorg.libXext --run "hover run"
nix-shell -p libGL pkgconfig glfw xorg.libX11 xorg.libXrandr xorg.libXinerama xorg.libXxf86vm xorg.libXi xorg.libXcursor xorg.libXext --run "hover run"
hover: Missing config: file hover.yaml not found: open go/hover.yaml: no such file or directory
hover: ⚠ The go-flutter project tries to stay compatible with the beta channel of Flutter.
hover: ⚠ It's advised to use the beta channel: `flutter channel beta`
hover: Building flutter bundle
Font subetting is not supported in debug mode. The --tree-shake-icons flag will be ignored.
hover: Using engine from cache
hover: Compiling 'go-flutter' and plugins
hover: Successfully compiled executable binary for linux
hover: Build finished, starting app...
hover: Running app in debug mode
So I think the issue is that GCC or something else inside hover needs to have access to X11/OpenGL libraries, but I can’t figure out how to do it.
Sorry, not working actually. Testing inside a nix-shell --pure and got this:
[nix-shell:~/dev/nu/mini-meta-repo]$ hover run
hover: Missing config: file hover.yaml not found: open go/hover.yaml: no such file or directory
hover: ⚠ The go-flutter project tries to stay compatible with the beta channel of Flutter.
hover: ⚠ It's advised to use the beta channel: `flutter channel beta`
hover: Building flutter bundle
Font subetting is not supported in debug mode. The --tree-shake-icons flag will be ignored.
hover: Using engine from cache
hover: Compiling 'go-flutter' and plugins
github.com/go-gl/glfw/v3.3/glfw
github.com/go-gl/gl/v3.3-core/gl
# pkg-config --cflags -- gl gl
pkg-config: exec: "pkg-config": executable file not found in $PATH
# github.com/go-gl/glfw/v3.3/glfw
In file included from ./glfw/src/internal.h:188,
from ./glfw/src/context.c:30,
from ../../../../go/pkg/mod/github.com/go-gl/glfw/v3.3/glfw@v0.0.0-20200707082815-5321531c36a2/c_glfw.go:4:
./glfw/src/x11_platform.h:33:10: fatal error: X11/Xlib.h: No such file or directory
33 | #include <X11/Xlib.h>
| ^~~~~~~~~~~~
compilation terminated.
hover: Go build failed: exit status 2
Sadly it didn’t make any difference for the go issue. I think for some reason pkgs.go_1_14 is broken, because this is the PATH that I got when I exported it in some other test: /nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-go-1.14.4/bin
But the major issue I have for now is that I can make it work in nix-shell --pure, but using it in environment.systemPackages as a overlay does not.
The difference is that I needed to wrap CPATH, or I would get a ./glfw/src/x11_platform.h:33:10: fatal error: X11/Xlib.h: No such file or directory. This works well in nix-shell --pure, but trying it in a overlay results in the same error.
Maybe this will help, this is what hover ends doing when we call hover build linux:
So it ends up calling the go binary in the system, that AFAIK ends up calling gcc to compile the C code in one of the dependencies of hover. In the end, hover is basically a wrapper for go command to pass some special instructions to build a Go program.
This is why that I think wrapping hover executable with CPATH and LD_LIBRARY_PATH works, because when hover calls go (that eventually calls gcc) everything will be available in the environment variables. But I don’t have a clue why outside a nix-shell --pure this doesn’t work anymore.
BTW, calling nix-shell -p libGL pkgconfig glfw xorg.libX11 xorg.libXrandr xorg.libXinerama xorg.libXxf86vm xorg.libXi xorg.libXcursor xorg.libXext --run "hover run" works, so it does seem that something is missing outside a nix-shell.
I am only using buildFHSUserEnv as a workaround here to make sure that gcc can see the X11 headers. If someone knows a better solution please say so, because I am out of ideas.