Skip to content

Commit c10d3a0

Browse files
bors[bot]hswong3i
andauthored
Merge #2380
2380: [nix] Add nix derivation for static builds r=rhatdan a=hswong3i #### What type of PR is this? <!-- Please label this pull request according to what type of issue you are addressing, especially if this is a release targeted pull request. Uncomment only one `/kind <>` line, hit enter to put that in a new line, and remove leading whitespace from that line: --> > /kind feature #### What this PR does / why we need it: Similar PR goes for crun/conmon/libpod/cri-o/etc, too. Also see: - ~~containers/crun#372 - ~~containers/conmon#161 - containers/skopeo#932 - #2380 - containers/podman#6402 - cri-o/cri-o#3804 Static binaries: - [crun-0.13-linux-amd64](https://github.com/alvistack/crun/releases/download/0.13/crun-0.13-linux-amd64) - [conmon-v2.0.17-linux-amd64](https://github.com/alvistack/conmon/releases/download/v2.0.17/conmon-v2.0.17-linux-amd64) - [skopeo-v1.0.0-linux-amd64](https://github.com/alvistack/skopeo/releases/download/v1.0.0/skopeo-v1.0.0-linux-amd64) - [buildah-v1.14.9-linux-amd64](https://github.com/alvistack/buildah/releases/download/v1.14.9/buildah-v1.14.9-linux-amd64) - [podman-v1.9.3-linux-amd64](https://github.com/alvistack/libpod/releases/download/v1.9.3/podman-v1.9.3-linux-amd64) - [cri-o-v1.17.4-linux-amd64.tar.gz](https://github.com/alvistack/cri-o/releases/download/v1.17.4/cri-o-v1.17.4-linux-amd64.tar.gz) - [cri-o-v1.18.1-linux-amd64.tar.gz](https://github.com/alvistack/cri-o/releases/download/v1.18.1/cri-o-v1.18.1-linux-amd64.tar.gz) Ansible Roles: - https://github.com/alvistack/ansible-role-crun - https://github.com/alvistack/ansible-role-conmon - https://github.com/alvistack/ansible-role-skopeo - https://github.com/alvistack/ansible-role-buildah - https://github.com/alvistack/ansible-role-podman - https://github.com/alvistack/ansible-role-cri_o #### How to verify it ``` nix build -f nix/ ``` #### Which issue(s) this PR fixes: <!-- Automatically closes linked issue when PR is merged. Uncomment the following comment block and include the issue number or None on one line. Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`, or `None`. --> <!-- Fixes # or None --> #### Special notes for your reviewer: Here I skip the btrfs and lvm2 support for static binary, because: 1. btrfs will not support in CentOS 8 2. With skopeo experience both btrfs and lvm2 are not easy for compile as static binary Also see: - containers/podman#6402 (comment) #### Does this PR introduce a user-facing change? <!-- If no, just write `None` in the release-note block below. If yes, a release note is required: Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required". For more information on release notes please follow the kubernetes model: https://git.k8s.io/community/contributors/guide/release-notes.md --> ```release-note ``` Co-authored-by: Wong Hoi Sing Edison <hswong3i@gmail.com>
2 parents e5f71bf + 3a122aa commit c10d3a0

File tree

10 files changed

+98
-24
lines changed

10 files changed

+98
-24
lines changed

.cirrus.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ gce_instance:
157157

158158
env:
159159
matrix:
160-
CROSS_TARGET: darwin
160+
CROSS_TARGET: bin/buildah.darwin
161161

162162
setup_script: '${SCRIPT_BASE}/setup.sh |& ${_TIMESTAMP}'
163163
build_script: '${SCRIPT_BASE}/build.sh |& ${_TIMESTAMP}'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ tests/tools/build
66
Dockerfile*
77
!/tests/bud/*/Dockerfile*
88
*.swp
9+
result

Makefile

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,36 @@ CNI_COMMIT := $(shell sed -n 's;\tgithub.com/containernetworking/cni \([^ \n]*\)
3333
RUNC_COMMIT := v1.0.0-rc8
3434
LIBSECCOMP_COMMIT := release-2.3
3535

36-
EXTRALDFLAGS :=
37-
LDFLAGS := -ldflags '-X main.GitCommit=$(GIT_COMMIT) -X main.buildInfo=$(SOURCE_DATE_EPOCH) -X main.cniVersion=$(CNI_COMMIT)' $(EXTRALDFLAGS)
36+
EXTRA_LDFLAGS ?=
37+
LDFLAGS := -ldflags '-X main.GitCommit=$(GIT_COMMIT) -X main.buildInfo=$(SOURCE_DATE_EPOCH) -X main.cniVersion=$(CNI_COMMIT) $(EXTRA_LDFLAGS)'
3838
SOURCES=*.go imagebuildah/*.go bind/*.go chroot/*.go cmd/buildah/*.go docker/*.go pkg/blobcache/*.go pkg/cli/*.go pkg/parse/*.go util/*.go
3939

4040
LINTFLAGS ?=
4141

42-
all: buildah imgtype docs
42+
all: bin/buildah bin/imgtype docs
4343

44-
.PHONY: static
45-
static: $(SOURCES)
46-
$(MAKE) SECURITYTAGS="$(SECURITYTAGS)" STORAGETAGS=$(STATIC_STORAGETAGS) EXTRALDFLAGS='-ldflags "-extldflags '-static'"' BUILDAH=buildah.static binary
44+
nixpkgs:
45+
@nix run -f channel:nixpkgs-unstable nix-prefetch-git -c nix-prefetch-git \
46+
--no-deepClone https://github.com/nixos/nixpkgs > nix/nixpkgs.json
4747

48-
.PHONY: binary
49-
binary: $(SOURCES)
50-
$(GO_BUILD) $(LDFLAGS) -o $(BUILDAH) $(BUILDFLAGS) ./cmd/buildah
48+
.PHONY: bin/buildah
49+
bin/buildah: $(SOURCES)
50+
$(GO_BUILD) $(LDFLAGS) -o $@ $(BUILDFLAGS) ./cmd/buildah
5151

52-
buildah: binary
52+
.PHONY: buildah
53+
buildah: bin/buildah
5354

54-
darwin:
55-
GOOS=darwin $(GO_BUILD) $(LDFLAGS) -o buildah.darwin -tags "containers_image_openpgp" ./cmd/buildah
55+
.PHONY: bin/buildah.darwin
56+
bin/buildah.darwin:
57+
GOOS=darwin $(GO_BUILD) $(LDFLAGS) -o $@ -tags "containers_image_openpgp" ./cmd/buildah
5658

57-
imgtype: *.go docker/*.go util/*.go tests/imgtype/imgtype.go
58-
$(GO_BUILD) $(LDFLAGS) -o imgtype $(BUILDFLAGS) ./tests/imgtype/imgtype.go
59+
.PHONY: bin/imgtype
60+
bin/imgtype: *.go docker/*.go util/*.go tests/imgtype/imgtype.go
61+
$(GO_BUILD) $(LDFLAGS) -o $@ $(BUILDFLAGS) ./tests/imgtype/imgtype.go
5962

6063
.PHONY: clean
6164
clean:
62-
$(RM) -r buildah imgtype build buildah.static buildah.darwin tests/testreport/testreport
65+
$(RM) -r bin tests/testreport/testreport
6366
$(MAKE) -C docs clean
6467

6568
.PHONY: docs
@@ -105,7 +108,7 @@ install.cni.sudo: gopath
105108

106109
.PHONY: install
107110
install:
108-
install -D -m0755 buildah $(DESTDIR)/$(BINDIR)/buildah
111+
install -D -m0755 bin/buildah $(DESTDIR)/$(BINDIR)/buildah
109112
$(MAKE) -C docs install
110113

111114
.PHONY: uninstall

contrib/cirrus/build.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ else
2020
mkdir -p bin
2121
if [[ -z "$CROSS_TARGET" ]]
2222
then
23-
ln -v buildah bin/buildah
2423
showrun make install PREFIX=/usr
2524
showrun ./bin/buildah info
2625
else
27-
ln -v buildah.${CROSS_TARGET} bin/buildah
26+
ln -v ${CROSS_TARGET} bin/buildah
2827
fi
2928
fi

contrib/cirrus/logcollector.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ case $1 in
1717
df) showrun df -lhTx tmpfs ;;
1818
journal) showrun journalctl -b ;;
1919
podman) showrun podman system info ;;
20-
buildah_version) showrun $GOSRC/buildah version;;
21-
buildah_info) showrun $GOSRC/buildah info;;
20+
buildah_version) showrun $GOSRC/bin/buildah version;;
21+
buildah_info) showrun $GOSRC/bin/buildah info;;
2222
packages)
2323
# These names are common to Fedora and Ubuntu
2424
PKG_NAMES=(\

nix/default.nix

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
libseccomp = (static pkg.libseccomp);
10+
glib = pkg.glib.overrideAttrs(x: {
11+
outputs = [ "bin" "out" "dev" ];
12+
mesonFlags = [
13+
"-Ddefault_library=static"
14+
"-Ddevbindir=${placeholder ''dev''}/bin"
15+
"-Dgtk_doc=false"
16+
"-Dnls=disabled"
17+
];
18+
});
19+
};
20+
};
21+
});
22+
23+
static = pkg: pkg.overrideAttrs(x: {
24+
configureFlags = (x.configureFlags or []) ++
25+
[ "--without-shared" "--disable-shared" ];
26+
dontDisableStatic = true;
27+
enableSharedExecutables = false;
28+
enableStatic = true;
29+
});
30+
31+
self = with pkgs; buildGoPackage rec {
32+
name = "buildah";
33+
src = ./..;
34+
goPackagePath = "github.com/containers/buildah";
35+
doCheck = false;
36+
enableParallelBuilding = true;
37+
nativeBuildInputs = [ git installShellFiles pkg-config ];
38+
buildInputs = [ glib glibc glibc.static gpgme libapparmor libassuan libgpgerror libseccomp libselinux ];
39+
prePatch = ''
40+
export LDFLAGS='-s -w -static-libgcc -static'
41+
export EXTRA_LDFLAGS='-s -w -linkmode external -extldflags "-static -lm"'
42+
export BUILDTAGS='static netgo apparmor selinux seccomp exclude_graphdriver_btrfs exclude_graphdriver_devicemapper'
43+
'';
44+
buildPhase = ''
45+
pushd go/src/${goPackagePath}
46+
patchShebangs .
47+
make bin/buildah
48+
'';
49+
installPhase = ''
50+
install -Dm755 bin/buildah $out/bin/buildah
51+
'';
52+
};
53+
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": "78e324d2726127828a15f87a75b4d3199a8955ec",
4+
"date": "2020-06-16T18:23:14-07:00",
5+
"path": "/nix/store/bwhp0061k3fk00j8fskpfak261jdcjl6-nixpkgs",
6+
"sha256": "1j58aa9ngdmvbnds4x4a497nynj390dzqyb5yrvmhjc7k9anq6jm",
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

tests/e2e/buildah_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func CreateTempDirInTempDir() (string, error) {
9595
func BuildahCreate(tempDir string) BuildAhTest {
9696
cwd, _ := os.Getwd()
9797

98-
buildAhBinary := filepath.Join(cwd, "../../buildah")
98+
buildAhBinary := filepath.Join(cwd, "../../bin/buildah")
9999
if os.Getenv("BUILDAH_BINARY") != "" {
100100
buildAhBinary = os.Getenv("BUILDAH_BINARY")
101101
}

tests/helpers.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

3-
BUILDAH_BINARY=${BUILDAH_BINARY:-$(dirname ${BASH_SOURCE})/../buildah}
4-
IMGTYPE_BINARY=${IMGTYPE_BINARY:-$(dirname ${BASH_SOURCE})/../imgtype}
3+
BUILDAH_BINARY=${BUILDAH_BINARY:-$(dirname ${BASH_SOURCE})/../bin/buildah}
4+
IMGTYPE_BINARY=${IMGTYPE_BINARY:-$(dirname ${BASH_SOURCE})/../bin/imgtype}
55
TESTSDIR=${TESTSDIR:-$(dirname ${BASH_SOURCE})}
66
STORAGE_DRIVER=${STORAGE_DRIVER:-vfs}
77
PATH=$(dirname ${BASH_SOURCE})/..:${PATH}

0 commit comments

Comments
 (0)