Skip to content

Commit 1a93463

Browse files
committed
Add nix derivation for static builds
1 parent a2c1d46 commit 1a93463

File tree

5 files changed

+76
-12
lines changed

5 files changed

+76
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.1
22
/layers-*
33
/skopeo
4+
result
45

56
# ignore JetBrains IDEs (GoLand) config folder
6-
.idea
7+
.idea

Makefile

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ CONTAINER_RUN := $(CONTAINER_CMD) "$(IMAGE)"
6262

6363
GIT_COMMIT := $(shell git rev-parse HEAD 2> /dev/null || true)
6464

65+
EXTRA_LDFLAGS ?=
66+
LDFLAGS := -ldflags '-X main.gitCommit=${GIT_COMMIT} $(EXTRA_LDFLAGS)'
67+
6568
MANPAGES_MD = $(wildcard docs/*.md)
6669
MANPAGES ?= $(MANPAGES_MD:%.md=%)
6770

@@ -85,7 +88,7 @@ help:
8588
@echo
8689
@echo " * 'install' - Install binaries and documents to system locations"
8790
@echo " * 'binary' - Build skopeo with a container"
88-
@echo " * 'binary-local' - Build skopeo locally"
91+
@echo " * 'bin/skopeo' - Build skopeo locally"
8992
@echo " * 'test-unit' - Execute unit tests"
9093
@echo " * 'test-integration' - Execute integration tests"
9194
@echo " * 'validate' - Verify whether there is no conflict and all Go source files have been formatted, linted and vetted"
@@ -98,19 +101,18 @@ help:
98101
binary: cmd/skopeo
99102
${CONTAINER_RUNTIME} build ${BUILD_ARGS} -f Dockerfile.build -t skopeobuildimage .
100103
${CONTAINER_RUNTIME} run --rm --security-opt label=disable -v $$(pwd):/src/github.com/containers/skopeo \
101-
skopeobuildimage make binary-local $(if $(DEBUG),DEBUG=$(DEBUG)) BUILDTAGS='$(BUILDTAGS)'
104+
skopeobuildimage make bin/skopeo $(if $(DEBUG),DEBUG=$(DEBUG)) BUILDTAGS='$(BUILDTAGS)'
102105

103-
binary-static: cmd/skopeo
104-
${CONTAINER_RUNTIME} build ${BUILD_ARGS} -f Dockerfile.build -t skopeobuildimage .
105-
${CONTAINER_RUNTIME} run --rm --security-opt label=disable -v $$(pwd):/src/github.com/containers/skopeo \
106-
skopeobuildimage make binary-local-static $(if $(DEBUG),DEBUG=$(DEBUG)) BUILDTAGS='$(BUILDTAGS)'
106+
# Update nix/nixpkgs.json its latest master commit
107+
.PHONY: nixpkgs
108+
nixpkgs:
109+
@nix run -f channel:nixpkgs-unstable nix-prefetch-git -c nix-prefetch-git \
110+
--no-deepClone https://github.com/nixos/nixpkgs > nix/nixpkgs.json
107111

108112
# Build w/o using containers
109-
binary-local:
110-
$(GPGME_ENV) $(GO) build $(MOD_VENDOR) ${GO_DYN_FLAGS} -ldflags "-X main.gitCommit=${GIT_COMMIT}" -gcflags "$(GOGCFLAGS)" -tags "$(BUILDTAGS)" -o skopeo ./cmd/skopeo
111-
112-
binary-local-static:
113-
$(GPGME_ENV) $(GO) build $(MOD_VENDOR) -ldflags "-extldflags \"-static\" -X main.gitCommit=${GIT_COMMIT}" -gcflags "$(GOGCFLAGS)" -tags "$(BUILDTAGS)" -o skopeo ./cmd/skopeo
113+
.PHONY: bin/skopeo
114+
bin/skopeo:
115+
$(GPGME_ENV) $(GO) build $(MOD_VENDOR) ${GO_DYN_FLAGS} ${LDFLAGS} -gcflags "$(GOGCFLAGS)" -tags "$(BUILDTAGS)" -o $@ ./cmd/skopeo
114116

115117
build-container:
116118
${CONTAINER_RUNTIME} build ${BUILD_ARGS} -t "$(IMAGE)" .

nix/default.nix

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{ system ? builtins.currentSystem }:
2+
let
3+
pkgs = (import ./nixpkgs.nix {
4+
config = {
5+
packageOverrides = pkg: {
6+
gpgme = (static pkg.gpgme);
7+
libassuan = (static pkg.libassuan);
8+
libgpgerror = (static pkg.libgpgerror);
9+
};
10+
};
11+
});
12+
13+
static = pkg: pkg.overrideAttrs(x: {
14+
configureFlags = (x.configureFlags or []) ++
15+
[ "--without-shared" "--disable-shared" ];
16+
dontDisableStatic = true;
17+
enableSharedExecutables = false;
18+
enableStatic = true;
19+
});
20+
21+
self = with pkgs; buildGoPackage rec {
22+
name = "skopeo";
23+
src = ./..;
24+
goPackagePath = "github.com/containers/skopeo";
25+
doCheck = false;
26+
enableParallelBuilding = true;
27+
nativeBuildInputs = [ git go-md2man installShellFiles makeWrapper pkg-config ];
28+
buildInputs = [ glibc glibc.static gpgme libassuan libgpgerror ];
29+
prePatch = ''
30+
export LDFLAGS='-static-libgcc -static -s -w'
31+
export EXTRA_LDFLAGS='-s -w -linkmode external -extldflags "-static -lm"'
32+
export BUILDTAGS='static netgo exclude_graphdriver_btrfs exclude_graphdriver_devicemapper'
33+
'';
34+
buildPhase = ''
35+
pushd go/src/${goPackagePath}
36+
patchShebangs .
37+
make bin/skopeo
38+
'';
39+
installPhase = ''
40+
install -Dm755 bin/skopeo $out/bin/skopeo
41+
'';
42+
};
43+
in self

nix/nixpkgs.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"url": "https://github.com/nixos/nixpkgs",
3+
"rev": "0f114432d4a9399e0b225d5be1599c7ebc5e2772",
4+
"date": "2020-05-29T19:54:08-05:00",
5+
"path": "/nix/store/ds31sjj3ppsk0xclkficx9p3w6qslmdc-nixpkgs",
6+
"sha256": "1qd2dlc5dk98y0xdahv9k72ibv5dsy10jg25xqvj38sadxbs3g0j",
7+
"fetchSubmodules": false,
8+
"deepClone": false,
9+
"leaveDotGit": false
10+
}

nix/nixpkgs.nix

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let
2+
json = builtins.fromJSON (builtins.readFile ./nixpkgs.json);
3+
nixpkgs = import (builtins.fetchTarball {
4+
name = "nixos-unstable";
5+
url = "${json.url}/archive/${json.rev}.tar.gz";
6+
inherit (json) sha256;
7+
});
8+
in nixpkgs

0 commit comments

Comments
 (0)