Running Go with SDL2 (buildGoModule - help)

I want to work with the go module “go-sdl2” (link below) but I’m having trouble putting together a .nix-file. I’ve tried with the below but it fails.

Manual configuration:

{ pkgs ? (import <nixpkgs> {}).pkgs }:
with pkgs;
mkShell {
    buildInputs = [
    go
    SDL2                                                                       
    pkg-config
    ];
    #PKG_CONFIG_PATH=""$PKG_CONFIG_PATH{pkgs.sdl2}/lib
    #echo $PKG_CONFIG_PATH
    shellHook = ''
      LD_LIBRARY_PATH=${pkgs.SDL2}/lib
      echo $LD_LIBRARY_PATH
      go get -v github.com/veandco/go-sdl2/sdl
     '';
}

Steps to reproduce:
1: Copy the above code into shell.nix
2: run ‘nix-shell shell.nix’

Expected result: Entering the nix-shell succeeds in loading the module.
Received result:

go: downloading github.com/veandco/go-sdl2 v0.4.25
github.com/veandco/go-sdl2/sdl
# github.com/veandco/go-sdl2/sdl
In file included from _cgo_export.c:4:
events.go:4:10: fatal error: sdl_wrapper.h: No such file or directory
compilation terminated.

The issue is that ‘go get’ doesn’t look inside the gopath where the file is located (in my case at /home/$USER/go/github.com/veandco/go-sdl2@v0.4.24/sdl/").

How do I fix this and load the module? I looked into buildGoModule but ran into a wall quickly with sha256s and if it’s even relevant in my case (as I’m looking to import a module, not publish one).

Relevant links:
go-sdl2

Now the same with buildGoModule:

{ }: with import <nixpkgs>{};

buildGoModule {
  pname = "sdl2-go";
  version = "0.4.25";

  src = fetchFromGitHub {
    owner = "veandco";
    repo = "go-sdl2";
    rev = "v0.4.25";
    sha256 = "sha256-3t/f1pdNdlFn6sJ+/FOJuhgZdfRJFjWA+qsdvO4Cc8s=";
  };
  vendorSha256 = "sha256-yqYY6/4W6oCrAwl3RrZl09qeH2qj87R2cpdrVC3PLvo=";

  meta = with lib; {
    description = "go-sdl2 is SDL2 wrapped for Go users.";
    homepage = "https://github.com/veandco/go-sdl2";
  };
}

However, this leads into pkg-config errors:

# pkg-config --cflags  -- sdl2
Package sdl2 was not found in the pkg-config search path.
Perhaps you should add the directory containing `sdl2.pc'
to the PKG_CONFIG_PATH environment variable
No package 'sdl2' found
pkg-config: exit status 1

Still looking.

I think you’re misunderstanding how Go packaging works – with Go, you don’t package libraries, only applications.

What are you trying to do (develop a program that uses this library, package a program that uses it)?

@alcedie I think the issue is shellHook is ran before build applies any of the build inputs

for me

{ pkgs ? import <nixpkgs> { } }:
with pkgs;
mkShell {
  buildInputs = [
    go
    SDL2
    pkg-config
  ];
}
mkdir foo
cd foo
go mod init foo
go get github.com/veandco/go-sdl2/sdl

after that you can create main.go
with

package main

import "github.com/veandco/go-sdl2/sdl"

func main() {
	if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
		panic(err)
	}
	defer sdl.Quit()

	window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		800, 600, sdl.WINDOW_SHOWN)
	if err != nil {
		panic(err)
	}
	defer window.Destroy()

	surface, err := window.GetSurface()
	if err != nil {
		panic(err)
	}
	surface.FillRect(nil, 0)

	rect := sdl.Rect{0, 0, 200, 200}
	surface.FillRect(&rect, 0xffff0000)
	window.UpdateSurface()

	running := true
	for running {
		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event.(type) {
			case *sdl.QuitEvent:
				println("Quit")
				running = false
				break
			}
		}
	}
}
1 Like

I am trying to develop using it. Definitely misunderstood a few things there but thank you for the feedback.

That has worked, thank you kindly for taking the time and help out.