Skip to content

Commit 2ac6268

Browse files
author
Naman Jain
committed
Use pure Nix environment for reproducible kernel builds
Ensure only Nix-provided tools are used during reproducible builds, preventing system package leakage that could affect reproducibility. Changes: - Add --ignore-environment to nix develop for pure shell - Keep essential env vars: HOME, USER, TERM - Explicitly set CC=gcc to use Nix's GCC in all scenarios - Detect host architecture to avoid cross-compiler on native builds - Add LOCALVERSION= to prevent '+' suffix in version string - Add shell utilities to flake.nix (getopt, coreutils, rsync, etc.) - Print SHA256 checksum of vmlinux for verification This ensures cross-compiled and native builds use the correct compiler identification strings for reproducibility.
1 parent 13cf677 commit 2ac6268

3 files changed

Lines changed: 62 additions & 6 deletions

File tree

Microsoft/build-hcl-kernel.sh

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,46 @@ if test -z "$arch"; then
7070
arch=("x64")
7171
fi
7272

73+
# Detect host architecture
74+
HOST_ARCH="$(uname -m)"
75+
7376
objcopy=("objcopy")
74-
makeargs=("ARCH=x86_64")
77+
if [ -n "$REPRODUCIBLE_BUILD" ]; then
78+
# For reproducible builds, explicitly set CC to use Nix's gcc
79+
makeargs=("ARCH=x86_64" "CC=gcc")
80+
else
81+
makeargs=("ARCH=x86_64")
82+
fi
7583
targets=("vmlinux modules")
7684
if [ "$arch" = "arm64" ]; then
77-
# Use Nix cross-compiler prefix for reproducible builds, system prefix otherwise
78-
if [ -n "$REPRODUCIBLE_BUILD" ]; then
85+
# Only use cross-compiler when cross-compiling (host != target)
86+
if [ "$HOST_ARCH" = "aarch64" ]; then
87+
# Native arm64 build - no cross-compile prefix needed
88+
cross_prefix=""
89+
elif [ -n "$REPRODUCIBLE_BUILD" ]; then
90+
# Cross-compiling from x86_64 with Nix toolchain
7991
cross_prefix="aarch64-unknown-linux-gnu-"
8092
else
93+
# Cross-compiling from x86_64 with system toolchain
8194
cross_prefix="aarch64-linux-gnu-"
8295
fi
83-
objcopy=("${cross_prefix}objcopy")
84-
makeargs=("ARCH=arm64" "CROSS_COMPILE=${cross_prefix}")
96+
97+
if [ -n "$cross_prefix" ]; then
98+
objcopy=("${cross_prefix}objcopy")
99+
# For reproducible builds, explicitly set CC to use the Nix cross-compiler
100+
if [ -n "$REPRODUCIBLE_BUILD" ]; then
101+
makeargs=("ARCH=arm64" "CROSS_COMPILE=${cross_prefix}" "CC=${cross_prefix}gcc")
102+
else
103+
makeargs=("ARCH=arm64" "CROSS_COMPILE=${cross_prefix}")
104+
fi
105+
else
106+
# For native builds, explicitly set CC to ensure we use Nix's gcc in reproducible mode
107+
if [ -n "$REPRODUCIBLE_BUILD" ]; then
108+
makeargs=("ARCH=arm64" "CC=gcc")
109+
else
110+
makeargs=("ARCH=arm64")
111+
fi
112+
fi
85113
targets=("vmlinux Image modules")
86114
fi
87115

@@ -94,6 +122,8 @@ SRC_DIR=`realpath ${SCRIPT_DIR}/..`
94122
if [ -n "$REPRODUCIBLE_BUILD" ]; then
95123
makeargs+=("KBUILD_BUILD_ID=none")
96124
makeargs+=("KCFLAGS=-fdebug-prefix-map=$SRC_DIR=.")
125+
# Prevent + suffix from being added to version string
126+
makeargs+=("LOCALVERSION=")
97127
fi
98128

99129
build_kernel() {

Microsoft/nix-build.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ if [ -z "${IN_NIX_SHELL:-}" ]; then
2121
fi
2222

2323
# Re-execute this script inside nix develop with experimental features enabled
24-
exec nix --extra-experimental-features "nix-command flakes" develop --command "$0" "$@"
24+
# Use --ignore-environment (-i) to create a pure shell that excludes system packages
25+
# Keep essential variables: HOME (for temp files), USER (for build metadata), TERM (for output)
26+
exec nix --extra-experimental-features "nix-command flakes" develop \
27+
--ignore-environment \
28+
--keep-env-var HOME \
29+
--keep-env-var USER \
30+
--keep-env-var TERM \
31+
--command "$0" "$@"
2532
fi
2633

2734
# Script directory
@@ -120,6 +127,13 @@ main() {
120127

121128
log_info "Build completed successfully!"
122129
log_info "Build artifacts are in: ${BUILD_OUTPUT}"
130+
131+
# Print sha256sum of vmlinux for reproducibility verification
132+
if [ -f "${BUILD_OUTPUT}/vmlinux" ]; then
133+
echo ""
134+
log_info "Reproducibility verification:"
135+
echo " vmlinux sha256sum: $(sha256sum "${BUILD_OUTPUT}/vmlinux" | cut -d' ' -f1)"
136+
fi
123137
}
124138

125139
# Handle command line arguments

flake.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
perl
2727
python3
2828

29+
# Shell utilities needed for build scripts
30+
getopt
31+
coreutils
32+
findutils
33+
gnugrep
34+
gnused
35+
gawk
36+
bash
37+
rsync
38+
hostname
39+
which
40+
2941
# ARM64 cross-compilation toolchain
3042
pkgsCross.aarch64-multiplatform.stdenv.cc
3143
pkgsCross.aarch64-multiplatform.buildPackages.binutils

0 commit comments

Comments
 (0)