squashes / cpios: properly break the Nix storepaths by breaking them up in to chunks

This commit is contained in:
Graham Christensen 2020-06-08 09:17:32 -04:00
parent ee99e276cf
commit 4e8c7a08b8
No known key found for this signature in database
GPG Key ID: FE918C3A98C1030F
3 changed files with 30 additions and 15 deletions

View File

@ -2,20 +2,28 @@
let let
map-squash = ./map-squash.nix; map-squash = ./map-squash.nix;
mkSquashfsManifest = { name, storeContents, reverse ? false }: mkSquashfsManifest = { name, storeContents }:
runCommand "${name}-squashfs-manifest" { runCommand "${name}-squashfs-manifests" {
buildInputs = [ nix jq ]; buildInputs = [ nix jq ];
requiredSystemFeatures = [ "recursive-nix" ]; requiredSystemFeatures = [ "recursive-nix" ];
exportReferencesGraph = [ "root" storeContents ]; exportReferencesGraph = [ "root" storeContents ];
NIX_PATH = "nixpkgs=${path}"; NIX_PATH = "nixpkgs=${path}";
outputs = [ "out" "manifest" ];
} '' } ''
cat root | grep /nix/store | sort | uniq | jq -R . | jq -s . > paths.json cat root | grep /nix/store | sort | uniq | jq -R . | jq -s . > paths.json
nix-build ${map-squash} --arg pathsJson ./paths.json --arg reverse ${if reverse then "true" else "false"} nix-build ${map-squash} --arg pathsJson ./paths.json
touch $out touch $out
for f in $(cat result); do for f in $(cat result); do
find "$f" -type f >> $out find "$f" -type f >> $out
done done
touch $manifest
for f in $(cat "$out"); do
prefix=$(echo "$f" | head -c20)
suffix=$(echo "$f" | tail -c+21)
echo "$prefix $suffix" >> $manifest
done
''; '';
in in
mkSquashfsManifest mkSquashfsManifest

View File

@ -1,4 +1,4 @@
{ reverse ? false, pathsJson, pkgs ? import <nixpkgs> {} }: { pathsJson, pkgs ? import <nixpkgs> {} }:
let let
namePart = strPath: namePart = strPath:
let let
@ -12,7 +12,9 @@ let
buildInputs = [ pkgs.squashfsTools pkgs.utillinux ]; buildInputs = [ pkgs.squashfsTools pkgs.utillinux ];
} '' } ''
mkdir $out mkdir $out
revout=$(echo "$(basename ${strPath})" | rev) dirname=$(echo "$(basename ${strPath})" | head -c8)
filename=$(echo "$(basename ${strPath})" | tail -c+9)
mksquashfs \ mksquashfs \
"${builtins.storePath strPath}" \ "${builtins.storePath strPath}" \
./result \ ./result \
@ -20,11 +22,8 @@ let
-keep-as-directory \ -keep-as-directory \
-all-root -all-root
if ${if reverse then "true" else "false"}; then mkdir -p "$out/$dirname"
tac result > result.rev mv result "$out/$dirname/$filename"
mv result.rev result
fi
mv result "$out/$revout"
''; '';
in in
pkgs.writeText "squashes" (pkgs.lib.concatMapStringsSep "\n" mksquash paths) pkgs.writeText "squashes" (pkgs.lib.concatMapStringsSep "\n" mksquash paths)

View File

@ -46,8 +46,12 @@ in
mkdir -p /mnt-root/nix/.squash mkdir -p /mnt-root/nix/.squash
mkdir -p /mnt-root/nix/store mkdir -p /mnt-root/nix/store
for f in $(rev /nix-store-isos); do # the manifest splits the /nix/store/.... path with a " " to
dest=$(basename "$f" | rev) # prevent Nix from determining it depends on things.
for f in $(cat /nix-store-isos | sed 's/ //'); do
prefix=$(basename "$(dirname "$f")")
suffix=$(basename "$f")
dest="$prefix$suffix"
echo "$dest" echo "$dest"
mkdir "/mnt-root/nix/.squash/$dest" mkdir "/mnt-root/nix/.squash/$dest"
mount -t squashfs -o loop "$f" "/mnt-root/nix/.squash/$dest" mount -t squashfs -o loop "$f" "/mnt-root/nix/.squash/$dest"
@ -80,6 +84,12 @@ in
inherit config pkgs; inherit config pkgs;
initrds = { initrds = {
initrd = "${config.system.build.initialRamdisk}/initrd"; initrd = "${config.system.build.initialRamdisk}/initrd";
# squashfsStore is just a manifest file, and makeCpioRecursive
# will bring in all its dependencies automatically.
# the nix-store initrd is the actual files, and the manifest
# is intentionally only just the manifest file, located
# at /nix-store-isos
nix-store = "${( nix-store = "${(
netbootpkgs.makeCpioRecursive { netbootpkgs.makeCpioRecursive {
name = "better-initrd"; name = "better-initrd";
@ -92,9 +102,7 @@ in
contents = contents =
[ [
{ {
object = pkgs.runCommand "nix-store-isos-reversed" {} '' object = config.system.build.squashfsStore.manifest;
${pkgs.utillinux}/bin/rev ${config.system.build.squashfsStore} > $out
'';
symlink = "/nix-store-isos"; symlink = "/nix-store-isos";
} }
]; ];