Skip to content

Commit bc590ec

Browse files
committed
add disk script
Why === * The nix modules disk builds too slowly. * Moving the squashfs and tarball steps out of nix should make it faster * We can also speed up things and make the disk smaller with different compression choices What changed === * move squashfs and tarball steps into one script called disk-script that you can run with `nix run -L .#disk-script` or `nix run -L .#disk-script-dev` * run xargs cp in parallel * use lz4 compression for squashfs and 1M block size * use --best --recursive for pigz compression on tarball * add output for the time each stage takes Test plan === * ran it locally ``` xargs copy took 306 seconds mksquashfs took 148 seconds tar took 140 seconds ``` ~10 minutes ``` $ du -h /tmp/tmp.Lj42PAdmKN/disk.sqsh 21G /tmp/tmp.Lj42PAdmKN/disk.sqsh $ du -h /tmp/tmp.Lj42PAdmKN/disk.sqsh.tar.gz 17G /tmp/tmp.Lj42PAdmKN/disk.sqsh.tar.gz ```
1 parent a9b0cdb commit bc590ec

2 files changed

Lines changed: 103 additions & 10 deletions

File tree

pkgs/default.nix

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,45 @@ let
77
revstring_long = self.rev or "dirty";
88
revstring = builtins.substring 0 7 revstring_long;
99

10-
dev-module-ids = [ "python-3.10" "python-3.11" "nodejs-18" "nodejs-20" "nodejs-22" "go-1.21" "docker" "replit" "replit-rtld-loader" "ruby" "ruby-3.2" "postgresql-16" ];
11-
12-
mkPhonyOCI = pkgs.callPackage ./mk-phony-oci { ztoc-rs = self.inputs.ztoc-rs.packages.x86_64-linux.default; };
10+
dev-module-ids = [
11+
"python-3.10"
12+
"python-3.11"
13+
"nodejs-18"
14+
"nodejs-20"
15+
"nodejs-22"
16+
"go-1.21"
17+
"docker"
18+
"replit"
19+
"replit-rtld-loader"
20+
"ruby"
21+
"ruby-3.2"
22+
"postgresql-16"
23+
];
24+
25+
mkPhonyOCI = pkgs.callPackage ./mk-phony-oci {
26+
ztoc-rs = self.inputs.ztoc-rs.packages.x86_64-linux.default;
27+
};
1328

1429
bundle-fn = pkgs.callPackage ./bundle { inherit self; };
1530

16-
bundle-squashfs-fn = { moduleIds ? null, diskName ? "disk.raw" }:
31+
bundle-squashfs-fn =
32+
{ moduleIds ? null
33+
, diskName ? "disk.raw"
34+
,
35+
}:
1736
pkgs.callPackage ./bundle-image {
1837
bundle = bundle-fn { inherit moduleIds; };
1938
inherit revstring diskName;
2039
};
2140

41+
disk-script-fn =
42+
{ moduleIds ? null
43+
,
44+
}:
45+
pkgs.callPackage ./disk-script {
46+
bundle = bundle-fn { inherit moduleIds; };
47+
};
48+
2249
in
2350
rec {
2451
default = moduleit;
@@ -39,6 +66,12 @@ rec {
3966
# For prod use: builds the Nixmodules disk image
4067
bundle-image-tarball = pkgs.callPackage ./bundle-image-tarball { inherit bundle-image revstring; };
4168

69+
disk-script-dev = disk-script-fn {
70+
moduleIds = dev-module-ids;
71+
};
72+
73+
disk-script = disk-script-fn { };
74+
4275
# For dev use: builds the shared Nixmodules disk
4376
bundle-squashfs = bundle-squashfs-fn {
4477
moduleIds = dev-module-ids;
@@ -52,13 +85,16 @@ rec {
5285
};
5386

5487
phony-oci-bundles = mapAttrs
55-
(moduleId: _:
56-
mkPhonyOCI {
57-
inherit moduleId;
58-
module = self.deploymentModules.${moduleId};
59-
})
88+
(
89+
moduleId: _:
90+
mkPhonyOCI {
91+
inherit moduleId;
92+
module = self.deploymentModules.${moduleId};
93+
}
94+
)
6095
modules;
6196

6297
deploymentModules = self.deploymentModules;
6398

64-
} // modules
99+
}
100+
// modules

pkgs/disk-script/default.nix

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
writeShellApplication,
3+
bundle,
4+
squashfsTools,
5+
gnutar,
6+
pigz,
7+
coreutils,
8+
findutils,
9+
closureInfo,
10+
}:
11+
12+
let
13+
diskClosureInfo = closureInfo { rootPaths = [ bundle ]; };
14+
in
15+
writeShellApplication {
16+
name = "disk-script";
17+
runtimeInputs = [
18+
coreutils
19+
findutils
20+
gnutar
21+
squashfsTools
22+
pigz
23+
];
24+
text = ''
25+
set -x
26+
TMP_DIR=$(mktemp -d)
27+
28+
cd "$TMP_DIR"
29+
30+
root="$TMP_DIR/root"
31+
diskImage="$TMP_DIR/disk.sqsh"
32+
tarball="$TMP_DIR/disk.sqsh.tar.gz"
33+
34+
(
35+
mkdir -p "$root/nix/store" "$root/etc/nixmodules"
36+
37+
cp --archive --reflink=auto "${bundle}/etc/nixmodules/"* "$root/etc/nixmodules"
38+
39+
SECONDS=0
40+
xargs -P "$(nproc)" cp -a --reflink=auto -t "$root/nix/store/" < "${diskClosureInfo}/store-paths"
41+
echo "xargs copy took $SECONDS seconds" >&2
42+
43+
echo "making squashfs..."
44+
SECONDS=0
45+
mksquashfs "$root" "$diskImage" -force-uid 11000 -force-gid 11000 -comp lz4 -b 1M
46+
echo "mksquashfs took $SECONDS seconds" >&2
47+
48+
SECONDS=0
49+
tar --use-compress-program="pigz --best --recursive | pv" -Scf "$tarball" disk.sqsh
50+
echo "tar took $SECONDS seconds" >&2
51+
52+
echo Tarball created at "$tarball" >&2
53+
) 1>&2
54+
55+
echo "$tarball"
56+
'';
57+
}

0 commit comments

Comments
 (0)