-
Notifications
You must be signed in to change notification settings - Fork 1
/
flake.nix
261 lines (243 loc) · 9.3 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{
description = "Go development environment";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
flake-utils.url = "github:numtide/flake-utils";
unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
gomod2nix.url = "github:nix-community/gomod2nix";
gomod2nix.inputs.nixpkgs.follows = "nixpkgs";
gomod2nix.inputs.flake-utils.follows = "flake-utils";
};
outputs = {
self,
nixpkgs,
flake-utils,
unstable,
gomod2nix,
...
}:
flake-utils.lib.eachDefaultSystem
(
system: let
pkgs = import nixpkgs {
inherit system;
overlays = [
(import "${gomod2nix}/overlay.nix")
];
};
version = toString (self.ref or self.shortRev or self.dirtyShortRev or self.lastModified or "unknown");
tag-name = let
branch-name = import ./branch-name.nix;
tag = toString (
if isNull branch-name
then "${version}"
else "${toString branch-name}"
);
in
builtins.replaceStrings ["/"] ["_"] tag;
service_names = [
"cartservice"
"frontend"
"metrics"
"productcatalogservice"
];
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 = tag-name;
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 = let
go-tidy-all = pkgs.writeShellApplication {
name = "go-tidy-all";
runtimeInputs = with pkgs; [go git gomod2nix];
text = ''
root_dirpath=$(git rev-parse --show-toplevel)
find "$root_dirpath" -type f -name 'go.mod' -exec sh -c 'dir=$(dirname "$1") && cd "$dir" && echo "$dir" && go mod tidy && gomod2nix' shell {} \;
'';
};
tag-branch = pkgs.writeShellApplication {
name = "tag-branch";
runtimeInputs = with pkgs; [git];
text = ''
GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "\"$CURRENT_BRANCH\"" >"$GIT_ROOT/branch-name.nix"
echo "Branch name \"$CURRENT_BRANCH\" written to $GIT_ROOT/branch-name.nix"
'';
};
tag-branch-version = pkgs.writeShellApplication {
name = "tag-branch-version";
runtimeInputs = with pkgs; [git];
text = ''
GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
SHORT_COMMIT_HASH=$(git rev-parse --short HEAD)
echo "\"$CURRENT_BRANCH-$SHORT_COMMIT_HASH\"" >"$GIT_ROOT/branch-name.nix"
echo "Branch name \"$CURRENT_BRANCH-$SHORT_COMMIT_HASH\" written to $GIT_ROOT/branch-name.nix"
'';
};
in
pkgs.mkShell {
buildInputs = with pkgs; [
go
gopls
go-tools
golangci-lint
pkgs.gomod2nix
skaffold
minikube
dive
go-tidy-all
tag-branch
tag-branch-version
];
shellHook = ''
echo "Go development environment loaded"
go version
'';
};
packages.cartservice = pkgs.callPackage ./src/cartservice {
inherit pkgs;
};
packages.metrics = pkgs.callPackage ./src/metrics {
inherit pkgs;
};
packages.frontend = pkgs.callPackage ./src/frontend {
inherit pkgs;
};
packages.productcatalogservice = pkgs.callPackage ./src/productcatalogservice {
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
pkgs.dockerTools.buildImage {
name = "${imageRegistry}/${service_name}";
tag = "${tag-name}-${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;
};
# 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
);
}