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 = [
    #echo $PKG_CONFIG_PATH
    shellHook = ''
      echo $LD_LIBRARY_PATH
      go get -v

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 v0.4.25
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/").

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:

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 = "";

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 = [
mkdir foo
cd foo
go mod init foo
go get

after that you can create main.go

package main

import ""

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

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

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

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

	running := true
	for running {
		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event.(type) {
			case *sdl.QuitEvent:
				running = false
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.