1 # This is an expression meant to be called from `./repart.nix`, it is NOT a
2 # NixOS module that can be imported.
36 , definitionsDirectory
44 inherit (stdenvNoCC) hostPlatform;
46 if hostPlatform.isAarch32 then "arm"
47 else if hostPlatform.isAarch64 then "arm64"
48 else if hostPlatform.isx86_32 then "x86"
49 else if hostPlatform.isx86_64 then "x86-64"
50 else if hostPlatform.isMips32 then "mips-le"
51 else if hostPlatform.isMips64 then "mips64-le"
52 else if hostPlatform.isPower then "ppc"
53 else if hostPlatform.isPower64 then "ppc64"
54 else if hostPlatform.isRiscV32 then "riscv32"
55 else if hostPlatform.isRiscV64 then "riscv64"
56 else if hostPlatform.isS390 then "s390"
57 else if hostPlatform.isS390x then "s390x"
58 else if hostPlatform.isLoongArch64 then "loongarch64"
59 else if hostPlatform.isAlpha then "alpha"
60 else hostPlatform.parsed.cpu.name;
62 amendRepartDefinitions = runCommand "amend-repart-definitions.py"
64 # TODO: ruff does not splice properly in nativeBuildInputs
65 depsBuildBuild = [ ruff ];
66 nativeBuildInputs = [ python3 black mypy ];
68 install ${./amend-repart-definitions.py} $out
69 patchShebangs --build $out
71 black --check --diff $out
72 ruff check --line-length 88 $out
76 fileSystemToolMapping = {
77 "vfat" = [ dosfstools mtools ];
78 "ext4" = [ e2fsprogs.bin ];
79 "squashfs" = [ squashfsTools ];
80 "erofs" = [ erofs-utils ];
81 "btrfs" = [ btrfs-progs ];
85 fileSystemTools = builtins.concatMap (f: fileSystemToolMapping."${f}") fileSystems;
90 }."${compression.algorithm}";
92 compressionCommand = {
93 "zstd" = "zstd --no-progress --threads=$NIX_BUILD_CORES -${toString compression.level}";
94 "xz" = "xz --keep --verbose --threads=$NIX_BUILD_CORES -${toString compression.level}";
95 }."${compression.algorithm}";
97 stdenvNoCC.mkDerivation (finalAttrs:
99 then { pname = name; inherit version; }
100 else { inherit name; }
102 __structuredAttrs = true;
105 # the image will be self-contained so we can drop references
106 # to the closure that was used to build it
107 unsafeDiscardReferences.out = true;
109 nativeBuildInputs = [
112 ] ++ lib.optionals (compression.enable) [
114 ] ++ fileSystemTools;
118 inherit finalPartitions definitionsDirectory;
120 partitionsJSON = builtins.toJSON finalAttrs.finalPartitions;
122 # relative path to the repart definitions that are read by systemd-repart
123 finalRepartDefinitions = "repart.d";
125 systemdRepartFlags = [
126 "--architecture=${systemdArch}"
130 "--definitions=${finalAttrs.finalRepartDefinitions}"
131 "--split=${lib.boolToString split}"
133 ] ++ lib.optionals createEmpty [
135 ] ++ lib.optionals (sectorSize != null) [
136 "--sector-size=${toString sectorSize}"
140 dontConfigure = true;
146 amendedRepartDefinitionsDir=$(${amendRepartDefinitions} <(echo "$partitionsJSON") $definitionsDirectory)
147 ln -vs $amendedRepartDefinitionsDir $finalRepartDefinitions
155 echo "Building image with systemd-repart..."
156 fakeroot systemd-repart \
157 ''${systemdRepartFlags[@]} \
158 ${imageFileBasename}.raw \
159 | tee repart-output.json
169 # Compression is implemented in the same derivation as opposed to in a
170 # separate derivation to allow users to save disk space. Disk images are
171 # already very space intensive so we want to allow users to mitigate this.
172 + lib.optionalString compression.enable
174 for f in ${imageFileBasename}*; do
175 echo "Compressing $f with ${compression.algorithm}..."
176 # Keep the original file when compressing and only delete it afterwards
177 ${compressionCommand} $f && rm $f
180 mv -v repart-output.json ${imageFileBasename}* $out
186 inherit amendRepartDefinitions;