2023-12-18 22:33:04 +01:00
|
|
|
# This is an expression meant to be called from `./repart.nix`, it is NOT a
|
|
|
|
# NixOS module that can be imported.
|
|
|
|
|
|
|
|
{ lib
|
|
|
|
, runCommand
|
|
|
|
, python3
|
|
|
|
, black
|
|
|
|
, ruff
|
|
|
|
, mypy
|
|
|
|
, systemd
|
|
|
|
, fakeroot
|
|
|
|
, util-linux
|
2023-12-30 00:15:17 +01:00
|
|
|
|
|
|
|
# filesystem tools
|
2023-12-18 22:33:04 +01:00
|
|
|
, dosfstools
|
|
|
|
, mtools
|
|
|
|
, e2fsprogs
|
|
|
|
, squashfsTools
|
|
|
|
, erofs-utils
|
|
|
|
, btrfs-progs
|
|
|
|
, xfsprogs
|
|
|
|
|
2023-12-30 00:15:17 +01:00
|
|
|
# compression tools
|
|
|
|
, zstd
|
|
|
|
, xz
|
|
|
|
|
2023-12-18 22:33:04 +01:00
|
|
|
# arguments
|
2023-12-30 00:15:17 +01:00
|
|
|
, imageFileBasename
|
|
|
|
, compression
|
2023-12-18 22:33:04 +01:00
|
|
|
, fileSystems
|
|
|
|
, partitions
|
|
|
|
, split
|
|
|
|
, seed
|
|
|
|
, definitionsDirectory
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
amendRepartDefinitions = runCommand "amend-repart-definitions.py"
|
|
|
|
{
|
|
|
|
# TODO: ruff does not splice properly in nativeBuildInputs
|
|
|
|
depsBuildBuild = [ ruff ];
|
|
|
|
nativeBuildInputs = [ python3 black mypy ];
|
|
|
|
} ''
|
|
|
|
install ${./amend-repart-definitions.py} $out
|
|
|
|
patchShebangs --build $out
|
|
|
|
|
|
|
|
black --check --diff $out
|
|
|
|
ruff --line-length 88 $out
|
|
|
|
mypy --strict $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
fileSystemToolMapping = {
|
|
|
|
"vfat" = [ dosfstools mtools ];
|
|
|
|
"ext4" = [ e2fsprogs.bin ];
|
|
|
|
"squashfs" = [ squashfsTools ];
|
|
|
|
"erofs" = [ erofs-utils ];
|
|
|
|
"btrfs" = [ btrfs-progs ];
|
|
|
|
"xfs" = [ xfsprogs ];
|
|
|
|
};
|
|
|
|
|
|
|
|
fileSystemTools = builtins.concatMap (f: fileSystemToolMapping."${f}") fileSystems;
|
2023-12-30 00:15:17 +01:00
|
|
|
|
|
|
|
compressionPkg = {
|
|
|
|
"zstd" = zstd;
|
|
|
|
"xz" = xz;
|
|
|
|
}."${compression.algorithm}";
|
|
|
|
|
|
|
|
compressionCommand = {
|
|
|
|
"zstd" = "zstd --no-progress --threads=0 -${toString compression.level}";
|
|
|
|
"xz" = "xz --keep --verbose --threads=0 -${toString compression.level}";
|
|
|
|
}."${compression.algorithm}";
|
2023-12-18 22:33:04 +01:00
|
|
|
in
|
|
|
|
|
2023-12-30 00:15:17 +01:00
|
|
|
runCommand imageFileBasename
|
2023-12-18 22:33:04 +01:00
|
|
|
{
|
|
|
|
nativeBuildInputs = [
|
|
|
|
systemd
|
|
|
|
fakeroot
|
|
|
|
util-linux
|
2023-12-30 00:15:17 +01:00
|
|
|
compressionPkg
|
2023-12-18 22:33:04 +01:00
|
|
|
] ++ fileSystemTools;
|
|
|
|
} ''
|
|
|
|
amendedRepartDefinitions=$(${amendRepartDefinitions} ${partitions} ${definitionsDirectory})
|
|
|
|
|
|
|
|
mkdir -p $out
|
|
|
|
cd $out
|
|
|
|
|
2023-12-30 00:15:17 +01:00
|
|
|
echo "Building image with systemd-repart..."
|
2023-12-18 22:33:04 +01:00
|
|
|
unshare --map-root-user fakeroot systemd-repart \
|
|
|
|
--dry-run=no \
|
|
|
|
--empty=create \
|
|
|
|
--size=auto \
|
|
|
|
--seed="${seed}" \
|
|
|
|
--definitions="$amendedRepartDefinitions" \
|
|
|
|
--split="${lib.boolToString split}" \
|
|
|
|
--json=pretty \
|
2023-12-30 00:15:17 +01:00
|
|
|
${imageFileBasename}.raw \
|
2023-12-18 22:33:04 +01:00
|
|
|
| tee repart-output.json
|
2023-12-30 00:15:17 +01:00
|
|
|
|
|
|
|
# Compression is implemented in the same derivation as opposed to in a
|
|
|
|
# separate derivation to allow users to save disk space. Disk images are
|
|
|
|
# already very space intensive so we want to allow users to mitigate this.
|
|
|
|
if ${lib.boolToString compression.enable}; then
|
|
|
|
for f in ${imageFileBasename}*; do
|
|
|
|
echo "Compressing $f with ${compression.algorithm}..."
|
|
|
|
# Keep the original file when compressing and only delete it afterwards
|
|
|
|
${compressionCommand} $f && rm $f
|
|
|
|
done
|
|
|
|
fi
|
2023-12-18 22:33:04 +01:00
|
|
|
''
|