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 # the image will be self-contained so we can drop references
31 # to the closure that was used to build it
32 unsafeDiscardReferences.out = true;
33
34 nativeBuildInputs = [ squashfsTools ];
35
36 buildCommand = ''
37 closureInfo=${closureInfo { rootPaths = storeContents; }}
38
39 # Also include a manifest of the closures in a format suitable
40 # for nix-store --load-db.
41 cp $closureInfo/registration nix-path-registration
42
43 imgPath="$out"
44 ''
45 + lib.optionalString hydraBuildProduct ''
46
47 mkdir $out
48 imgPath="$out/${fileName}.squashfs"
49 ''
50 + lib.optionalString stdenv.buildPlatform.is32bit ''
51
52 # 64 cores on i686 does not work
53 # fails with FATAL ERROR: mangle2:: xz compress failed with error code 5
54 if ((NIX_BUILD_CORES > 48)); then
55 NIX_BUILD_CORES=48
56 fi
57 ''
58 + ''
59
60 # Generate the squashfs image.
61 # We have to set SOURCE_DATE_EPOCH to 0 here for reproducibility (https://github.com/NixOS/nixpkgs/issues/390696)
62 SOURCE_DATE_EPOCH=0 mksquashfs nix-path-registration $(cat $closureInfo/store-paths) $imgPath ${pseudoFilesArgs} \
63 -no-hardlinks ${lib.optionalString noStrip "-no-strip"} -keep-as-directory -all-root -b 1048576 ${compFlag} \
64 -processors $NIX_BUILD_CORES -root-mode 0755
65 ''
66 + lib.optionalString hydraBuildProduct ''
67
68 mkdir -p $out/nix-support
69 echo "file squashfs-image $out/${fileName}.squashfs" >> $out/nix-support/hydra-build-products
70 '';
71}