|
| 1 | +{ pkgs ? import <nixpkgs> {}}: |
| 2 | + |
| 3 | +let |
| 4 | + image = pkgs.dockerTools.pullImage { |
| 5 | + imageName = "hello-world"; |
| 6 | + sha256 = "sha256-pi33xlJgmjrPI9CqmwG1FW6mXN9tuUh69JT1hjH+uRc="; |
| 7 | + imageDigest = "sha256:e2fc4e5012d16e7fe466f5291c476431beaa1f9b90a5c2125b493ed28e2aba57"; |
| 8 | + }; |
| 9 | + |
| 10 | + extracted = pkgs.runCommand "container-extracted" { |
| 11 | + buildInputs = with pkgs; [ jq makeWrapper which ]; |
| 12 | + } '' |
| 13 | + mkdir -p $out/image-data $out/bin |
| 14 | + mkdir -p extracted-image |
| 15 | + tar -xvf ${image} -C extracted-image |
| 16 | + cp -r extracted-image $out/extracted-image |
| 17 | + cat extracted-image/manifest.json | jq |
| 18 | +
|
| 19 | + CONFIG_FILE="$(cat extracted-image/manifest.json | jq -r '.[].Config')" |
| 20 | +
|
| 21 | + cat extracted-image/$CONFIG_FILE | jq |
| 22 | +
|
| 23 | + cat extracted-image/$CONFIG_FILE | jq '.rootfs.diff_ids | .[]' -r \ |
| 24 | + | while IFS=":" read -r type hash; do |
| 25 | + tar -xvf extracted-image/$hash.tar -C $out/image-data |
| 26 | + done |
| 27 | +
|
| 28 | + WorkingDirectory="$(cat extracted-image/$CONFIG_FILE | jq '.config.WorkingDir' -r)" |
| 29 | +
|
| 30 | + makeWrapperArgs=() |
| 31 | + makeWrapperArgs+=(--add-flags -p) |
| 32 | + makeWrapperArgs+=(--add-flags "WorkingDirectory=$WorkingDirectory") |
| 33 | +
|
| 34 | + while IFS== read -r key value ; do |
| 35 | + echo env $key $value |
| 36 | + makeWrapperArgs+=(--add-flags "-E") |
| 37 | + makeWrapperArgs+=(--add-flags "$key=$value") |
| 38 | + done < <(cat extracted-image/$CONFIG_FILE | jq -r '.config.Env | .[]') |
| 39 | +
|
| 40 | + echo prefix $prefixCommand |
| 41 | +
|
| 42 | + makeWrapperArgs+=(--prefix PATH : ${pkgs.lib.makeBinPath [ pkgs.coreutils pkgs.xorg.lndir ]}) |
| 43 | + # makeWrapperArgs+=(--add-flags -p) |
| 44 | + # makeWrapperArgs+=(--add-flags TemporaryFileSystem=/:ro) |
| 45 | +
|
| 46 | + # the extracted image folder is readonly so making a writable overlay with symlinks pointing to the nix store |
| 47 | + makeWrapperArgs+=(--run 'rootfs=$(mktemp -d)') |
| 48 | + makeWrapperArgs+=(--run "rootfs_base=$out/image-data") |
| 49 | + makeWrapperArgs+=(--run 'lndir $rootfs_base $rootfs') |
| 50 | + makeWrapperArgs+=(--add-flags -p) |
| 51 | + makeWrapperArgs+=(--add-flags 'RootDirectory=$rootfs') |
| 52 | +
|
| 53 | + # nix store must be available for the symlinks to work |
| 54 | + makeWrapperArgs+=(--add-flags -p) |
| 55 | + makeWrapperArgs+=(--add-flags 'BindReadOnlyPaths=/nix/store') |
| 56 | +
|
| 57 | + # debugging |
| 58 | + makeWrapperArgs+=(--run 'echo rootfs $rootfs >&2') |
| 59 | + makeWrapperArgs+=(--run 'echo args $@ >&2') |
| 60 | + |
| 61 | + echo ${"$"}{makeWrapperArgs[@]} |
| 62 | + makeWrapper ${pkgs.systemd}/bin/systemd-run $out/bin/run-container "${"$"}{makeWrapperArgs[@]}" |
| 63 | + # makeWrapper $(which echo) $out/bin/run-container "${"$"}{makeWrapperArgs[@]}" |
| 64 | +
|
| 65 | + ''; |
| 66 | +in extracted |
0 commit comments