forked from Cosmian/kms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.nix
More file actions
393 lines (362 loc) · 12.6 KB
/
default.nix
File metadata and controls
393 lines (362 loc) · 12.6 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
{
# Pin nixpkgs so nix-build works without '-I nixpkgs=…' or channels
pkgs ?
let
nixpkgsSrc = builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/24.05.tar.gz";
};
in
import nixpkgsSrc { config.allowUnfree = true; },
# Allow callers (e.g., Docker) to toggle deterministic hash enforcement.
# Default is relaxed (false) so builds don't fail when hashes drift; CI/scripts
# can enable it explicitly when needed.
enforceDeterministicHash ? false,
}:
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
nixpkgsSrc = builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/24.05.tar.gz";
};
# Bring a modern Rust toolchain (1.90.0) via oxalica/rust-overlay for Cargo edition2024 support
rustOverlay = import (
builtins.fetchTarball {
url = "https://github.com/oxalica/rust-overlay/archive/refs/heads/master.tar.gz";
}
);
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, we need glibc <= 2.28. Import older nixpkgs to get its stdenv (2.28).
pkgs228 =
if pkgs.stdenv.isLinux then
(
let
nixpkgs1903 = builtins.getEnv "NIXPKGS_GLIBC_228_URL";
in
import (builtins.fetchTarball {
url =
if nixpkgs1903 != "" then
nixpkgs1903
else
# Pin to a stable tag tarball (glibc 2.28 lives in 19.03)
"https://github.com/NixOS/nixpkgs/archive/nixos-19.03.tar.gz";
}) { config.allowUnfree = true; }
)
else
pkgs;
# Create rustPlatform: on Linux use pkgs228.makeRustPlatform (glibc 2.27),
# but with modern Rust toolchain
rustPlatform190 =
if pkgs.stdenv.isLinux then
pkgs228.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
}
else
pkgsWithRust.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
# Build OpenSSL 3.1.2 with old nixpkgs stdenv (glibc 2.27)
# Create both static and dynamic versions
openssl312-static = pkgs228.callPackage ./nix/openssl.nix { static = true; };
openssl312-dynamic = pkgs228.callPackage ./nix/openssl.nix { static = false; };
# Default to static for backward compatibility
openssl312 = openssl312-static;
# 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-vXb6O9xoYRVAbFGlhbPE6xYYqjSWT/fvoXYl4dkMxEg=";
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 enforceDeterministicHash;
};
ui-non-fips = pkgs.callPackage ./nix/ui.nix {
features = [ "non-fips" ];
version = kmsVersion;
inherit rustToolchain enforceDeterministicHash;
};
# DRY helper to build servers for both variants and both linkage modes
mkKmsServer =
{
features,
ui,
static ? true,
enforceDeterministicHash ? true,
}:
pkgs.callPackage ./nix/kms-server.nix {
openssl312 = if static then openssl312-static else openssl312-dynamic;
inherit pkgs228;
rustPlatform = rustPlatform190;
version = kmsVersion;
inherit
features
ui
static
enforceDeterministicHash
;
};
# Build KMS server in both variants with static OpenSSL
kms-server-fips-static-openssl = mkKmsServer {
features = [ ];
ui = ui-fips;
static = true;
inherit enforceDeterministicHash;
};
kms-server-non-fips-static-openssl = mkKmsServer {
features = [ "non-fips" ];
ui = ui-non-fips;
static = true;
enforceDeterministicHash = false;
};
# Build KMS server with dynamic OpenSSL linking
kms-server-fips-dynamic-openssl = mkKmsServer {
features = [ ];
ui = ui-fips;
static = false;
enforceDeterministicHash = false;
};
kms-server-non-fips-dynamic-openssl = mkKmsServer {
features = [ "non-fips" ];
ui = ui-non-fips;
static = false;
enforceDeterministicHash = false;
};
# Docker images using dockerTools (minimal images)
docker-image-fips = pkgs.callPackage ./nix/docker.nix {
kmsServer = kms-server-fips-static-openssl;
variant = "fips";
version = kmsVersion;
};
docker-image-non-fips = pkgs.callPackage ./nix/docker.nix {
kmsServer = kms-server-non-fips-static-openssl;
variant = "non-fips";
version = kmsVersion;
};
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
;
# 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 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;
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}"
'';
}