1{
2 lib,
3 stdenv,
4 squashfsTools,
5 closureInfo,
6
7 fileName ? "squashfs",
8 # The root directory of the squashfs filesystem is filled with the
9 # closures of the Nix store paths listed here.
10 storeContents ? [ ],
11 # Pseudo files to be added to squashfs image
12 pseudoFiles ? [ ],
13 noStrip ? false,
14 # Compression parameters.
15 # For zstd compression you can use "zstd -Xcompression-level 6".
16 comp ? "xz -Xdict-size 100%",
17 # create hydra build product. will put image in directory instead
18 # of directly in the store
19 hydraBuildProduct ? false,
20}:
21
22let
23 pseudoFilesArgs = lib.concatMapStrings (f: ''-p "${f}" '') pseudoFiles;
24 compFlag = if comp == null then "-no-compression" else "-comp ${comp}";
25in
26stdenv.mkDerivation {
27 name = "${fileName}${lib.optionalString (!hydraBuildProduct) ".img"}";
28 __structuredAttrs = true;
29
30 nativeBuildInputs = [ squashfsTools ];
31
32 buildCommand = ''
33 closureInfo=${closureInfo { rootPaths = storeContents; }}
34
35 # Also include a manifest of the closures in a format suitable
36 # for nix-store --load-db.
37 cp $closureInfo/registration nix-path-registration
38
39 imgPath="$out"
40 ''
41 + lib.optionalString hydraBuildProduct ''
42
43 mkdir $out
44 imgPath="$out/${fileName}.squashfs"
45 ''
46 + lib.optionalString stdenv.buildPlatform.is32bit ''
47
48 # 64 cores on i686 does not work
49 # fails with FATAL ERROR: mangle2:: xz compress failed with error code 5
50 if ((NIX_BUILD_CORES > 48)); then
51 NIX_BUILD_CORES=48
52 fi
53 ''
54 + ''
55
56 # Generate the squashfs image.
57 # We have to set SOURCE_DATE_EPOCH to 0 here for reproducibility (https://github.com/NixOS/nixpkgs/issues/390696)
58 SOURCE_DATE_EPOCH=0 mksquashfs nix-path-registration $(cat $closureInfo/store-paths) $imgPath ${pseudoFilesArgs} \
59 -no-hardlinks ${lib.optionalString noStrip "-no-strip"} -keep-as-directory -all-root -b 1048576 ${compFlag} \
60 -processors $NIX_BUILD_CORES -root-mode 0755
61 ''
62 + lib.optionalString hydraBuildProduct ''
63
64 mkdir -p $out/nix-support
65 echo "file squashfs-image $out/${fileName}.squashfs" >> $out/nix-support/hydra-build-products
66 '';
67}