2023-04-05 00:35:49 +02:00
|
|
|
{
|
|
|
|
config
|
|
|
|
, pkgs
|
|
|
|
, lib
|
|
|
|
, ...
|
|
|
|
}:
|
|
|
|
let
|
2023-07-13 20:24:59 +02:00
|
|
|
inherit (lib) mkEnableOption mkOption mkIf types;
|
2023-04-05 00:35:49 +02:00
|
|
|
inherit (pkgs) runCommand callPackage writeText;
|
|
|
|
in
|
|
|
|
{
|
2023-04-10 20:07:27 +02:00
|
|
|
options = {
|
|
|
|
boot.initramfs = {
|
2023-08-04 21:19:27 +02:00
|
|
|
enable = mkEnableOption "initramfs";
|
2023-04-10 20:07:27 +02:00
|
|
|
};
|
2023-10-30 22:23:50 +01:00
|
|
|
system.outputs = {
|
|
|
|
initramfs = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
internal = true;
|
|
|
|
description = ''
|
|
|
|
Initramfs image capable of mounting the real root
|
|
|
|
filesystem
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
systemConfiguration = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
description = ''
|
|
|
|
pkgs.systemconfig for the configured filesystem,
|
|
|
|
contains 'activate' and 'init' commands
|
|
|
|
'';
|
|
|
|
internal = true;
|
|
|
|
};
|
2023-07-13 20:24:59 +02:00
|
|
|
};
|
2023-04-10 20:07:27 +02:00
|
|
|
};
|
|
|
|
config = mkIf config.boot.initramfs.enable {
|
2023-04-15 18:35:02 +02:00
|
|
|
kernel.config = {
|
|
|
|
BLK_DEV_INITRD = "y";
|
2023-07-13 20:24:59 +02:00
|
|
|
INITRAMFS_SOURCE = builtins.toJSON "${config.system.outputs.initramfs}";
|
2024-09-07 22:30:51 +02:00
|
|
|
INITRAMFS_COMPRESSION_ZSTD = "y";
|
2023-04-15 18:35:02 +02:00
|
|
|
};
|
2023-04-05 00:35:49 +02:00
|
|
|
|
2023-07-13 20:24:59 +02:00
|
|
|
system.outputs = {
|
2023-04-05 00:35:49 +02:00
|
|
|
initramfs =
|
2024-09-07 22:28:05 +02:00
|
|
|
let
|
|
|
|
inherit (pkgs.pkgsBuildBuild) gen_init_cpio cpio writeScript;
|
|
|
|
inherit (pkgs) busybox;
|
|
|
|
failsafe-init = writeScript "init" ''
|
|
|
|
#!/bin/sh
|
|
|
|
exec >/dev/console
|
|
|
|
echo Running in initramfs
|
|
|
|
PATH=${busybox}/bin:$PATH
|
|
|
|
export PATH
|
|
|
|
mount -t proc none /proc
|
|
|
|
mount -t sysfs none /sys
|
|
|
|
${busybox}/bin/sh
|
|
|
|
'';
|
|
|
|
refs = pkgs.writeReferencesToFile busybox;
|
2023-04-10 21:35:06 +02:00
|
|
|
in runCommand "initramfs.cpio" {} ''
|
2024-09-07 22:28:05 +02:00
|
|
|
cat << SPECIALS | ${gen_init_cpio}/bin/gen_init_cpio /dev/stdin > out
|
2023-04-05 00:35:49 +02:00
|
|
|
dir /proc 0755 0 0
|
|
|
|
dir /dev 0755 0 0
|
2023-04-15 18:35:02 +02:00
|
|
|
nod /dev/console 0600 0 0 c 5 1
|
2023-04-05 00:35:49 +02:00
|
|
|
dir /target 0755 0 0
|
|
|
|
dir /target/persist 0755 0 0
|
|
|
|
dir /target/nix 0755 0 0
|
2024-09-07 22:28:05 +02:00
|
|
|
dir /nix 0755 0 0
|
|
|
|
dir /nix/store 0755 0 0
|
|
|
|
dir /bin 0755 0 0
|
|
|
|
file /bin/sh ${busybox}/bin/sh 0755 0 0
|
2023-04-15 18:35:02 +02:00
|
|
|
file /init ${pkgs.preinit}/bin/preinit 0755 0 0
|
2024-09-07 22:28:05 +02:00
|
|
|
file /failsafe-init ${failsafe-init} 0755 0 0
|
2023-04-05 00:35:49 +02:00
|
|
|
SPECIALS
|
2024-09-07 22:28:05 +02:00
|
|
|
|
|
|
|
find $(cat ${refs}) | ${cpio}/bin/cpio -H newc -o -A -v -O out
|
|
|
|
mv out $out
|
2023-04-05 00:35:49 +02:00
|
|
|
'';
|
2023-12-11 22:47:15 +01:00
|
|
|
systemConfiguration =
|
|
|
|
pkgs.systemconfig config.filesystem.contents;
|
2023-04-05 00:35:49 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|