Wrap several files into derivation directly without creating nix-store entry for each file

Hi, I’m trying to pack some pre-built binary files into a derivation. However, foo.img and bar.imgalways copied to /nix/store/<hash>-{foo,bar}.img. Is there a way to eliminate this, only keeping the final result (/nix/store/<hash>-mypkg/data/foo.img)? Like what writeTextDirdoes.

Source of mypkg/default.nix:

pkgs: builtins.derivation {
  name = "mypkg";
  system = "x86_64-linux";
  builder = "${pkgs.bash}/bin/bash";

  file1 = ./foo.img;
  file2 = ./bar.img;

  args = [ "-c" "${pkgs.coreutils}/bin/mkdir -p $out/data; ${pkgs.coreutils}/bin/cp $file1 $out/data/foo.img; ${pkgs.coreutils}/bin/cp $file2 $out/data/bar.img" ];
}

If those files do not contain nulls, it is technically possible with builtins.readFile, but I don’t recommend it. Just run garbage collection afterwards and it should delete the intermediate files.

Can you specify why those files being created is a problem?

Also, I’d recommend using pkgs.runCommand rather than builtins.derivation. Just simpler.

1 Like