initramfs: pass contents using an attrset

This commit significantly cleans up the assembly of the initramfs by
specifying and manipulating its contents using an attrset (in { dest
= src; } form) rather than chunks of bash script.
master
Adam Joseph 1 year ago
parent 6dbb2f8c92
commit a2c0e1ddfb

@ -12,6 +12,31 @@
# files which should be included in the initrd
, modules ? [ ]
# An attrset containing additional files to include in the initramfs
# image. Each attrname in this set is a path relative to the root
# of the initramfs (e.g. "bin/signify") and the corresponding value is
# the file to be copied to that location (e.g. "${signify}/bin/signify).
#
# - If the attrvalue has a trailing "/" then both the source and
# destination are considered to be directories; the source will be
# copied recursively, and symbolic links therein will be preserved
# (i.e. not dereferenced). See `withBusybox` below for an example.
#
# - If the attrvalue does NOT have a trailing "/" then both the
# source and destination are considered to be files. If the
# source is a symbolic link it will be dereferenced before copying.
#
# After copying, `chmod -R u+w` is performed, since the contents are
# likely to be coming from /nix/store where Nix clears the u-w bit.
#
, contents ? {}
# cause usr/{bin,sbin} to be symlinks to {bin/sbin}
, withMergedUsr ? true
# cause sbin to be a symlink to bin
, withMergedBinSbin ? true
, withBusybox ? true
, withSignify ? true
# Inclusion of these is temporarily disabled. I had a bunch of
@ -31,46 +56,55 @@ let
nvramtool = nixpkgsOnBuildForHost.pkgsStatic.nvramtool.overrideAttrs(a: {
NIX_CFLAGS_COMPILE = "-D__GLIBC__";
});
contents' = (lib.pipe modules [
(map (m: let name = "${kernel.version}/kernel/${m}";
in {
name = "lib/modules/${name}"; # dest
value = "${kernel}/lib/modules/${name}"; # source
}))
lib.listToAttrs
]) // lib.optionalAttrs withBusybox {
"bin" = "${busybox}/bin/";
} // lib.optionalAttrs withSignify {
"bin/signify" = "${signify}/bin/signify";
} // lib.optionalAttrs withLvm {
"bin/lvm" = "${lib.getBin lvm2}/bin/lvm";
"bin/dmsetup" = "${lib.getBin lvm2}/bin/dmsetup";
} // lib.optionalAttrs withCryptsetup {
"bin/cryptsetup" = "${cryptsetup}/bin/cryptsetup";
} // lib.optionalAttrs withKexec {
"bin/kexec" = "${kexec-tools}/bin/kexec";
} // lib.optionalAttrs withNvramTool {
"bin/nvramtool" = "${nvramtool}/bin/nvramtool";
} // lib.optionalAttrs (bootScript != null) {
"sbin/init" = "${bootScript}";
} // contents;
in stdenv.mkDerivation {
name = "initramfs.cpio";
dontUnpack = true;
dontFixup = true;
modulesList = (lib.concatMapStringsSep "\n" (m: "${kernel.version}/kernel/" + m) modules)+"\n";
passAsFile = [ "modulesList" ];
buildPhase = ''
runHook preBuild
mkdir -p $NIX_BUILD_TOP/lib/modules
pushd ${kernel}/lib/modules/
cat $modulesListPath | ${cpio}/bin/cpio -p -d $NIX_BUILD_TOP/lib/modules
popd
mkdir -p $NIX_BUILD_TOP/usr
ln -s bin $NIX_BUILD_TOP/sbin
ln -s ../bin $NIX_BUILD_TOP/usr/bin
ln -s ../sbin $NIX_BUILD_TOP/usr/sbin
'' + lib.optionalString withBusybox ''
cp -r ${busybox}/bin $NIX_BUILD_TOP/bin
chmod -R u+w $NIX_BUILD_TOP/bin
'' + lib.optionalString (bootScript != null) ''
cp ${bootScript} $NIX_BUILD_TOP/boot.sh
chmod +x $NIX_BUILD_TOP/boot.sh
ln -s boot.sh $NIX_BUILD_TOP/init
chmod +x $NIX_BUILD_TOP/sbin/init
'' + lib.optionalString withSignify ''
cp ${signify}/bin/signify $NIX_BUILD_TOP/bin/
'' + lib.optionalString withLvm ''
cp ${lib.getBin lvm2}/bin/lvm $NIX_BUILD_TOP/bin/
cp ${lib.getBin lvm2}/bin/dmsetup $NIX_BUILD_TOP/bin/
'' + lib.optionalString withCryptsetup ''
cp ${cryptsetup}/bin/cryptsetup $NIX_BUILD_TOP/bin/
'' + lib.optionalString withKexec ''
cp ${kexec-tools}/bin/kexec $NIX_BUILD_TOP/bin/
'' + lib.optionalString withNvramTool ''
cp ${nvramtool}/bin/nvramtool $NIX_BUILD_TOP/bin/
'' + ''
'' + lib.optionalString withMergedBinSbin ''
ln -s bin sbin
'' + lib.optionalString withMergedUsr ''
mkdir -p usr
ln -s ../bin usr/bin
ln -s ../sbin usr/sbin
'' + (lib.pipe contents' [
(lib.mapAttrsToList (dest: src:
if lib.hasSuffix "/" src then ''
mkdir -p ${lib.escapeShellArg (builtins.dirOf dest)}
cp -Tr ${lib.escapeShellArg src} ${lib.escapeShellArg dest}
chmod -R u+w ${lib.escapeShellArg dest}
'' else ''
install -vDT ${lib.escapeShellArg src} ${lib.escapeShellArg dest}
''))
lib.concatStrings
]) +
''
runHook postBuild
'';

@ -17,12 +17,11 @@
'';
});
initramfs = prev.initramfs.overrideAttrs (a: {
postBuild = (a.postBuild or "") + ''
mkdir -p $NIX_BUILD_TOP/etc
cp ${final.image.src}/src/mainboard/asus/kgpe-d16/cmos.layout $NIX_BUILD_TOP/etc/
cp ${final.image.src}/src/mainboard/asus/kgpe-d16/cmos.default $NIX_BUILD_TOP/etc/
'';
initramfs = prev.initramfs.override (previousArgs: {
contents = (previousArgs.contents or {}) // {
"etc/cmos.layout" = "${final.image.src}/src/mainboard/asus/kgpe-d16/cmos.layout";
"etc/cmos.default" = "${final.image.src}/src/mainboard/asus/kgpe-d16/cmos.default";
};
});
# microcode updates are needed only for Opteron 63xx

Loading…
Cancel
Save