# Zig single file scripts?

**URL:** <https://ziggit.dev/t/zig-single-file-scripts/3990>\
**Category:** Brainstorming\
**Tags:** build-system\
**Created:** [April 17, 2024, 3:43pm UTC](https://ziggit.dev/t/zig-single-file-scripts/3990 "2024-04-17T15:43:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [April 17, 2024, 3:43pm UTC](https://ziggit.dev/t/zig-single-file-scripts/3990/1 "2024-04-17T15:43:55Z")

</div>

Based on:

> [@Best way to release packages for libraries?](https://ziggit.dev/t/best-way-to-release-packages-for-libraries/3988/5):
>
> [https://ziggit.dev/t/fluent-algorithmic-chaining-library-for-slice-manipulation/3978/11?u=sze](https://ziggit.dev/t/fluent-algorithmic-chaining-library-for-slice-manipulation/3978/11) One slightly different take on that idea might be to craft a generic build.zig, that parses the build.zig.zon in the same folder and adds every dependency via a predictable scheme as importable modules. That way you could just copy that build.zig where you want it and then use it. Similarly it could take the name for the exe also from the build.zig.zon. One other possibility would be to write a scri…

It seems like there are a lot of people (@gonzo @Luke @ngrilly) who want some way to have minimal little files, that can use some set of dependencies and be run with dependencies automatically hooked up to them.

When running `zig build --help` these options seem interesting:

```zig
Advanced Options:
  --build-file [file] Override path to build.zig
  --build-runner [file] Override path to build runner

```

I wonder whether it would be possible to write some custom build-runner, that extracts dependencies from a comment at the start of the file, fetches them, configures them as imports and then compiles the file as an executable.

I am currently busy with other stuff, but maybe somebody wants to explore that idea and see whether it is possible? I think I will explore that idea eventually, but it could take a while.

* * *

Or maybe some variant of `zig run`, that can extract dependencies from a `build.zig.zon` that is embedded in a comment at the beginning of the file?  
Imagining something like `zig run --allow-fetch script.zig`

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [April 17, 2024, 4:34pm UTC](https://ziggit.dev/t/zig-single-file-scripts/3990/2 "2024-04-17T16:34:04Z")

</div>

A post was split to a new topic: [Can I avoid repetitive names while using the build system?](https://ziggit.dev/t/can-i-avoid-repetitive-names-while-using-the-build-system/3991)

---

<div class="post-metadata">

**Author:** ![gonzo](https://ziggit.dev/user_avatar/ziggit.dev/gonzo/32/54_2.png) [@gonzo](https://ziggit.dev/u/gonzo)\
**Post date:** [April 17, 2024, 4:39pm UTC](https://ziggit.dev/t/zig-single-file-scripts/3990/3 "2024-04-17T16:39:03Z")

</div>

I’m sure there are many ways to skin this cat… The one that seems obvious to me is, having a `build.zig.zon` file which lists external dependencies could be taken to mean that if you do `@import("foo")` in a zig file in that directory, you trigger the behaviour of having a `build.zig` file that lists that dependency as a module.

---

<div class="post-metadata">

**Author:** ![tauoverpi](https://ziggit.dev/user_avatar/ziggit.dev/tauoverpi/32/184_2.png) [@tauoverpi](https://ziggit.dev/u/tauoverpi)\
**Post date:** [April 18, 2024, 5:09pm UTC](https://ziggit.dev/t/zig-single-file-scripts/3990/4 "2024-04-18T17:09:16Z")

</div>

This was explicitly rejected in a different form [Remove shebang support · Issue #2165 · ziglang/zig · GitHub](https://github.com/ziglang/zig/issues/2165)
