# Downloading/install binary assets in build.zig

**URL:** <https://ziggit.dev/t/downloading-install-binary-assets-in-build-zig/5701>\
**Category:** Help\
**Tags:** build-system\
**Created:** [August 20, 2024, 7:49pm UTC](https://ziggit.dev/t/downloading-install-binary-assets-in-build-zig/5701 "2024-08-20T19:49:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [August 20, 2024, 7:49pm UTC](https://ziggit.dev/t/downloading-install-binary-assets-in-build-zig/5701/1 "2024-08-20T19:49:29Z")

</div>

i have a small number of binary assets (pre-built executables) which i’d like to download/install as a step within my `build.zig`… i control the assets themselves and could package them in any format… also note that these assets are _specific_ to the host OS on which `zig build` is being run…

should i bundle these assets into a package, which i’d call out through `build.zig.zon`??? or should i do somethiing a little more adhoc inside the `build.zig` file itself???

a minimal example that codifies best practice would probably answer these and other questions…

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [August 20, 2024, 10:46pm UTC](https://ziggit.dev/t/downloading-install-binary-assets-in-build-zig/5701/2 "2024-08-20T22:46:07Z")

</div>

You probably want to use lazy dependencies and `b.lazyDependency`:

- [build.zig.zon docs](https://github.com/ziglang/zig/blob/master/doc/build.zig.zon.md#lazy)
- [lazyDependency docs](https://ziglang.org/documentation/master/std/#std.Build.lazyDependency)

Example from mach:

- [build.zig.zon](https://github.com/hexops/mach/blob/c04a6080b50f7919fe12ccd029f6999f2a349817/build.zig.zon#L51-L55)
- [build.zig](https://github.com/hexops/mach/blob/c04a6080b50f7919fe12ccd029f6999f2a349817/build.zig#L222-L228)

For installing prebuilt executables from the lazy dependency, you could use one of `b.installBinFile`/`b.installBinDirectory`/`b.addInstallFileWithDir`/`b.addInstallDirectory`, and use `dep.path("foo")` to get the src path (where `dep` is the return from `b.lazyDependency`).

Or, if you want to just package them all together and then choose which to install at build-time, then you don’t need `.lazy = true`/`b.lazyDependency` (instead just use `b.dependency`), but the rest is the same.

---

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [August 21, 2024, 1:44am UTC](https://ziggit.dev/t/downloading-install-binary-assets-in-build-zig/5701/3 "2024-08-21T01:44:37Z")

</div>

some simple follow-on questions, as i’m trying to increase my understanding:

are the URLs found in the `build.zig.zon` assumed to be `.tar.gz` files??? i can certainly create a single archive that includes all of my individual binary assets…

once this dependency is “fetched”, is it automatically deflated into a directory structure that lives in the cache???

do i then get some actual path into the cache and copy files manually, or do i use one of the `b.install*` methods???

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [August 22, 2024, 11:42pm UTC](https://ziggit.dev/t/downloading-install-binary-assets-in-build-zig/5701/4 "2024-08-22T23:42:40Z")

</div>

> [@biosbob](#):
>
> are the URLs found in the `build.zig.zon` assumed to be `.tar.gz` files

See [here](https://github.com/ziglang/zig/blob/master/src/Package/Fetch.zig#L843-L850) for the filetypes currently supported.

> [@biosbob](#):
>
> once this dependency is “fetched”, is it automatically deflated into a directory structure that lives in the cache???

Yes, it lives in `p/<hash>/` in the global cache.

> [@biosbob](#):
>
> do i then get some actual path into the cache and copy files manually, or do i use one of the `b.install*` methods???

You use `dep.path("relative/path")` where `dep` is the return of either `b.dependency` or `b.lazyDependency` as mentioned in my previous post. The path is resolved relative to the root folder of the uncompressed package.

If you don’t want to “install” files, then you can use [`b.addWriteFiles()`](https://ziglang.org/documentation/master/std/#std.Build.addWriteFiles) to copy files.

---

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [August 23, 2024, 5:08pm UTC](https://ziggit.dev/t/downloading-install-binary-assets-in-build-zig/5701/5 "2024-08-23T17:08:28Z")

</div>

i tried something like this:

```zig
const dep = b.dependency("mytool", .{}); // URL in build.zig.zon for mytool.zip
var wf = b.addWriteFiles();
wf.addCopyDirectory(dep.path("mytool"), ".tools", .{});

```

i’m trying to populate a `.tools` folder in my build site which contains the (unzipped) contents of `mytool.zip` – which contains a folder named `mytool` which itself contains other artifacts… said another way, i want to unzip `mytool.zip` into my `.tools` folder…

when i run `zig build`, however, i receive an error on my `addCopyDirectory` call stating that `value of type 'Build.LazyPath' ignored`

help!!!

---

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [August 23, 2024, 5:45pm UTC](https://ziggit.dev/t/downloading-install-binary-assets-in-build-zig/5701/6 "2024-08-23T17:45:03Z")

</div>

i was able to get the following to work, for anyone looking for a small gist:

```zig
const dep = b.dependency("mytool", .{});
const install_step = b.addInstallDirectory(.{
    .source_dir = dep.path("."),
     .install_dir = std.Build.InstallDir{ .custom = "tools" },
     .install_subdir = "mytool",
});
exe.step.dependOn(&install_step.step);
```
