Go package compilation

I am trying to compile a go package using Nix.
When I run following commands, it compiles well.

nix-shell -p gccgo12
go build ./examples/simplecam/simplecam.go

Same thing I want to achieve through a derivation so that I can integrate it in my project, but it throws error. Below is my derivation to compile the package:

default.nix

with (import <nixpkgs> {});
stdenv.mkDerivation rec {
  pname = "simplecam";
  version = "v0.1";

  src = fetchFromGitHub {
    owner = "vladimirvivien";
    repo = "go4vl";
    rev = "018089c752cb092417d259661d6d8dc3874fc319";
    sha256 = "sha256-2JbTZCMhN98CJUzhjYTi/9rThobXVZ9pTTMDzGY1NVQ";
  };

  buildInputs = [
    gccgo12
  ];

  # Added export HOME=$(pwd) to overcome homeless-shelter
  buildPhase = ''
    runHook preBuild
    export HOME=$(pwd)
    gccgo build ./examples/simplecam/simplecam.go
    runHook postBuild

  '';

  installPhase = ''
    runHook preInstall

    #mkdir -p $out
    #cp ./simplecam $out

    runHook postConfigure
  '';
}

I am getting following error:

nix log /nix/store/i6vyplwjsr6pwcri29309fr361nb69kj-simplecam-v0.1.drv
@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking sources
unpacking source archive /nix/store/48ll3bqy5393wnng247a7gci877jwm10-source
source root is source
@nix { "action": "setPhase", "phase": "patchPhase" }
patching sources
@nix { "action": "setPhase", "phase": "configurePhase" }
configuring
no configure script, doing nothing
@nix { "action": "setPhase", "phase": "buildPhase" }
building
go1: warning: command-line option '-Wformat=1' is valid for C/C++/ObjC/ObjC++ but not for Go
go1: warning: command-line option '-Wformat-security' is valid for C/C++/ObjC/ObjC++ but not for Go
go1: warning: '-Werror=' argument '-Werror=format-security' is not valid for Go
./examples/simplecam/simplecam.go:12:47: error: import file 'github.com/vladimirvivien/go4vl/device' not found
   12 |         "github.com/vladimirvivien/go4vl/device"
      |                                               ^
./examples/simplecam/simplecam.go:13:45: error: import file 'github.com/vladimirvivien/go4vl/v4l2' not found
   13 |         "github.com/vladimirvivien/go4vl/v4l2"
      |                                             ^
./examples/simplecam/simplecam.go:48:52: error: missing ')'
   48 |                 device.WithPixFormat(v4l2.PixFormat{PixelFormat: v4l2.PixelFmtMJPEG, Width: 640, Height: 480}),
      |                                                    ^
./examples/simplecam/simplecam.go:46:24: error: reference to undefined name 'device'
   46 |         camera, err := device.Open(
      |                        ^
./examples/simplecam/simplecam.go:48:17: error: reference to undefined name 'device'
   48 |                 device.WithPixFormat(v4l2.PixFormat{PixelFormat: v4l2.PixelFmtMJPEG, Width: 640, Height: 480}),
      |                 ^
./examples/simplecam/simplecam.go:48:38: error: reference to undefined name 'v4l2'
   48 |                 device.WithPixFormat(v4l2.PixFormat{PixelFormat: v4l2.PixelFmtMJPEG, Width: 640, Height: 480}),
      |                                      ^

Can any body help me to fix this?

Use buildGoModule or buildGoApplication as described in the nixpkgs manual.

2 Likes

thank you, I am able to compile it using buildGoModule