From 138e51b3b3ff26a054566cd2038bb88bc491fd7a Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Mon, 25 May 2020 21:46:51 -0400 Subject: [PATCH] makeSquashfsManifest: recursively create squashfs --- pkgs/default.nix | 1 + pkgs/squashfs-recursive/default.nix | 21 ++++++++++++++++++ pkgs/squashfs-recursive/map-squash.nix | 30 ++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 pkgs/squashfs-recursive/default.nix create mode 100644 pkgs/squashfs-recursive/map-squash.nix diff --git a/pkgs/default.nix b/pkgs/default.nix index 9401f9a..2de3e84 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -1,4 +1,5 @@ { callPackage }: { makeCpioRecursive = callPackage ./cpio-recursive {}; + makeSquashfsManifest = callPackage ./squashfs-recursive {}; } diff --git a/pkgs/squashfs-recursive/default.nix b/pkgs/squashfs-recursive/default.nix new file mode 100644 index 0000000..19307c4 --- /dev/null +++ b/pkgs/squashfs-recursive/default.nix @@ -0,0 +1,21 @@ +{ runCommand, nixUnstable, jq, path }: +let + map-squash = ./map-squash.nix; + + mkSquashfsManifest = { name, storeContents, reverse ? false }: + runCommand "${name}-squashfs-manifest" { + buildInputs = [ nixUnstable jq ]; + requiredSystemFeatures = [ "recursive-nix" ]; + exportReferencesGraph = [ "root" storeContents ]; + NIX_PATH = "nixpkgs=${path}"; + } '' + 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"} + + touch $out + for f in $(cat result); do + find "$f" -type f >> $out + done + ''; +in +mkSquashfsManifest diff --git a/pkgs/squashfs-recursive/map-squash.nix b/pkgs/squashfs-recursive/map-squash.nix new file mode 100644 index 0000000..5125145 --- /dev/null +++ b/pkgs/squashfs-recursive/map-squash.nix @@ -0,0 +1,30 @@ +{ reverse ? false, pathsJson, pkgs ? import {} }: +let + namePart = strPath: + let + nameParts = builtins.tail (builtins.tail (builtins.split "[-]" strPath)); + namePartsWithDashes = (builtins.map (x: if x == [] then "-" else x) nameParts); + in + builtins.foldl' (collect: part: "${collect}${part}") "" namePartsWithDashes; + + paths = builtins.fromJSON (builtins.readFile pathsJson); + mksquash = strPath: pkgs.runCommand "${namePart strPath}-squash" { + buildInputs = [ pkgs.squashfsTools pkgs.utillinux ]; + } '' + mkdir $out + revout=$(echo "$(basename ${strPath})" | rev) + mksquashfs \ + "${builtins.storePath strPath}" \ + ./result \ + -comp gzip -Xcompression-level 9 \ + -keep-as-directory \ + -all-root + + if ${if reverse then "true" else "false"}; then + tac result > result.rev + mv result.rev result + fi + mv result "$out/$revout" + ''; +in +pkgs.writeText "squashes" (pkgs.lib.concatMapStringsSep "\n" mksquash paths)