I’m currently playing around with some code to extract a tar file and do some work on it after extraction to clean it up. So I have built a derivation that does what I require. It seems that the derivation is only rebuilt if the inputs are changed, but it doesn’t include the buildScript as an input.
This means that if I have a generic builder, then it’s possible to build two different outputs that have the same hash:
with (import <nixpkgs> {});
rec {
pullImageBase = var: {imageName, ref, sha256}:
stdenv.mkDerivation {
name = "docker-image_sha256_${ref}";
outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = sha256;
buildInput = [ skopeo ];
tmp = "./tmp_dir";
buildCommand = ''
mkdir $tmp
mkdir $out
${skopeo}/bin/skopeo copy --src-tls-verify=false docker://${imageName}@sha256:${ref} dir:$tmp
files=`ls $tmp/*.tar`
for file in $files;
do
SHA256=`basename $file | cut -d'.' -f1`
echo $SHA256
mkdir $out/$SHA256
mv $tmp/$SHA256.tar $out/$SHA256/layer.tar
echo "1.0" > $out/$SHA256/VERSION
echo "{\"id\":\"$SHA256\"}" > $out/$SHA256/json
done
cp $tmp/manifest.json $out/${var}
'';
};
pullImage = pullImageBase "first.json";
pullImage2 = pullImageBase "second.json";
foo = pullImage {
imageName = "library/alpine";
ref = "7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0";
sha256 = "070kc10wxi332na7p9mgwr18a6wsz5nzx2krhgz7b2548d7fk3v3";
};
bar = pullImage2 {
imageName = "library/alpine";
ref = "7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0";
sha256 = "070kc10wxi332na7p9mgwr18a6wsz5nzx2krhgz7b2548d7fk3v3";
};
}
Evaluating the foo & bar attributes result in the same derivation. I feel like this is unintended behaviour. I concede that I may be missing something though, so input/feedback would be greatly appreciated.
Apologies for the non-minimal example.