-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdefault.nix
More file actions
483 lines (442 loc) · 16.4 KB
/
default.nix
File metadata and controls
483 lines (442 loc) · 16.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
{
# Pin nixpkgs so nix-build works without '-I nixpkgs=…' or channels
# Pin nixpkgs for a stable toolchain; Linux builds target glibc 2.34 compatibility.
pkgs ?
let
nixpkgsSrc = builtins.fetchTarball {
# Use an immutable commit tarball so builds are deterministic across machines.
url = "https://package.cosmian.com/nixpkgs/8b27c1239e5c421a2bbc2c65d52e4a6fbf2ff296.tar.gz";
sha256 = "sha256-CqCX4JG7UiHvkrBTpYC3wcEurvbtTADLbo3Ns2CEoL8=";
};
in
import nixpkgsSrc { config.allowUnfree = true; },
}:
let
# Extract version from workspace Cargo.toml
# Read the file and parse it to find version = "x.y.z"
cargoTomlContent = builtins.readFile ./Cargo.toml;
# Split into lines and find the version line in [workspace.package] section
lines = pkgs.lib.splitString "\n" cargoTomlContent;
# Find workspace.package section, then extract version
extractVersion =
lines:
let
# Find index of [workspace.package]
findWorkspacePackage =
idx:
if idx >= builtins.length lines then
null
else if pkgs.lib.hasPrefix "[workspace.package]" (builtins.elemAt lines idx) then
idx
else
findWorkspacePackage (idx + 1);
workspaceIdx = findWorkspacePackage 0;
# Starting from workspace.package section, find version line
findVersion =
idx:
if idx >= builtins.length lines || workspaceIdx == null then
null
else
let
line = builtins.elemAt lines idx;
# Stop at next section
isNextSection = pkgs.lib.hasPrefix "[" line && idx > workspaceIdx;
in
if isNextSection then
null
else if pkgs.lib.hasPrefix "version" (pkgs.lib.replaceStrings [ " " "\t" ] [ "" "" ] line) then
# Extract "x.y.z" from version = "x.y.z"
let
# Remove everything before the first quote
afterFirstQuote = builtins.elemAt (pkgs.lib.splitString "\"" line) 1;
in
afterFirstQuote
else
findVersion (idx + 1);
in
if workspaceIdx == null then
throw "Could not find [workspace.package] in Cargo.toml"
else
let
ver = findVersion (workspaceIdx + 1);
in
if ver == null then throw "Could not find version in [workspace.package] section" else ver;
kmsVersion = extractVersion lines;
# Reuse the same pinned nixpkgs for internal imports/overlays
# Reuse the same pinned nixpkgs; Linux builds target glibc 2.34 compatibility.
nixpkgsSrc = builtins.fetchTarball {
# Mirrored on package.cosmian.com to avoid transient GitHub curl failures on macOS CI runners.
url = "https://package.cosmian.com/nixpkgs/8b27c1239e5c421a2bbc2c65d52e4a6fbf2ff296.tar.gz";
sha256 = "sha256-CqCX4JG7UiHvkrBTpYC3wcEurvbtTADLbo3Ns2CEoL8=";
};
# Bring a modern Rust toolchain (1.90.0) via oxalica/rust-overlay for Cargo edition2024 support
rustOverlay = import (
builtins.fetchTarball {
# Mirrored on package.cosmian.com to avoid transient GitHub curl failures on macOS CI runners.
url = "https://package.cosmian.com/nixpkgs/rust-overlay-23dd7fa91602a68bd04847ac41bc10af1e6e2fd2.tar.gz";
sha256 = "sha256-KvmjUeA7uODwzbcQoN/B8DCZIbhT/Q/uErF1BBMcYnw=";
}
);
pkgsWithRust = import nixpkgsSrc {
overlays = [ rustOverlay ];
config.allowUnfree = true;
};
# Use minimal Rust profile (no docs) and add only needed components to save disk space
rustToolchain = pkgsWithRust.rust-bin.stable."1.90.0".minimal.override {
extensions = [
"rustfmt"
"clippy"
];
targets = [ "wasm32-unknown-unknown" ];
};
# For Linux, pin nixpkgs 22.05 (glibc 2.34) to get its stdenv while using a modern
# Rust toolchain (1.90.0) from rust-overlay. Rocky Linux 9 compatibility requires GLIBC <= 2.34.
# Hardcoded URL+hash for full determinism — override via `--arg pkgs234 ...` if needed.
pkgs234 =
if pkgs.stdenv.isLinux then
import (builtins.fetchTarball {
url = "https://package.cosmian.com/nixpkgs/380be19fbd2d9079f677978361792cb25e8a3635.tar.gz";
sha256 = "sha256-Zffu01pONhs/pqH07cjlF10NnMDLok8ix5Uk4rhOnZQ=";
}) { config.allowUnfree = true; }
else
pkgs;
# For Linux, pin nixpkgs ≈ 19.09 (glibc 2.28) for CLI builds targeting
# RHEL 8 / CentOS 8 / Debian 10 / Ubuntu 18.04 compatibility.
# Override via `--arg pkgs228 ...` if needed.
# To update: nix-prefetch-url --unpack https://package.cosmian.com/nixpkgs/<commit>.tar.gz
pkgs228 =
if pkgs.stdenv.isLinux then
import (builtins.fetchTarball {
url = "https://package.cosmian.com/nixpkgs/nixos-19.03.tar.gz";
sha256 = "sha256-i1XCn9rKuLjvCdu2UeXKzGLF6IuQePQKFt4hEKRU5oc=";
}) { config.allowUnfree = true; }
else
pkgs;
# Create rustPlatform: on Linux use pkgs234.makeRustPlatform (glibc 2.34),
# but with modern Rust toolchain
rustPlatform190 =
if pkgs.stdenv.isLinux then
pkgs234.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
}
else
pkgsWithRust.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
# rustPlatform for CLI builds: on Linux use pkgs228.makeRustPlatform (glibc 2.28)
# so the CLI binary only references glibc symbols ≤ 2.28.
rustPlatform190_228 =
if pkgs.stdenv.isLinux then
pkgs228.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
}
else
pkgsWithRust.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
# Build OpenSSL 3.1.2 with nixpkgs 22.05 stdenv (glibc 2.34 for Rocky Linux 9)
# Create both static and dynamic versions
openssl312-static = pkgs234.callPackage ./nix/openssl.nix { static = true; };
openssl312-dynamic = pkgs234.callPackage ./nix/openssl.nix { static = false; };
# Default to static for backward compatibility
openssl312 = openssl312-static;
# Build OpenSSL 3.6.0 with legacy provider for non-FIPS builds
# Common parameters for both static and dynamic builds
openssl36Args = {
version = "3.6.0";
enableLegacy = true;
srcUrl = "https://package.cosmian.com/openssl/openssl-3.6.0.tar.gz";
sha256SRI = "sha256-tqX0S362nj+jXb8VUkQFtEg3pIHUPYHa3d4/8h/LuOk=";
expectedHash = "b6a5f44b7eb69e3fa35dbf15524405b44837a481d43d81daddde3ff21fcbb8e9";
};
openssl36-static = pkgs234.callPackage ./nix/openssl.nix (openssl36Args // { static = true; });
openssl36-dynamic = pkgs234.callPackage ./nix/openssl.nix (openssl36Args // { static = false; });
# OpenSSL variants built against pkgs228 (glibc 2.28) for CLI binaries
openssl312-static-228 = pkgs228.callPackage ./nix/openssl.nix { static = true; };
openssl312-dynamic-228 = pkgs228.callPackage ./nix/openssl.nix { static = false; };
openssl36-static-228 = pkgs228.callPackage ./nix/openssl.nix (openssl36Args // { static = true; });
openssl36-dynamic-228 = pkgs228.callPackage ./nix/openssl.nix (
openssl36Args // { static = false; }
);
# Tool: cargo-generate-rpm (not available in some nixpkgs pins). Build it from crates.io.
# Build cargo-generate-rpm with the same modern Rust toolchain (Cargo 1.90)
# to support lockfile v4 and avoid -Znext-lockfile-bump issues
cargoGenerateRpmTool = rustPlatform190.buildRustPackage rec {
pname = "cargo-generate-rpm";
version = "0.16.0";
src = pkgs.fetchCrate {
inherit pname version;
# Pinned crate tarball hash from crates.io fetch
sha256 = "sha256-esp3MJ24RQpMFn9zPgccp7NESoFAUPU7y+YRsJBVVr4=";
};
# Pinned cargo vendor hash for reproducible builds
cargoSha256 = "sha256-mUsoPBgv60Eir/uIK+Xe+GmXdSFKXoopB4PlvFvHZuA=";
nativeBuildInputs = [
rustToolchain
pkgs.pkg-config
pkgs.git
pkgs.cacert
];
doCheck = false; # skip upstream tests; they assume specific host paths like /usr/bin/ldd
};
# Tool: cargo-packager (may be missing in pinned nixpkgs). Build it from crates.io
# so DMG packaging keeps using cargo-packager as required.
cargoPackagerTool = rustPlatform190.buildRustPackage rec {
pname = "cargo-packager";
# Align with version used in CI scripts to reduce surprises
version = "0.11.7"; # Update if needed; hash will enforce correctness
src = pkgs.fetchCrate {
inherit pname version;
# Initial placeholder; Nix will suggest the correct one on first build if mismatched
sha256 = "sha256-dSF2BzT+wun75qRBvDJpoOwNG4dHUeVnTx/Ygm5wtK0=";
};
# Pinned cargo vendor hash differs by platform (target-specific deps)
# Observed on current macOS build: got sha256-uhXPFBZ6sWQch+liz7F67PC6ns+P63eMJ6bYWr07L8U=
# Swap mapping accordingly to keep both platforms green.
# - macOS (Darwin): sha256-uhXPFBZ6sWQch+liz7F67PC6ns+P63eMJ6bYWr07L8U=
# - Linux: sha256-bV3OMNY/UM+1Cz/bmpKRMMCV863e7qMKDQ3Dh+bofo8=
cargoSha256 =
if pkgs.stdenv.isDarwin then
"sha256-uhXPFBZ6sWQch+liz7F67PC6ns+P63eMJ6bYWr07L8U="
else
"sha256-bV3OMNY/UM+1Cz/bmpKRMMCV863e7qMKDQ3Dh+bofo8=";
nativeBuildInputs = [
rustToolchain
pkgs.pkg-config
pkgs.git
pkgs.cacert
];
doCheck = false;
};
# Build UI for both variants (use modern pkgs for UI build tools)
ui-fips = pkgs.callPackage ./nix/ui.nix {
features = [ ];
version = kmsVersion;
inherit rustToolchain;
};
ui-non-fips = pkgs.callPackage ./nix/ui.nix {
features = [ "non-fips" ];
version = kmsVersion;
inherit rustToolchain;
};
# DRY helper to build servers for both variants and both linkage modes
mkKmsServer =
{
features,
ui,
static ? true,
}:
pkgs.callPackage ./nix/kms-server.nix {
openssl312 = if static then openssl312-static else openssl312-dynamic;
openssl36 = if static then openssl36-static else openssl36-dynamic;
inherit pkgs234;
rustPlatform = rustPlatform190;
version = kmsVersion;
inherit
features
ui
static
;
};
mkKmsCli =
{
features,
static ? true,
}:
pkgs.callPackage ./nix/cli.nix {
# Use pkgs228 (glibc 2.28) so the CLI binary only requires GLIBC ≤ 2.28.
openssl312 = if static then openssl312-static-228 else openssl312-dynamic-228;
openssl36 = if static then openssl36-static-228 else openssl36-dynamic-228;
inherit pkgs228;
rustPlatform = rustPlatform190_228;
version = kmsVersion;
inherit
features
static
;
};
# Build KMS server in both variants with static OpenSSL
kms-server-fips-static-openssl = mkKmsServer {
features = [ ];
ui = ui-fips;
static = true;
};
kms-server-non-fips-static-openssl = mkKmsServer {
features = [ "non-fips" ];
ui = ui-non-fips;
static = true;
};
# Build KMS server with dynamic OpenSSL linking
kms-server-fips-dynamic-openssl = mkKmsServer {
features = [ ];
ui = ui-fips;
static = false;
};
kms-server-non-fips-dynamic-openssl = mkKmsServer {
features = [ "non-fips" ];
ui = ui-non-fips;
static = false;
};
kms-cli-fips-static-openssl = mkKmsCli {
features = [ ];
static = true;
};
kms-cli-non-fips-static-openssl = mkKmsCli {
features = [ "non-fips" ];
static = true;
};
kms-cli-fips-dynamic-openssl = mkKmsCli {
features = [ ];
static = false;
};
kms-cli-non-fips-dynamic-openssl = mkKmsCli {
features = [ "non-fips" ];
static = false;
};
# Docker images using dockerTools (minimal images)
docker-image-fips = pkgs.callPackage ./nix/docker.nix {
kmsServer = kms-server-fips-static-openssl;
pkcs11LibDrv = kms-cli-fips-static-openssl;
variant = "fips";
version = kmsVersion;
opensslDrv = openssl312;
};
docker-image-non-fips = pkgs.callPackage ./nix/docker.nix {
kmsServer = kms-server-non-fips-static-openssl;
pkcs11LibDrv = kms-cli-non-fips-static-openssl;
variant = "non-fips";
version = kmsVersion;
# Provide OpenSSL 3.6.0 so the Docker image ships legacy provider + non-FIPS openssl.cnf
opensslDrv = openssl36-static;
};
in
rec {
# Binary packages (can be installed with nix-env)
inherit
kms-server-fips-static-openssl
kms-server-non-fips-static-openssl
kms-server-fips-dynamic-openssl
kms-server-non-fips-dynamic-openssl
kms-cli-fips-static-openssl
kms-cli-non-fips-static-openssl
kms-cli-fips-dynamic-openssl
kms-cli-non-fips-dynamic-openssl
;
# Export UI builds for debugging/development
inherit ui-fips ui-non-fips;
# Docker images
inherit
docker-image-fips
docker-image-non-fips
;
# Export OpenSSL 3.1.2 FIPS derivations for tooling (packaging script)
inherit openssl312 openssl312-static openssl312-dynamic;
# Export OpenSSL 3.6.0 derivations (with legacy provider for non-FIPS)
inherit openssl36-static openssl36-dynamic;
# Export cargo-packager and cargo-generate-rpm tools for scripts and dev shell
inherit cargoPackagerTool cargoGenerateRpmTool;
# Export the pinned Rust toolchain (1.90.0) so scripts can use a modern Cargo (edition2024)
inherit rustToolchain;
# Default to FIPS variant
kms-server = kms-server-fips-static-openssl;
kms-cli = kms-cli-fips-static-openssl;
docker-image = docker-image-fips;
# Expected-hash files (generated by Nix, copyable into nix/expected-hashes)
# Each attribute produces one file named per convention under $out/.
expected-hash-server-fips-static =
let
sys = pkgs.stdenv.hostPlatform.system;
parts = pkgs.lib.splitString "-" sys;
arch = builtins.elemAt parts 0;
os = builtins.elemAt parts 1;
name = "cosmian-kms-server.fips.static-openssl.${arch}.${os}.sha256";
in
pkgs.runCommand "expected-hash-server-fips-static"
{
buildInputs = [
pkgs.openssl
pkgs.coreutils
];
}
''
set -euo pipefail
mkdir -p "$out"
bin='${kms-server-fips-static-openssl}/bin/cosmian_kms'
[ -x "$bin" ] || { echo "Missing binary: $bin" >&2; exit 1; }
hash="$(${pkgs.openssl}/bin/openssl dgst -sha256 -r "$bin" | awk '{print $1}')"
printf '%s\n' "$hash" >"$out/${name}"
'';
expected-hash-server-fips-dynamic =
let
sys = pkgs.stdenv.hostPlatform.system;
parts = pkgs.lib.splitString "-" sys;
arch = builtins.elemAt parts 0;
os = builtins.elemAt parts 1;
name = "cosmian-kms-server.fips.dynamic-openssl.${arch}.${os}.sha256";
in
pkgs.runCommand "expected-hash-server-fips-dynamic"
{
buildInputs = [
pkgs.openssl
pkgs.coreutils
];
}
''
set -euo pipefail
mkdir -p "$out"
bin='${kms-server-fips-dynamic-openssl}/bin/cosmian_kms'
[ -x "$bin" ] || { echo "Missing binary: $bin" >&2; exit 1; }
hash="$(${pkgs.openssl}/bin/openssl dgst -sha256 -r "$bin" | awk '{print $1}')"
printf '%s\n' "$hash" >"$out/${name}"
'';
expected-hash-server-non-fips-static =
let
sys = pkgs.stdenv.hostPlatform.system;
parts = pkgs.lib.splitString "-" sys;
arch = builtins.elemAt parts 0;
os = builtins.elemAt parts 1;
name = "cosmian-kms-server.non-fips.static-openssl.${arch}.${os}.sha256";
in
pkgs.runCommand "expected-hash-server-non-fips-static"
{
buildInputs = [
pkgs.openssl
pkgs.coreutils
];
}
''
set -euo pipefail
mkdir -p "$out"
bin='${kms-server-non-fips-static-openssl}/bin/cosmian_kms'
[ -x "$bin" ] || { echo "Missing binary: $bin" >&2; exit 1; }
hash="$(${pkgs.openssl}/bin/openssl dgst -sha256 -r "$bin" | awk '{print $1}')"
printf '%s\n' "$hash" >"$out/${name}"
'';
expected-hash-server-non-fips-dynamic =
let
sys = pkgs.stdenv.hostPlatform.system;
parts = pkgs.lib.splitString "-" sys;
arch = builtins.elemAt parts 0;
os = builtins.elemAt parts 1;
name = "cosmian-kms-server.non-fips.dynamic-openssl.${arch}.${os}.sha256";
in
pkgs.runCommand "expected-hash-server-non-fips-dynamic"
{
buildInputs = [
pkgs.openssl
pkgs.coreutils
];
}
''
set -euo pipefail
mkdir -p "$out"
bin='${kms-server-non-fips-dynamic-openssl}/bin/cosmian_kms'
[ -x "$bin" ] || { echo "Missing binary: $bin" >&2; exit 1; }
hash="$(${pkgs.openssl}/bin/openssl dgst -sha256 -r "$bin" | awk '{print $1}')"
printf '%s\n' "$hash" >"$out/${name}"
'';
}