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.

4 Likes

After executing nix-collect-garbage -d they still exists (/nix/store/<hash>-{foo,bar}.img). I’ve added mypkg to environment.systemPackages. Is it related?

Well, they’d only still exist after garbage collection if something retained a link to them. mypkg wouldn’t retain such a link, the way you’ve written it, so presumably it’s something else.

To be honest, why don’t link them? Should even make things quicker, as you need to copy them once less.

2 Likes