Building RPM of go software

I’ve been cheating on my packages for acme-dns–I’ve just been taking the developer’s pre-compiled binaries and packaging them into RPMs. But that really isn’t the concept of RPMs; they’re intended to be built from source. I haven’t been able to find much in the way of documentation on how to do this with go, and I’m probably in over my head, but that hasn’t stopped me before… @mark_nl has shared some very helpful information that gets me to the point of building the app, but I’m having trouble translating that into building a RPM. Here’s what I’ve got so far:

  • Build the .spec file. Make sure to include BuildRequires: go. Here’s what I have in mine, in what I think is the relevant part:
%prep
%setup -c -q -n "acme-dns-%{pkg_version}"
cp %{SOURCE1} %{_builddir}/acme-dns-%{pkg_version}
mv %{_builddir}/acme-dns-%{pkg_version}/acme-dns-%{pkg_version}/* %{_builddir}/acme-dns-%{pkg_version}/
mv %{_builddir}/acme-dns-%{pkg_version}/vendor/ %{_builddir}/acme-dns-%{pkg_version}/src/
export GOPATH=%{_builddir}/acme-dns-%{pkg_version}/

%build
go build -v -o acme-dns
  • Put the .spec file and the source files into a directory
  • Use mock to build the .srpm: mock -r nethserver-7-x86_64 --buildsrpm --spec <specfile> --sources . --resultdir . This finishes without apparent issue.
  • Then use mock to build the binary RPM: mock -r nethserver-7-x86_64 --rebuild <.src.rpm> --resultdir . This is where I have a problem:
+ go build -v -o acme-dns
util.go:12:2: cannot find package "github.com/BurntSushi/toml" in any of:
	/usr/lib/golang/src/github.com/BurntSushi/toml (from $GOROOT)
	/builddir/go/src/github.com/BurntSushi/toml (from $GOPATH)
acmetxt.go:7:2: cannot find package "github.com/google/uuid" in any of:
	/usr/lib/golang/src/github.com/google/uuid (from $GOROOT)
	/builddir/go/src/github.com/google/uuid (from $GOPATH)
api.go:9:2: cannot find package "github.com/julienschmidt/httprouter" in any of:
	/usr/lib/golang/src/github.com/julienschmidt/httprouter (from $GOROOT)
	/builddir/go/src/github.com/julienschmidt/httprouter (from $GOPATH)
db.go:13:2: cannot find package "github.com/lib/pq" in any of:
	/usr/lib/golang/src/github.com/lib/pq (from $GOROOT)
	/builddir/go/src/github.com/lib/pq (from $GOPATH)
db.go:14:2: cannot find package "github.com/mattn/go-sqlite3" in any of:
	/usr/lib/golang/src/github.com/mattn/go-sqlite3 (from $GOROOT)
	/builddir/go/src/github.com/mattn/go-sqlite3 (from $GOPATH)
dns.go:5:2: cannot find package "github.com/miekg/dns" in any of:
	/usr/lib/golang/src/github.com/miekg/dns (from $GOROOT)
	/builddir/go/src/github.com/miekg/dns (from $GOPATH)
main.go:15:2: cannot find package "github.com/rs/cors" in any of:
	/usr/lib/golang/src/github.com/rs/cors (from $GOROOT)
	/builddir/go/src/github.com/rs/cors (from $GOPATH)
acmetxt.go:8:2: cannot find package "github.com/sirupsen/logrus" in any of:
	/usr/lib/golang/src/github.com/sirupsen/logrus (from $GOROOT)
	/builddir/go/src/github.com/sirupsen/logrus (from $GOPATH)
main.go:17:2: cannot find package "golang.org/x/crypto/acme/autocert" in any of:
	/usr/lib/golang/src/golang.org/x/crypto/acme/autocert (from $GOROOT)
	/builddir/go/src/golang.org/x/crypto/acme/autocert (from $GOPATH)
db.go:16:2: cannot find package "golang.org/x/crypto/bcrypt" in any of:
	/usr/lib/golang/src/golang.org/x/crypto/bcrypt (from $GOROOT)
	/builddir/go/src/golang.org/x/crypto/bcrypt (from $GOPATH)

This package appears to be trying to pull in other packages from GitHub, and isn’t able to do so, resulting in build failure. If I set up a mock environment and run the go build command inside it, I’m able to build this package without an issue. Where should I be looking to fix this?

1 Like

OK, solved this problem. Replaced these lines in the .spec file:

export GOPATH=%{_builddir}/acme-dns-%{pkg_version}/
go build -v -o acme-dns

…with this one in %build:

GOPATH=%{_builddir}/acme-dns-%{pkg_version}/ go build -v -o acme-dns

Still TBD whether the resulting RPM works, but it’s definite progress.

2 Likes