-
Notifications
You must be signed in to change notification settings - Fork 3
/
flake.nix
129 lines (107 loc) · 4.23 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
{
description = "Fullnodes";
inputs = {
nixpkgs = {
url = "github:NixOS/nixpkgs";
};
};
outputs = { self, flake-utils, nixpkgs }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ self.overlays.default ];
};
allFullnodeVersions = import ./versions.nix;
importVersion = name: version: {
"${version}" = import (./fullnodes/${name}) { inherit pkgs version; };
};
allFullnodes = builtins.mapAttrs (name: versions:
builtins.zipAttrsWith (version: bundles:
builtins.head bundles
) (builtins.map (importVersion name) versions)
) allFullnodeVersions;
fullnodeImage = name: version: fullnode: pkgs.dockerTools.buildLayeredImage ({
name = "coinmetrics/${name}";
tag = version;
maxLayers = 2;
created = "now";
} // fullnode.imageConfig);
allFullnodeImages = builtins.mapAttrs (name: versions:
builtins.mapAttrs (fullnodeImage name) versions
) allFullnodes;
allFullnodeBinaries = builtins.mapAttrs (_: versions:
builtins.mapAttrs (_: fullnode: fullnode.package) versions
) allFullnodes;
flattenAttrs = f: s: builtins.concatLists (
pkgs.lib.mapAttrsToList (outerName: outerValue:
pkgs.lib.mapAttrsToList (innerName: innerValue:
{ "${f outerName innerName}" = innerValue; }
) outerValue
) s
);
flatBinaryMap = builtins.zipAttrsWith
(_: binaries: builtins.head binaries)
(flattenAttrs
(outer: inner: builtins.replaceStrings [ "." ] [ "_" ] "${outer}_${inner}")
allFullnodeBinaries
);
flatImageMap = builtins.zipAttrsWith
(_: images: builtins.head images)
(flattenAttrs
(outer: inner: builtins.replaceStrings [ "." ] [ "_" ] "${outer}_${inner}-image")
allFullnodeImages
);
registryLoginScript = pkgs.writeShellApplication {
name = "authenticate-to-registries";
runtimeInputs = [ pkgs.skopeo ];
text = ''
skopeo login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY" <<< "$CI_REGISTRY_PASSWORD"
skopeo login -u "$DOCKERHUB_USERNAME" --password-stdin docker.io <<< "$DOCKERHUB_TOKEN"
'';
};
imagesFor = fullnode: pkgs.lib.mapAttrsToList (tag: image:
{ inherit tag image; }
) allFullnodeImages.${fullnode};
publishImagesFor = fullnode:
let
images = imagesFor fullnode;
in
pkgs.writeShellApplication {
name = "publish-${fullnode}";
runtimeInputs = [ pkgs.skopeo ];
text = ''
if [[ $# -gt 0 ]] && [[ $1 == "dry-run" ]]; then
echo "Build complete, not pushing images."
exit 0
fi
images=(${builtins.concatStringsSep " " (builtins.map (i: "\"${i.image}\"") images)})
tags=(${builtins.concatStringsSep " " (builtins.map (i: "\"${i.tag}\"") images)})
for i in "''${!images[@]}"; do
if [[ -n ''${CI_REGISTRY_IMAGE+x} ]]; then
until skopeo --insecure-policy copy --retry-times 10 -f oci docker-archive:"''${images[i]}" docker://"$CI_REGISTRY_IMAGE"/${fullnode}:"''${tags[i]}"; do
echo Copy failed, retrying in 10 seconds...
sleep 10
done
fi
if [[ $PRIVATE == "true" ]]; then
echo "Image marked private: Skipping publish to Docker Hub."
else
skopeo --insecure-policy copy --retry-times 10 -f oci docker-archive:"''${images[i]}" docker://docker.io/coinmetrics/${fullnode}:"''${tags[i]}"
fi
done
'';
};
in
{
packages = flatBinaryMap // flatImageMap;
apps = {
login = { type = "app"; program = "${registryLoginScript}/bin/authenticate-to-registries"; };
} // builtins.foldl' (accum: fullnode:
accum // { "publish-${fullnode}" = { type = "app"; program = "${publishImagesFor fullnode}/bin/publish-${fullnode}"; }; }
) {} (builtins.attrNames allFullnodeImages);
}
) // {
overlays.default = import ./overlays/default.nix;
};
}