-
Notifications
You must be signed in to change notification settings - Fork 16
/
flake.nix
219 lines (205 loc) · 7.8 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
flake-utils.url = "github:numtide/flake-utils";
gomod2nix.url = "github:nix-community/gomod2nix";
gomod2nix.inputs.nixpkgs.follows = "nixpkgs";
gomod2nix.inputs.flake-utils.follows = "flake-utils";
};
outputs = {
self,
nixpkgs,
flake-utils,
gomod2nix,
...
}:
flake-utils.lib.eachDefaultSystem
(
system: let
pkgs = import nixpkgs {
inherit system;
overlays = [
(import "${gomod2nix}/overlay.nix")
];
};
service_names = ["kardinal-manager" "kardinal-cli" "redis-proxy-overlay"];
architectures = ["amd64" "arm64"];
imageRegistry = "kurtosistech";
matchingContainerArch =
if builtins.match "aarch64-.*" system != null
then "arm64"
else if builtins.match "x86_64-.*" system != null
then "amd64"
else throw "Unsupported system type: ${system}";
mergeContainerPackages = acc: service:
pkgs.lib.recursiveUpdate acc {
packages."${service}-container" = self.containers.${system}.${service}.${matchingContainerArch};
};
multiPlatformDockerPusher = acc: service:
pkgs.lib.recursiveUpdate acc {
packages."publish-${service}-container" = let
name = "${imageRegistry}/${service}";
tagBase = "latest";
images =
map (
arch: rec {
inherit arch;
image = self.containers.${system}.${service}.${arch};
tag = "${tagBase}-${arch}";
}
)
architectures;
loadAndPush = builtins.concatStringsSep "\n" (pkgs.lib.concatMap
({
arch,
image,
tag,
}: [
"$docker load -i ${image}"
"$docker push ${name}:${tag}"
])
images);
imageNames =
builtins.concatStringsSep " "
(map ({
arch,
image,
tag,
}: "${name}:${tag}")
images);
in
pkgs.writeTextFile {
inherit name;
text = ''
#!${pkgs.stdenv.shell}
set -euxo pipefail
docker=${pkgs.docker}/bin/docker
${loadAndPush}
$docker manifest create --amend ${name}:${tagBase} ${imageNames}
$docker manifest push ${name}:${tagBase}
'';
executable = true;
destination = "/bin/push";
};
};
systemOutput = rec {
devShells.default = pkgs.callPackage ./shell.nix {
inherit pkgs;
};
packages.kardinal-cli = pkgs.callPackage ./kardinal-cli/default.nix {
inherit pkgs;
};
packages.kardinal-manager = pkgs.callPackage ./kardinal-manager/default.nix {
inherit pkgs;
};
packages.redis-proxy-overlay = pkgs.callPackage ./sidecars/redis-overlay-service/default.nix {
inherit pkgs;
};
packages.cli-kontrol-api = pkgs.callPackage ./libs/cli-kontrol-api/default.nix {
inherit pkgs;
};
packages.go-tidy-all = pkgs.callPackage ./scripts/go-tidy-all.nix {
inherit pkgs;
};
containers = let
os = "linux";
all =
pkgs.lib.mapCartesianProduct ({
arch,
service_name,
}: {
"${service_name}" = {
"${toString arch}" = let
nix_arch =
builtins.replaceStrings
["arm64" "amd64"] ["aarch64" "x86_64"]
arch;
container_pkgs = import nixpkgs {
system = "${nix_arch}-${os}";
};
# if running from linux no cross-compilation is needed to palce the service in a container
needsCrossCompilation =
"${nix_arch}-${os}"
!= system;
service =
if !needsCrossCompilation
then
packages.${service_name}.overrideAttrs
(old: old // {doCheck = false;})
else
packages.${service_name}.overrideAttrs (old:
old
// {
GOOS = os;
GOARCH = arch;
# CGO_ENABLED = disabled breaks the CLI compilation
# CGO_ENABLED = 0;
doCheck = false;
});
in
builtins.trace "${service}/bin" pkgs.dockerTools.buildImage {
name = "${imageRegistry}/${service_name}";
tag = "latest-${arch}";
# tag = commit_hash;
created = "now";
copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = [
service
container_pkgs.bashInteractive
container_pkgs.nettools
container_pkgs.gnugrep
container_pkgs.coreutils
container_pkgs.cacert
];
pathsToLink = ["/bin"];
};
architecture = arch;
config.Cmd =
if !needsCrossCompilation
then ["${service}/bin/${service.pname}"]
else ["${service}/bin/${os}_${arch}/${service.pname}"];
config.Env = ["SSL_CERT_FILE=${container_pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"];
};
};
}) {
arch = architectures;
service_name = service_names;
};
in
pkgs.lib.foldl' (set: acc: pkgs.lib.recursiveUpdate acc set) {}
all;
cross-compiled-cli = let
all =
pkgs.lib.mapCartesianProduct ({
arch,
os,
}: {
"${os}" = {
"${toString arch}" = packages.kardinal-cli.overrideAttrs (old:
old
// {
GOOS = os;
GOARCH = arch;
# CGO_ENABLED = disabled breaks the CLI compilation
# CGO_ENABLED = 0;
doCheck = false;
});
};
}) {
arch = architectures;
os = ["linux" "darwin" "windows"];
};
in
pkgs.lib.foldl' (set: acc: pkgs.lib.recursiveUpdate acc set) {}
all;
};
# Add containers matching architecture with local system as toplevel packages
# this means calling `nix build .#<SERVICE_NAME>-container` will build the container matching the local system.
# For cross-compilation use the containers attribute directly: `nix build .containers.<LOCAL_SYSTEM>.<SERVICE_NAME>.<ARCH>`
outputWithContaniers = pkgs.lib.foldl' mergeContainerPackages systemOutput service_names;
outputWithContainersAndPushers = pkgs.lib.foldl' multiPlatformDockerPusher outputWithContaniers service_names;
in
outputWithContainersAndPushers
);
}