Skip to content

Commit

Permalink
build(nix-shell): use musl libc and static build
Browse files Browse the repository at this point in the history
The non-buildx Dockerfile copies a binary built outside of the docker
build, which may be broken if the dependencies are not met.
This happens on nix-shell as the deps exist on /nix/store.
Get around this by building static binaries in the nix-shell.
We now depend on golang-alpine image and install musl-dev as well.

Signed-off-by: Tiago Castro <[email protected]>
  • Loading branch information
tiagolobocastro committed Jan 7, 2025
1 parent 1b78903 commit f0a4ac2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
12 changes: 7 additions & 5 deletions buildscripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,22 @@ XC_ARCH=$(go env GOARCH)
# Build!
echo "==> Building ${CTLNAME} using $(go version)... "

if [ -n "$IN_NIX_SHELL" ]; then
MAYBE_STATIC="-linkmode external -extldflags -static"
fi

GOOS="${XC_OS}"
GOARCH="${XC_ARCH}"
output_name=bin/"$PNAME"/"$GOOS"_"$GOARCH"/"$CTLNAME"

if [ "$GOOS" = "windows" ]; then
output_name+='.exe'
fi
env GOOS="$GOOS" GOARCH="$GOARCH" go build -ldflags \

if command -v musl-gcc; then
CC="musl-gcc"
fi

env CC="$CC" GOOS="$GOOS" GOARCH="$GOARCH" go build -ldflags \
"-X github.com/openebs/lvm-localpv/pkg/version.GitCommit=${GIT_COMMIT} \
-X main.CtlName='${CTLNAME}' \
-linkmode external -extldflags -static \
-X github.com/openebs/lvm-localpv/pkg/version.Version=${VERSION} \
-X github.com/openebs/lvm-localpv/pkg/version.VersionMeta=${VERSION_META}" \
-o "$output_name" \
Expand Down
6 changes: 3 additions & 3 deletions buildscripts/lvm-driver/Dockerfile.buildx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:1.19.9 AS build
FROM golang:1.19.9-alpine AS build

ARG BRANCH
ARG RELEASE_TAG
Expand All @@ -31,7 +31,7 @@ ENV GO111MODULE=on \

WORKDIR /go/src/github.com/openebs/lvm-localpv/

RUN apt-get update && apt-get install -y make git
RUN apk add make git bash gcc musl-dev

COPY go.mod go.sum ./
# Get dependencies - will also be cached if we won't change mod/sum
Expand All @@ -44,7 +44,7 @@ RUN make buildx.csi-driver
FROM alpine:3.18.4
RUN apk add --no-cache lvm2 lvm2-extra util-linux device-mapper
RUN apk add --no-cache btrfs-progs xfsprogs xfsprogs-extra e2fsprogs e2fsprogs-extra
RUN apk add --no-cache ca-certificates libc6-compat
RUN apk add --no-cache ca-certificates

ARG DBUILD_DATE
ARG DBUILD_REPO_URL
Expand Down
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pkgs.mkShell {
curl
cacert
util-linux
musl
] ++ pkgs.lib.optional (builtins.getEnv "IN_NIX_SHELL" == "pure") docker;
shellHook = ''
export GOPATH=$(pwd)/nix/.go
Expand Down

0 comments on commit f0a4ac2

Please sign in to comment.