From ed925588f7277daef1859ce65485f2bf54927312 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Tue, 5 Dec 2023 17:29:01 +0000 Subject: [PATCH] extract common code to make root filesystem hierarchy which is then used by the filesystem image creators (ubifs, ext4, jffs2 etc) --- modules/ext4fs.nix | 19 +++++++++---------- modules/jffs2.nix | 14 ++++++++------ modules/outputs.nix | 21 ++++++++++++++++++++- modules/ubifs.nix | 20 +++++++++----------- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/modules/ext4fs.nix b/modules/ext4fs.nix index 6a8c7c8..e31dcce 100644 --- a/modules/ext4fs.nix +++ b/modules/ext4fs.nix @@ -6,6 +6,7 @@ }: let inherit (lib) mkIf mkOption types; + o = config.system.outputs; in { imports = [ @@ -18,7 +19,7 @@ in FS_ENCRYPTION = "y"; }; boot.initramfs.enable = true; - system.outputs = rec { + system.outputs = { systemConfiguration = pkgs.systemconfig config.filesystem.contents; rootfs = @@ -27,18 +28,16 @@ in in runCommand "mkfs.ext4" { depsBuildBuild = [ e2fsprogs ]; } '' - mkdir -p $TMPDIR/empty/nix/store/ $TMPDIR/empty/secrets - cp ${systemConfiguration}/bin/activate $TMPDIR/empty/activate - ln -s ${pkgs.s6-init-bin}/bin/init $TMPDIR/empty/init - mkdir -p $TMPDIR/empty/nix/store - for path in $(cat ${systemConfiguration}/etc/nix-store-paths) ; do - (cd $TMPDIR/empty && cp -a $path .$path) - done - size=$(du -s --apparent-size --block-size 1024 $TMPDIR/empty |cut -f1) + cp -a ${o.rootfsFiles} tmp + ${if config.boot.loader.extlinux.enable + then "(cd tmp && ln -s ${o.extlinux} boot)" + else "" + } + size=$(du -s --apparent-size --block-size 1024 tmp |cut -f1) # add 25% for filesystem overhead size=$(( 5 * $size / 4)) dd if=/dev/zero of=$out bs=1024 count=$size - mke2fs -t ext4 -j -d $TMPDIR/empty $out + mke2fs -t ext4 -j -d tmp $out ''; }; }; diff --git a/modules/jffs2.nix b/modules/jffs2.nix index d85ba26..a1b9c2a 100644 --- a/modules/jffs2.nix +++ b/modules/jffs2.nix @@ -6,6 +6,7 @@ }: let inherit (lib) mkIf mkOption types; + o = config.system.outputs; in { imports = [ @@ -22,7 +23,7 @@ in JFFS2_CMODE_SIZE = "y"; }; boot.initramfs.enable = true; - system.outputs = rec { + system.outputs = { systemConfiguration = pkgs.systemconfig config.filesystem.contents; rootfs = @@ -33,11 +34,12 @@ in in runCommand "make-jffs2" { depsBuildBuild = [ mtdutils ]; } '' - mkdir -p $TMPDIR/empty/nix/store/ $TMPDIR/empty/secrets - cp ${systemConfiguration}/bin/activate $TMPDIR/empty/activate - ln -s ${pkgs.s6-init-bin}/bin/init $TMPDIR/empty/init - grafts=$(sed < ${systemConfiguration}/etc/nix-store-paths 's/^\(.*\)$/--graft \1:\1/g') - mkfs.jffs2 --compression-mode=size ${endian} -e ${toString config.hardware.flash.eraseBlockSize} --enable-compressor=lzo --pad --root $TMPDIR/empty --output $out $grafts --squash --faketime + cp -a ${o.rootfsFiles} tmp + ${if config.boot.loader.extlinux.enable + then "(cd tmp && ln -s ${o.extlinux} boot)" + else "" + } + (cd tmp && mkfs.jffs2 --compression-mode=size ${endian} -e ${toString config.hardware.flash.eraseBlockSize} --enable-compressor=lzo --pad --root . --output $out --squash --faketime ) ''; }; }; diff --git a/modules/outputs.nix b/modules/outputs.nix index df2343d..ce2ba66 100644 --- a/modules/outputs.nix +++ b/modules/outputs.nix @@ -7,6 +7,7 @@ let inherit (lib) mkOption types concatStringsSep; inherit (pkgs) liminix callPackage writeText; + o = config.system.outputs; in { imports = [ @@ -58,6 +59,13 @@ in out what's in the image, which is nice if it's unexpectedly huge ''; }; + rootfsFiles = mkOption { + type = types.package; + internal = true; + description = '' + directory of files to package into root filesystem + ''; + }; rootfs = mkOption { type = types.package; internal = true; @@ -69,7 +77,6 @@ in }; config = { system.outputs = rec { - # tftpd = pkgs.buildPackages.tufted; kernel = liminix.builders.kernel.override { inherit (config.kernel) config src extraPatchPhase; }; @@ -87,6 +94,18 @@ in inherit kernel; inherit dtb; }; + rootfsFiles = + let + inherit (pkgs.pkgsBuildBuild) runCommand; + in runCommand "mktree" { } '' + mkdir -p $out/nix/store/ $out/secrets $out/boot + cp ${o.systemConfiguration}/bin/activate $out/activate + ln -s ${pkgs.s6-init-bin}/bin/init $out/init + mkdir -p $out/nix/store + for path in $(cat ${o.systemConfiguration}/etc/nix-store-paths) ; do + (cd $out && cp -a $path .$path) + done + ''; manifest = writeText "manifest.json" (builtins.toJSON config.filesystem.contents); }; }; diff --git a/modules/ubifs.nix b/modules/ubifs.nix index b04c128..4f93cf0 100644 --- a/modules/ubifs.nix +++ b/modules/ubifs.nix @@ -6,6 +6,7 @@ }: let inherit (lib) mkIf mkOption types; + o = config.system.outputs; in { imports = [ @@ -20,12 +21,11 @@ in config = mkIf (config.rootfsType == "ubifs") { kernel.config = { MTD_UBI="y"; - UBIFS_FS = "y"; UBIFS_FS_SECURITY = "n"; }; boot.initramfs.enable = true; - system.outputs = rec { + system.outputs = { systemConfiguration = pkgs.systemconfig config.filesystem.contents; rootfs = @@ -35,15 +35,13 @@ in in runCommand "mkfs.ubifs" { depsBuildBuild = [ mtdutils ]; } '' - mkdir -p $TMPDIR/empty/nix/store/ $TMPDIR/empty/secrets $TMPDIR/empty/boot - cp ${systemConfiguration}/bin/activate $TMPDIR/empty/activate - ln -s ${pkgs.s6-init-bin}/bin/init $TMPDIR/empty/init - cp ${config.system.outputs.uimage} $TMPDIR/empty/boot/uimage - mkdir -p $TMPDIR/empty/nix/store - for path in $(cat ${systemConfiguration}/etc/nix-store-paths) ; do - (cd $TMPDIR/empty && cp -a $path .$path) - done - mkfs.ubifs -x favor_lzo -c ${cfg.maxLEBcount} -m ${cfg.minIOSize} -e ${cfg.eraseBlockSize} -y -r $TMPDIR/empty --output $out --squash-uids -o $out + mkdir tmp + cp -a ${o.rootfsFiles} tmp + ${if config.boot.loader.extlinux.enable + then "(cd tmp && ln -s ${o.extlinux} boot)" + else "" + } + mkfs.ubifs -x favor_lzo -c ${cfg.maxLEBcount} -m ${cfg.minIOSize} -e ${cfg.eraseBlockSize} -y -r tmp --output $out --squash-uids -o $out ''; }; };