forked from st8ed/aws-cost-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
166 lines (139 loc) · 5.12 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
{
outputs = { self, nixpkgs }:
let
version = "0.3.5";
chartVersion = "0.1.5";
vendorSha256 = "sha256-e3AUY+qKnLEugLviQxTK1Dj6mIuo2oCu8pmjuLqrbio=";
dockerPackageTag = "st8ed/aws-cost-exporter:${version}";
src = with lib; builtins.path {
name = "aws-cost-exporter-src";
path = sources.cleanSourceWith rec {
filter = name: type:
let baseName = baseNameOf (toString name); in
!(
(baseName == ".github") ||
(hasSuffix ".nix" baseName) ||
(hasSuffix ".md" baseName) ||
(hasPrefix "${src}/deployments" name)
);
src = lib.cleanSource ./.;
};
};
src-chart = with lib; builtins.path {
name = "aws-cost-exporter-chart-src";
path = lib.cleanSource ./deployments/chart;
};
package = { go_1_17, buildGo117Module }: buildGo117Module {
pname = "aws-cost-exporter";
inherit version vendorSha256 src;
ldflags =
let
t = "github.com/prometheus/common";
in
[
"-s"
"-X ${t}.Revision=unknown"
"-X ${t}.Version=${version}"
"-X ${t}.Branch=unknown"
"-X ${t}.BuildUser=nix@nixpkgs"
"-X ${t}.BuildDate=unknown"
"-X ${t}.GoVersion=${lib.getVersion go_1_17}"
];
preInstall = ''
mkdir -p $out/share/aws-cost-exporter/queries
cp $src/configs/queries/* $out/share/aws-cost-exporter/queries/
'';
meta = with lib; {
homepage = "https://github.com/st8ed/aws-cost-exporter";
license = licenses.asl20;
platforms = platforms.unix;
};
};
dockerPackage = { pkgs, aws-cost-exporter, dockerTools, cacert, skopeo, moreutils, runCommandNoCC }:
let
# We compress image layers so the digest
# will be reproducible when pushing to registry
buildCompressedImage = stream: runCommandNoCC "aws-cost-exporter-dockerImage"
{
buildInputs = [ skopeo moreutils ];
} ''
# Piping archive stream to skopeo isn't working correctly
${stream} > archive.tar
skopeo --insecure-policy copy docker-archive:./archive.tar dir:$out \
--format v2s2 \
--dest-compress
'';
in
buildCompressedImage (dockerTools.streamLayeredImage {
name = "st8ed/aws-cost-exporter";
tag = "${version}";
contents = [
aws-cost-exporter
];
fakeRootCommands = ''
install -dm750 -o 1000 -g 1000 \
./etc/aws-cost-exporter \
./var/lib/aws-cost-exporter
cp -r \
${aws-cost-exporter}/share/aws-cost-exporter/* \
./etc/aws-cost-exporter
'';
config = {
Entrypoint = [ "/bin/aws-cost-exporter" ];
Cmd = [ ];
User = "1000:1000";
WorkingDir = "/var/lib/aws-cost-exporter";
Env = [
"SSL_CERT_FILE=${cacert}/etc/ssl/certs/ca-bundle.crt"
];
ExposedPorts = {
"9100/tcp" = { };
};
Volumes = {
"/var/lib/aws-cost-exporter" = { };
};
};
});
helmChart = { pkgs, aws-cost-exporter-dockerImage, kubernetes-helm, jq, gnused }: pkgs.runCommand "aws-cost-exporter-chart-${chartVersion}.tgz"
{
src = src-chart;
buildInputs = [ kubernetes-helm jq gnused ];
} ''
cp -r $src ./chart
chmod -R a+w ./chart
sed -i \
-e 's/^version: 0\.0\.0$/version: ${chartVersion}/' \
-e 's/^appVersion: "0\.0\.0"$/appVersion: "${version}"/' \
./chart/Chart.yaml
digest="sha256:$(sha256sum "${aws-cost-exporter-dockerImage}/manifest.json" | cut -d' ' -f1)"
echo "Digest: $digest"
sed -i \
-e 's|^image:.*$|image: "${dockerPackageTag}@'$digest'"|' \
./chart/values.yaml
mkdir -p ./package
helm package --destination ./package ./chart
mv ./package/*.tgz $out
'';
inherit (nixpkgs) lib;
supportedSystems = [ "x86_64-linux" "aarch64-linux" ];
forAllSystems = lib.genAttrs supportedSystems;
nixpkgsFor = lib.genAttrs supportedSystems (system: import nixpkgs {
inherit system;
overlays = [ self.overlay ];
});
in
{
overlay = pkgs: _: {
aws-cost-exporter = pkgs.callPackage package { };
aws-cost-exporter-dockerImage = pkgs.callPackage dockerPackage { };
aws-cost-exporter-helmChart = pkgs.callPackage helmChart { };
};
defaultPackage = forAllSystems (system: nixpkgsFor."${system}".aws-cost-exporter);
packages = forAllSystems (system: {
package = nixpkgsFor."${system}".aws-cost-exporter;
dockerImage = nixpkgsFor."${system}".aws-cost-exporter-dockerImage;
helmChart = nixpkgsFor."${system}".aws-cost-exporter-helmChart;
inherit src src-chart;
});
};
}