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 # files which should be included in the initrd
, modules ? [ ] , 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 , withBusybox ? true
, withSignify ? true , withSignify ? true
# Inclusion of these is temporarily disabled. I had a bunch of # Inclusion of these is temporarily disabled. I had a bunch of
@ -31,46 +56,55 @@ let
nvramtool = nixpkgsOnBuildForHost.pkgsStatic.nvramtool.overrideAttrs(a: { nvramtool = nixpkgsOnBuildForHost.pkgsStatic.nvramtool.overrideAttrs(a: {
NIX_CFLAGS_COMPILE = "-D__GLIBC__"; 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 { in stdenv.mkDerivation {
name = "initramfs.cpio"; name = "initramfs.cpio";
dontUnpack = true; dontUnpack = true;
dontFixup = true; dontFixup = true;
modulesList = (lib.concatMapStringsSep "\n" (m: "${kernel.version}/kernel/" + m) modules)+"\n";
passAsFile = [ "modulesList" ];
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild
'' + lib.optionalString withMergedBinSbin ''
mkdir -p $NIX_BUILD_TOP/lib/modules ln -s bin sbin
pushd ${kernel}/lib/modules/ '' + lib.optionalString withMergedUsr ''
cat $modulesListPath | ${cpio}/bin/cpio -p -d $NIX_BUILD_TOP/lib/modules mkdir -p usr
popd ln -s ../bin usr/bin
ln -s ../sbin usr/sbin
mkdir -p $NIX_BUILD_TOP/usr '' + (lib.pipe contents' [
ln -s bin $NIX_BUILD_TOP/sbin (lib.mapAttrsToList (dest: src:
ln -s ../bin $NIX_BUILD_TOP/usr/bin if lib.hasSuffix "/" src then ''
ln -s ../sbin $NIX_BUILD_TOP/usr/sbin mkdir -p ${lib.escapeShellArg (builtins.dirOf dest)}
'' + lib.optionalString withBusybox '' cp -Tr ${lib.escapeShellArg src} ${lib.escapeShellArg dest}
cp -r ${busybox}/bin $NIX_BUILD_TOP/bin chmod -R u+w ${lib.escapeShellArg dest}
chmod -R u+w $NIX_BUILD_TOP/bin '' else ''
'' + lib.optionalString (bootScript != null) '' install -vDT ${lib.escapeShellArg src} ${lib.escapeShellArg dest}
cp ${bootScript} $NIX_BUILD_TOP/boot.sh ''))
chmod +x $NIX_BUILD_TOP/boot.sh lib.concatStrings
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/
'' + ''
runHook postBuild runHook postBuild
''; '';

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

Loading…
Cancel
Save