asterinas/test/initramfs/nix/initramfs.nix

86 lines
3.1 KiB
Nix
Raw Normal View History

{ lib, stdenvNoCC, fetchFromGitHub, hostPlatform, writeClosure, busybox
, xfstests, apps, benchmark, syscall, dnsServer, pkgs }:
2025-07-04 08:37:47 +00:00
let
etc = lib.fileset.toSource {
root = ./../src/etc;
fileset = ./../src/etc;
};
gvisor_libs = if syscall != null && syscall.testSuite == "gvisor" then
builtins.path {
name = "gvisor-libs";
path = "/lib/x86_64-linux-gnu";
}
else
null;
resolv_conf = pkgs.callPackage ./resolv_conf.nix { dnsServer = dnsServer; };
2025-11-11 08:59:49 +00:00
# Whether the initramfs should include evtest, a common tool to debug input devices (`/dev/input/eventX`)
is_evtest_included = false;
all_pkgs = [ busybox etc resolv_conf ]
++ lib.optionals (xfstests != null) [ xfstests ]
++ lib.optionals (apps != null) [ apps.package ]
2025-07-04 08:37:47 +00:00
++ lib.optionals (benchmark != null) [ benchmark.package ]
2025-11-11 08:59:49 +00:00
++ lib.optionals (syscall != null) [ syscall.package ]
++ lib.optionals is_evtest_included [ pkgs.evtest ];
in stdenvNoCC.mkDerivation {
2025-07-04 08:37:47 +00:00
name = "initramfs";
buildCommand = ''
mkdir -p $out/{dev,etc,root,usr,opt,tmp,var,proc,sys}
mkdir -p $out/{benchmark,test,xfstests,ext2,exfat}
mkdir -p $out/xfstests/{test,scratch}
2025-07-04 08:37:47 +00:00
mkdir -p $out/usr/{bin,sbin,lib,lib64,local}
ln -sfn usr/bin $out/bin
ln -sfn usr/sbin $out/sbin
ln -sfn usr/lib $out/lib
ln -sfn usr/lib64 $out/lib64
cp -r ${busybox}/bin/* $out/bin/
2025-11-11 08:59:49 +00:00
${lib.optionalString is_evtest_included ''
cp -r ${pkgs.evtest}/bin/* $out/bin/
''}
2025-07-04 08:37:47 +00:00
cp -r ${etc}/* $out/etc/
cp ${resolv_conf}/resolv.conf $out/etc/
2025-07-17 09:37:02 +00:00
${lib.optionalString (apps != null) ''
cp -r ${apps.package}/* $out/test/
''}
2025-07-04 08:37:47 +00:00
${lib.optionalString (benchmark != null) ''
cp -r "${benchmark.package}"/* $out/benchmark/
''}
${lib.optionalString (syscall != null) ''
cp -r "${syscall.package}"/opt/* $out/opt/
''}
2025-07-04 08:37:47 +00:00
${lib.optionalString (syscall != null && syscall.testSuite == "gvisor") ''
2025-07-04 08:37:47 +00:00
# FIXME: Build gvisor syscall test with nix to avoid manual library copying.
mkdir -p $out/lib/x86_64-linux-gnu
cp -L ${gvisor_libs}/ld-linux-x86-64.so.2 $out/lib64/ld-linux-x86-64.so.2
cp -L ${gvisor_libs}/libstdc++.so.6 $out/lib/x86_64-linux-gnu/libstdc++.so.6
cp -L ${gvisor_libs}/libgcc_s.so.1 $out/lib/x86_64-linux-gnu/libgcc_s.so.1
cp -L ${gvisor_libs}/libc.so.6 $out/lib/x86_64-linux-gnu/libc.so.6
cp -L ${gvisor_libs}/libm.so.6 $out/lib/x86_64-linux-gnu/libm.so.6
2025-07-04 08:37:47 +00:00
''}
${lib.optionalString (xfstests != null) ''
# Copy xfstests package content
cp -r ${xfstests}/xfstests/* $out/xfstests/
''}
2025-07-04 08:37:47 +00:00
# Use `writeClosure` to retrieve all dependencies of the specified packages.
# This will generate a text file containing the complete closure of the packages,
# including the packages themselves.
# The output of `writeClosure` is equivalent to `nix-store -q --requisites`.
2025-07-07 03:39:38 +00:00
mkdir -p $out/nix/store
2025-07-04 08:37:47 +00:00
pkg_path=${lib.strings.concatStringsSep ":" all_pkgs}
while IFS= read -r dep_path; do
if [[ "$pkg_path" == *"$dep_path"* ]]; then
continue
fi
2025-07-07 03:39:38 +00:00
cp -r $dep_path $out/nix/store/
2025-07-04 08:37:47 +00:00
done < ${writeClosure all_pkgs}
'';
}