-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
306 lines (270 loc) · 12.4 KB
/
flake.nix
File metadata and controls
306 lines (270 loc) · 12.4 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#
# https://github.com/randomizedcoder/go_nix_simple
#
# flake.nix
#
{
description = "A simple Go application packaged with Nix and Docker";
inputs = {
#nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
gomod2nix = {
url = "github:tweag/gomod2nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
};
outputs = { self, nixpkgs, flake-utils, gomod2nix }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ gomod2nix.overlays.default ];
config = {
allowUnfree = true;
};
};
appVersion = builtins.readFile ./VERSION;
# Common ldflags for Go builds
commonLdflags = [ "-s" "-w" "-X main.version=${appVersion}" "-X main.commit=nix-build" "-X main.date=unknown" ];
# Common buildFlags for Go builds
commonBuildFlags = [ "-tags=netgo,osusergo" "-trimpath" ];
# --- Base Binary Derivations ---
# Binary built using Nixpkgs buildGoModule
binaryNixBuildGoModule = pkgs.buildGoModule { # Renamed from binaryNixDefault
pname = "go-nix-simple-buildgomodule"; # Adjusted pname
version = appVersion;
src = ./.;
subPackages = [ "cmd/go_nix_simple" ];
# Ensure this hash is updated when go.mod/go.sum changes
# Run: nix build .#binary-nix-buildgomodule --rebuild
#vendorHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
vendorHash = "sha256-iMUa+OE/Ecb3TDw4PmvRfujo++T/r4g/pW0ZT63zIC4=";
ldflags = commonLdflags;
buildFlags = commonBuildFlags;
env = { CGO_ENABLED = 0; };
# postInstall = ''
# mv $out/bin/go-nix-simple-buildgomodule $out/bin/go_nix_simple
# '';
};
# Binary built using gomod2nix buildGoApplication
binaryNixGomod2nix = pkgs.buildGoApplication {
pname = "go-nix-simple-gomod2nix";
version = appVersion;
modules = ./gomod2nix.toml;
src = ./.;
ldflags = commonLdflags;
buildFlags = commonBuildFlags;
#env = { CGO_ENABLED = 0; };
CGO_ENABLED = 0;
# postInstall = ''
# mv $out/bin/go-nix-simple-gomod2nix $out/bin/go_nix_simple
# '';
};
# --- UPX Packed Binary Derivations ---
# UPX version of the buildGoModule binary
binaryNixBuildGoModuleUpx = pkgs.runCommand "go-nix-simple-buildgomodule-upx" { # Renamed
nativeBuildInputs = [ pkgs.upx ];
src = binaryNixBuildGoModule; # Depend on the renamed binary derivation
} ''
mkdir -p $out/bin
local orig_bin="$src/bin/go_nix_simple"
echo "Original size ($(basename $orig_bin)): $(ls -lh $orig_bin | awk '{print $5}')"
upx --best --lzma -o $out/bin/go_nix_simple "$orig_bin"
echo "Compressed size ($(basename $out/bin/go_nix_simple)): $(ls -lh $out/bin/go_nix_simple | awk '{print $5}')"
chmod +x $out/bin/go_nix_simple
'';
# UPX version of the gomod2nix binary
binaryNixGomod2nixUpx = pkgs.runCommand "go-nix-simple-gomod2nix-upx" {
nativeBuildInputs = [ pkgs.upx ];
src = binaryNixGomod2nix;
} ''
mkdir -p $out/bin
local orig_bin="$src/bin/go_nix_simple" # Assumes postInstall worked
echo "Original size ($(basename $orig_bin)): $(ls -lh $orig_bin | awk '{print $5}')"
upx --best --lzma -o $out/bin/go_nix_simple "$orig_bin"
echo "Compressed size ($(basename $out/bin/go_nix_simple)): $(ls -lh $out/bin/go_nix_simple | awk '{print $5}')"
chmod +x $out/bin/go_nix_simple
'';
# --- Common Image Components ---
etcFiles = pkgs.runCommand "etc-files" {} ''
mkdir -p $out/etc
echo 'nogroup:x:65534:' > $out/etc/group
echo 'nobody:x:65534:65534:Nobody:/:/sbin/nologin' > $out/etc/passwd
'';
versionFilePkg = pkgs.runCommand "version-file" {} ''
mkdir -p $out
cp ${./VERSION} $out/VERSION
'';
distrolessBase = pkgs.dockerTools.pullImage {
imageName = "gcr.io/distroless/static-debian12";
imageDigest = "sha256:3d0f463de06b7ddff27684ec3bfd0b54a425149d0f8685308b1fdf297b0265e9";
sha256 = "0ajgz5slpdv42xqrildx850vp4cy6x44yj0hfz53raz3r971ikcf";
finalImageTag = "latest";
};
# Helper function to build layered images
buildImage = { name, tag ? "latest", baseImage ? null, binaryPkg, extraContents ? [] }:
pkgs.dockerTools.buildLayeredImage {
inherit name tag;
fromImage = baseImage;
contents = [ binaryPkg versionFilePkg etcFiles ] ++ extraContents;
config = {
User = "nobody";
WorkingDir = "/";
ExposedPorts = { "9108/tcp" = {}; };
Cmd = [ "${binaryPkg}/bin/go_nix_simple" ];
};
};
# --- Image Derivations (All Combinations) ---
# Distroless + BuildGoModule + NoUPX
imageNixDistrolessBuildGoModuleNoupx = buildImage { # Renamed
name = "randomizedcoder/nix-go-nix-simple-distroless-buildgomodule-noupx"; # Renamed
baseImage = distrolessBase;
binaryPkg = binaryNixBuildGoModule; # Use renamed binary
};
# Distroless + BuildGoModule + UPX
imageNixDistrolessBuildGoModuleUpx = buildImage { # Renamed
name = "randomizedcoder/nix-go-nix-simple-distroless-buildgomodule-upx"; # Renamed
baseImage = distrolessBase;
binaryPkg = binaryNixBuildGoModuleUpx; # Use renamed binary
};
# Distroless + Gomod2nix + NoUPX
imageNixDistrolessGomod2nixNoupx = buildImage {
name = "randomizedcoder/nix-go-nix-simple-distroless-gomod2nix-noupx";
baseImage = distrolessBase;
binaryPkg = binaryNixGomod2nix;
};
# Distroless + Gomod2nix + UPX
imageNixDistrolessGomod2nixUpx = buildImage {
name = "randomizedcoder/nix-go-nix-simple-distroless-gomod2nix-upx";
baseImage = distrolessBase;
binaryPkg = binaryNixGomod2nixUpx;
};
# Scratch + BuildGoModule + NoUPX
imageNixScratchBuildGoModuleNoupx = buildImage { # Renamed
name = "randomizedcoder/nix-go-nix-simple-scratch-buildgomodule-noupx"; # Renamed
binaryPkg = binaryNixBuildGoModule; # Use renamed binary
};
# Scratch + BuildGoModule + UPX
imageNixScratchBuildGoModuleUpx = buildImage { # Renamed
name = "randomizedcoder/nix-go-nix-simple-scratch-buildgomodule-upx"; # Renamed
binaryPkg = binaryNixBuildGoModuleUpx; # Use renamed binary
};
# Scratch + Gomod2nix + NoUPX
imageNixScratchGomod2nixNoupx = buildImage {
name = "randomizedcoder/nix-go-nix-simple-scratch-gomod2nix-noupx";
binaryPkg = binaryNixGomod2nix;
};
# Scratch + Gomod2nix + UPX
imageNixScratchGomod2nixUpx = buildImage {
name = "randomizedcoder/nix-go-nix-simple-scratch-gomod2nix-upx";
binaryPkg = binaryNixGomod2nixUpx;
};
# --- Utility Images (Example: Athens) ---
athensNixImage = pkgs.dockerTools.buildLayeredImage {
name = "randomizedcoder/athens-nix";
tag = "latest";
fromImage = distrolessBase;
contents = [ pkgs.athens etcFiles ];
config = {
User = "nobody";
WorkingDir = "/data";
ExposedPorts = { "8888/tcp" = {}; };
Volumes = { "/data/athens" = {}; };
Env = [
"ATHENS_HOST=0.0.0.0"
"ATHENS_PORT=8888"
"ATHENS_STORAGE_TYPE=disk"
"ATHENS_DISK_STORAGE_ROOT=/data/athens"
"ATHENS_LOG_LEVEL=info"
];
Cmd = [ "${pkgs.athens}/bin/athens" ];
};
};
in
{
# --- Consistent Package Naming ---
packages = {
# Binaries
binary-nix-buildgomodule = binaryNixBuildGoModule; # Renamed key
binary-nix-buildgomodule-upx = binaryNixBuildGoModuleUpx; # Renamed key
binary-nix-gomod2nix = binaryNixGomod2nix;
binary-nix-gomod2nix-upx = binaryNixGomod2nixUpx;
# Images (All 8 combinations)
image-nix-distroless-buildgomodule-noupx = imageNixDistrolessBuildGoModuleNoupx; # Renamed key
image-nix-distroless-buildgomodule-upx = imageNixDistrolessBuildGoModuleUpx; # Renamed key
image-nix-distroless-gomod2nix-noupx = imageNixDistrolessGomod2nixNoupx;
image-nix-distroless-gomod2nix-upx = imageNixDistrolessGomod2nixUpx;
image-nix-scratch-buildgomodule-noupx = imageNixScratchBuildGoModuleNoupx; # Renamed key
image-nix-scratch-buildgomodule-upx = imageNixScratchBuildGoModuleUpx; # Renamed key
image-nix-scratch-gomod2nix-noupx = imageNixScratchGomod2nixNoupx;
image-nix-scratch-gomod2nix-upx = imageNixScratchGomod2nixUpx;
# Utility Images
athens-nix-image = athensNixImage;
# Default package for `nix build`
default = self.packages.${system}.image-nix-distroless-buildgomodule-noupx; # Updated default
};
# --- Apps ---
apps = {
# Default app for `nix run`
default = flake-utils.lib.mkApp {
drv = self.packages.${system}.binary-nix-buildgomodule;
# Explicitly tell mkApp where the executable is
exePath = "/bin/go_nix_simple";
};
gomod2nix = flake-utils.lib.mkApp {
drv = self.packages.${system}.binary-nix-gomod2nix;
# Also specify exePath here for consistency, since postInstall renames it
exePath = "/bin/go_nix_simple";
};
# Apps to output image tarballs (useful for loading into Docker)
# Update keys to match package names
image-distroless-buildgomodule-noupx-tarball = flake-utils.lib.mkApp { drv = self.packages.${system}.image-nix-distroless-buildgomodule-noupx; };
image-distroless-buildgomodule-upx-tarball = flake-utils.lib.mkApp { drv = self.packages.${system}.image-nix-distroless-buildgomodule-upx; };
image-distroless-gomod2nix-noupx-tarball = flake-utils.lib.mkApp { drv = self.packages.${system}.image-nix-distroless-gomod2nix-noupx; };
image-distroless-gomod2nix-upx-tarball = flake-utils.lib.mkApp { drv = self.packages.${system}.image-nix-distroless-gomod2nix-upx; };
image-scratch-buildgomodule-noupx-tarball = flake-utils.lib.mkApp { drv = self.packages.${system}.image-nix-scratch-buildgomodule-noupx; };
image-scratch-buildgomodule-upx-tarball = flake-utils.lib.mkApp { drv = self.packages.${system}.image-nix-scratch-buildgomodule-upx; };
image-scratch-gomod2nix-noupx-tarball = flake-utils.lib.mkApp { drv = self.packages.${system}.image-nix-scratch-gomod2nix-noupx; };
image-scratch-gomod2nix-upx-tarball = flake-utils.lib.mkApp { drv = self.packages.${system}.image-nix-scratch-gomod2nix-upx; };
};
# --- Dev Shell ---
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
go
gopls
gotools
golint
golangci-lint
go-tools
golangci-lint-langserver
gomod2nix.packages.${system}.default
#gomod2nix
upx
# https://github.com/bazelbuild/bazel/tags
# https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/development/tools/build-managers/bazel/bazel_7/default.nix#L524
bazel_7
# https://github.com/bazel-contrib/bazel-gazelle/tags
# https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/by-name/ba/bazel-gazelle/package.nix#L26
bazel-gazelle
bazel-buildtools
bazelisk
#
curl
jq
#
dive
#
code-cursor
];
shellHook = ''
export PS1='(nix-dev) \w\$ '
echo "Entered Nix development shell for go-nix-simple."
'';
# You might have other shell attributes here
# Example: GOPATH = "${pkgs.buildGoModule}/share/go";
};
});
}
# end