# Checked in modules?

**URL:** <https://ziggit.dev/t/checked-in-modules/7432>\
**Category:** Brainstorming\
**Tags:** build-system\
**Created:** [December 20, 2024, 9:20am UTC](https://ziggit.dev/t/checked-in-modules/7432 "2024-12-20T09:20:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobyjaffey](https://ziggit.dev/user_avatar/ziggit.dev/tobyjaffey/32/3790_2.png) [@tobyjaffey](https://ziggit.dev/u/tobyjaffey)\
**Post date:** [December 20, 2024, 9:20am UTC](https://ziggit.dev/t/checked-in-modules/7432/1 "2024-12-20T09:20:54Z")

</div>

With node.js, it’s handy to be able to check `node_modules` into source control. This ensures that they cannot be externally modified/404’d and makes my repository stand-alone so I can archive it with confidence.

I can achieve this by walking my `build.zig.zon` dependencies, downloading and changing the url to a directory path. But, is there a tool or mechanism already for doing it? Should there be?

Perhaps “zig ossify/shrinkwrap/fossilise”, etc.

---

<div class="post-metadata">

**Author:** ![matklad](https://ziggit.dev/user_avatar/ziggit.dev/matklad/32/2462_2.png) [@matklad](https://ziggit.dev/u/matklad)\
**Post date:** [December 20, 2024, 10:11am UTC](https://ziggit.dev/t/checked-in-modules/7432/2 "2024-12-20T10:11:33Z")

</div>

Never tried this, so might be wrong, but:

- The correct terminology here would be `vendor`
- The key idea is `zig fetch` command, which takes a directory as an argument and puts its contents into cache of downloaded packages, identified by the hash of the content of the package. Content-hashing is key, it means that Zig doesn’t really care where from the package is coming, as long as the hash matches.
- You can write `zig build vendor-save` command which iterates over dependencies in build.zig.zon file and saves them to `./dependencies` folder inside your repository
- You can then write `zig build vendor-load` command, which iterates over the contents of the `./dependencies` directory and calls `zig fetch` on each entry.

With this setup, you can manually call `zig build vendor-save` after adding a new dependency, and `zig build vendor-load` before building the source code.

I don’t know how to automate this though. Ah! I guess you can have some code in the `configure` phase of the build (so inside, `fn build`, not in `fn make` of some step) that just checks that build.zig.zon, ./dependencies, and global package cache are all in sync. _should_ be relatively fast I think.

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [December 20, 2024, 4:34pm UTC](https://ziggit.dev/t/checked-in-modules/7432/3 "2024-12-20T16:34:19Z")

</div>

A similar concept to this is supply chain security.

At a company, you may want to protect yourself from your dependency changing underneath you (perhaps an attacker replacing the current version with a malicious one). This is covered by the hash, but it will still stop your builds if the tars become unavailable.

It’s common to use a tool like artifactory to store your tars on company servers for faster / more reliable fetching.

I’m also not sure how this can be automated. Since there is no concept of a central repository right now, one cannot just pass `--repository-url=repository.mycompany.com` in the build.

---

<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:** [December 20, 2024, 6:34pm UTC](https://ziggit.dev/t/checked-in-modules/7432/4 "2024-12-20T18:34:55Z")

</div>

One idea might be to add an alternative protocol to the package manager instead of https that uses content addressing (urls that don’t refer to a location but contain hashes that represent the content), then the company could host their own node/server for that protocol that then returns the content for those hash addresses.

I imagine something like nix/guix/ipfs or different peer to peer systems could be used as the technology. The hash makes sure the content is valid, then something downloads the content using the url that basically is just a hash, with some other protocol.

---

<div class="post-metadata">

**Author:** ![andrewrk](https://ziggit.dev/user_avatar/ziggit.dev/andrewrk/32/7011_2.png) [@andrewrk](https://ziggit.dev/u/andrewrk)\
**Post date:** [December 20, 2024, 7:33pm UTC](https://ziggit.dev/t/checked-in-modules/7432/5 "2024-12-20T19:33:47Z")

</div>

There is an intentional bit of friction for this use case because the purpose of packages is distribution of labor, and “vendoring” dependencies is antithetical to participating in that process. It interferes with the premise that the goal of a dependency is to be using the same code as other people, collaborating on bug fixes and enhancements by sending them upstream, and making a continuous effort to use an unpatched version.

The two stated goals here are:

- Ensure they cannot be externally modified
- Ensure they can be fetched

The first goal is already solved with content hashes. If an external modification occurs, it would be equivalent to a fetch failure, so really we are left with only the second point, which is planned to be addressed with mirrors.

An implicit point being made here is that using source control means that dependencies and source have the _same_ failure mode. Either all of it worked, or all of it failed, and that is a desirable property. Similarly a tarball can be created that contains source as well as all dependencies.

Before I dig into that, I want to split that into two parts and then cast shade on one of them. The two parts:

- Storing dependencies in the same location as the source
- Ability to ship a tarball that contains dependencies as well as source

For the second one, I think this use case is generally invalid because the purpose of a tarball source distribution is entirely for package managers, for example Debian or Homebrew. However system package managers don’t want vendored dependencies, in fact that is typically disqualifying and those vendored deps have to be patched out in favor of real dependencies that are managed via the system package manager.

So if you’ve agreed with me so far then I’ve whittled down the use case to merely ability to mirror the dependencies in source control. That means this use case can be solved with some more creative ideas than copy pasting into a “dependencies” folder in the source tree.

For example, if your source control system is git, dependencies could be copied onto a special, separate branch, and a mirror could be added in `build.zig.zon` to reference this data. Git even has some fancy data store things where this data could be addressed in a more sophisticated manner without a user-visible branch, but that’s a bit outside my current knowledge area.

Some related Zig issues to track:

> <https://github.com/ziglang/zig/issues/14291>
>
> Extracted from #14265.
> 
> \`\`\`zig
> .{
> .name = "libffmpeg",
> .version = "…5.1.2",
> .dependencies = .{
> .libz = .{
> .url = .{
> "https://github.com/andrewrk/libz/archive/f0e53cc2391741034b144a2c2076ed8a9937b29b.tar.gz",
> "https://andrewkelley.me/temp/backup (2).tar.gz",
> },
> .hash = "sha256=c9b30cffc40999d2c078ff350cbcee642970a224fe123c756d0892f876cf1aae",
> },
> },
> }
> \`\`\`
> 
> Note that the hash is the same regardless of which mirror URL is used.
> 
> URL mirrors are tried in succession; if one mirror fails to deliver a directory of files with a matching hash, then the next URL is tried, and so on until all the mirrors have been tried.

> <https://github.com/ziglang/zig/issues/14292>
>
> Extracted from #14265.
> 
> After supporting URL mirrors (#14291), an additional e…nhancement is to allow any project in the dependency tree to provide additional mirrors for any dependency in the entire tree.
> 
> This might look something like:
> 
> \`\`\`zig
> .{
> .name = "libffmpeg",
> .version = "5.1.2",
> // ...
> .mirrors = .{
> .@"sha256=c9b30cffc40999d2c078ff350cbcee642970a224fe123c756d0892f876cf1aae" = .{
> "https://example.com/1.tar.gz",
> "https://example.com/2.tar.gz",
> },
> },
> }
> \`\`\`
> 
> This would provide a way for a project to vendor dependencies in a git branch, or use something like \[git-subtrac\](https://jakstys.lt/2022/smart-bundling/).

---

<div class="post-metadata">

**Author:** ![andrewrk](https://ziggit.dev/user_avatar/ziggit.dev/andrewrk/32/7011_2.png) [@andrewrk](https://ziggit.dev/u/andrewrk)\
**Post date:** [December 20, 2024, 8:13pm UTC](https://ziggit.dev/t/checked-in-modules/7432/6 "2024-12-20T20:13:35Z")

</div>

I typed all that out but I also want to add that I think it would make sense to add support for a “packages” directory declared in `build.zig.zon` which basically uses the directory as mirrors. And then sure, why not, some tool to auto-populate this directory. Whether or not this can avoid the data being redundantly stored in the global cache is a separate issue. There might be some neat trick that can be done there with hard links.

Related:

> <https://github.com/ziglang/zig/issues/20178>
>
> Make package hashes generally more user-friendly, so that it is more practical t…o interact with package directories on the file system, as well as interact with stack traces, debuggers, and other tooling that uses source code paths.
> 
> The current hash format is a hex-encoded \[multihash\](https://www.multiformats.io/multihash/) SHA-256.
> 
> It looks like this:
> 
> \`\`\`console
> andy@bark ~\> ls ~/.cache/zig/p/
> 122004fa7e2ff0b3d472049743358f8fdf065cdf63bc0e5e3d54c6bb8d81d93e40da
> 1220060f743248be7cb57396b491a92e63403afb1d28fff6d1ff5fb06124b008a25e
> 1220138f4aba0c01e66b68ed9e1e1e74614c06e4743d88bc58af4f1c3dd0aae5fea7
> 122032707cdf94da394e309978146ee33c61a285300eeb916928af376ec1638a95f1
> 122048db601b6da2c69d0d783b0b19ff132e9a6d69b77820351d11c6e57553ac9433
> 12204cfebcccb9fb8a5c7b4a6ec663aea691d180f7d346d36f213b4e154a6be1f823
> 122050e58ca4d57f5e2cc3d6404691d3040bbe41e76e4ef93b52f2105f1157f7d429
> 122074e0bf09c3622780e697c11c6744e763dd63777e480baf2b583ee3ab6a02ff14
> 12207c40cefa38fe90e4230dfba2e5c76b37e1ee36602512cad8ff0501f892002a65
> 12207d353609d95cee9da7891919e6d9582e97b7aa2831bd50f33bf523a582a08547
> 1220884c1636f0e6dc92b6e74b97a2d25fe240a77bab9fed3af3e1581f80c3e7256f
> 12208b3c98b4dcc88608e65889abc853f625a06edbb835da90d902bed1ade4da0ac8
> 12209083b0c43d0f68a26a48a7b26ad9f93b22c9cff710c78ddfebb47b89cfb9c7a4
> 1220958bf550739591e62cd55fcd2009e72f9bd6c8168ceb7ad7dd8f92dda0b58a4d
> 122098b31c5b4412780898de969f7014f5c7d693f10acc8168bff86a811061d829da
> 1220b3e1fb33317c92f9ead09630f6b4be59e80d0a8780754f8aa4ee7da61cb7b47a
> 1220bee0fcf98bf6ad75b7bb09ff1f873ca38547a15b1e7a4532d20d94107d8d330a
> 1220c4a15f871f0784113c34e92e57b2862e7f678a467e5d246a6f2ebfadfca8d116
> 1220d9c400445c9c3ed46f71ebdbc364b7b349473231884c2f6e540817d7b68553ae
> 1220db11bb50364857ec6047cfcdf0938dea6af3f24d360c6b6a6103364c8e353679
> 1220dd6f0bbf4614f338d632473e4b0a879ec26eca445ed305dcdbc6b5cb6405e3cd
> 1220e783088aadba2eb7324e8dce8c6146c888a6835148dbbdc017ec2b6996a7dab8
> 1220e920d74980c0794a969e1fc0647c863023acbe935ed244a79ff8ec65f2875023
> 1220f9bd108d1e7097b27d388a7a65effd503598df61e34a2af02be00b22af567fc7
> \`\`\`
> 
> After this proposal, it would look like this instead:
> 
> \`\`\`console
> andy@bark ~\> ls ~/.cache/zig/p/
> nasm-2.16.1-2-BWdcABvF\_jM1
> libsoundio-2.0.1-7-BmEKAAr47fud
> zlib-1.3.1-3-IQwAAPXlgi9M
> libffmpeg-7.0.1-3-ReEHBD4IapnL
> StaticHttpFileServer-0.0.0-iDYAACr46GhU
> mime-1.0.0-TSAAAANL2H\_R
> EiBQ5Yyk1X9eLMPWQEaR0wQLvkHnbk75O1LyEF8RV\_fU
> libvorbis-1.3.8-3-NQ8kAD5eWxrE
> openssl-3.3.1-1-KLdkAMs-vt5n
> EiB9NTYJ2VzunaeJGRnm2Vgul7eqKDG9UPM79SOlgqCF
> libvorbis-1.3.8-2-Hw8kALYtGBJ0
> mime-2.0.0-jiAAAL-BobCs
> mime-2.0.1-hCAAAC1FfNe4
> StaticHttpFileServer-0.0.0-ozYAAOnhf9Zq
> cpython-3.11.4-cW8vBMOZSHPt
> EiCz4fszMXyS-erQljD2tL5Z6A0Kh4B1T4qk7n2mHLe0
> EiC-4Pz5i\_atdbe7Cf8fhzyjhUehWx56RTLSDZQQfY0z
> pulseaudio-16.1.1-2-kVA2ABuZh0op
> EiDZxABEXJw-1G9x69vDZLezSUcyMYhML25UCBfXtoVT
> StaticHttpFileServer-0.0.0-eTYAAFRBXp0H
> libffmpeg-7.0.1-3-7dgHBCZFa3DD
> libsoundio-2.0.1-7-7VwKAIRNMw\_X
> EiDpINdJgMB5SpaeH8BkfIYwI6y+k17SRKef+Oxl8odQ
> nasm-2.16.1-2-J2lcAPu-2VWT
> \`\`\`
> 
> This proposal is to change the hash format to \`$name-$semver-$sizedhash\` where:
> \* name is the \`name\` field from \`build.zig.zon\`, limited to 32 bytes based on new rules outlined below
> \* semver is the \`version\` field from \`build.zig.zon\`, limited to 32 bytes based on new rules outlined below
> \* sizedhash is the following 9-byte array, base64 encoded using \`-\_\` to make it filesystem safe
> \* (4 bytes) LE u32 total decompressed size in bytes (#16528)
> \* (5 bytes) truncated SHA-256 of hashed files of the package
> 
> Package names gain new rules:
> \* Limited to 32 bytes
> \* Must be a legal unquoted identifier in Zig source code (\`/\[A-Za-z\_\]\[A-Za-z0-9\_\]\*/\`)
> - Package names therefore will be represented as an enum literal in .zon format. Using a string will be deprecated.
> 
> The version field gains new rules:
> \* Limited to 32 bytes
> 
> Packages gain new rules:
> \* Limited to total file bytes of 4 GiB or less
> \* ...or, should the size field saturate for packages bigger than this?
> 
> Packages which lack a \`build.zig.zon\` file will have a \`$hashiname-P-$sizedhash\` scheme instead:
> \* hashiname is \`\[5..\]\[0..24\]\` bytes of the SHA-256, fss-base64-encoded, for a total of 32 bytes encoded
> \* the semver section is replaced with a hardcoded \`P\` which stands for "Pristine Tarball" or whatever you want, really. It acts as a version number so that any future updates to the hash format can tell this hash format apart. Note that \`"P"\` is an invalid semver.
> \* sizedhash is the following 9-byte array, fss-base64-encoded
> \* (4 bytes) LE u32 total decompressed size in bytes
> \* (5 bytes) first 5 bytes of the SHA-256 of hashed files of the package
> 
> The hash is broken up this way so that "sizedhash" can be calculated exactly the same way in both cases, and so that "name" and "hashiname" can be used interchangeably in both cases.
> 
> \## Related Future Work
> 
> \* #20180
> \* #20183
> 
> \## Compatibility
> 
> Let's try to keep compatibility with the old hash format for at least 1 release cycle, so that there is 1 release cycle that supports both the old and new format at the same time.

> <https://github.com/ziglang/zig/issues/20180>
>
> \#20178 is not technically a prerequisite for this, but the two proposals would w…ork well together.
> 
> It would be nice if you could do something like this:
> 
> \`\`\`console
> $ cd my-zig-project
> $ git clone git://example.com/zlib/ .zig-cache/p/$(zig pkg-hash zlib)
> \`\`\`
> 
> or
> 
> \`\`\`console
> $ cd my-zig-project
> $ ln -s $HOME/path-to-local-zlib-checkout/ .zig-cache/p/$(zig pkg-hash zlib)
> \`\`\`
> 
> then you could make edits to \`.zig-cache/p/\<pkg hash\>\` that would override the global packages, as well as make it easy to interact with the upstream source control of the dependency.
> 
> Crucially, these directories are typically not tracked by source control, making the workflow convenient to test a patch to a dependency.
> 
> When building with local overrides, a warning should be issued:
> 
> \`\`\`console
> $ zig build
> warning: overriding 'zlib' dependency with './zig-cache/p/zlib-1.3.1-3-IQwAAPXlgi9M'
> \`\`\`
> 
> When building in \`--system\` mode, it ignores local overrides with a warning:
> 
> \`\`\`console
> $ zig build --system /usr/lib/zig/p
> warning: ignoring local override './zig-cache/p/zlib-1.3.1-3-IQwAAPXlgi9M' in system mode
> \`\`\`
> 
> \`zig pkg-hash --list\` would list all the immediate dependency packages and their hashes:
> 
> \`\`\`console
> $ zig pkg-hash --list
> libz zlib-1.3.1-3-IQwAAPXlgi9M
> libvorbis libvorbis-1.3.8-3-NQ8kAD5eWxrE
> \`\`\`
> 
> Then you could drill down into the sub-dependencies:
> 
> \`\`\`console
> $ zig pkg-hash --list libvorbis
> libvorbis.libz zlib-1.3.1-3-IQwAAPXlgi9M
> \`\`\`
> 
> Or maybe \`--list\` would just print the whole tree.
> 
> Perhaps another subcommand could do the path calculation, so even if you're in a subdirectory, it would give you the \*local override path\*:
> 
> \`\`\`console
> andy@ark:~/my-project/build$ zig lop libvorbis.libz
> ../.zig-cache/p/zlib-1.3.1-3-IQwAAPXlgi9M
> andy@ark:~/my-project/build$ zig lop libvorbis.libztypo
> error: no dependency package named 'libvorbis.libztypo'
> info: use \`zig pkg-hash --list\` to browse dependency tree
> \`\`\`
> 
> This would be a way to quickly find out the command line to pass to e.g. \`git clone\`.
> 
> Another part of this workflow would be finding out the list of overrides that have mismatching hashes. Perhaps:
> \* \`zig lop --clean\` - delete all local overrides with mismatching hashes
> \* \`zig lop --clear\` - delete all local overrides
> \* \`zig lop --status\` - list all local overrides, their expected hashes, and actual hashes (when mismatching)
> 
> Related:
> \* #14288, which introduces the concept of a "project ID".

---

<div class="post-metadata">

**Author:** ![matklad](https://ziggit.dev/user_avatar/ziggit.dev/matklad/32/2462_2.png) [@matklad](https://ziggit.dev/u/matklad)\
**Post date:** [December 21, 2024, 12:56am UTC](https://ziggit.dev/t/checked-in-modules/7432/7 "2024-12-21T00:56:43Z")

</div>

> [@andrewrk](#):
>
> There is an intentional bit of friction for this use case because the purpose of packages is distribution of labor, and “vendoring” dependencies is antithetical to participating in that process

TBH, vendoring in a sense of having a downstream-patched copy of a dependency, and vending in a sense of providing a redundant source for the same content hashes feel like completely different use-cases to me, which perhaps shouldn’t be lumped together. One is very bad, the other, I argue, essential.

If you have many dependencies, then the probability that _all_ of them are available at any given point in time can be quite low, as this is an AND property and you need to multiply individual probabilities.

So _some_ solution is worthwhile here. Rust&Node solve it by taking care of hosting the source-of-truth for dependencies, Go&Nix solve via centralized caching layer. Either is pretty costly! I’d imagine Zig wants something more decentralized, and local vendoring feels like a decent solution, provided that there are enough checksum verification along the way to make the use-case of _patching_ dependencies sufficiently inconvenient.

(of course, there’s also an option of not having dependencies 🙃 )

---

<div class="post-metadata">

**Author:** ![andrewrk](https://ziggit.dev/user_avatar/ziggit.dev/andrewrk/32/7011_2.png) [@andrewrk](https://ziggit.dev/u/andrewrk)\
**Post date:** [December 21, 2024, 3:55am UTC](https://ziggit.dev/t/checked-in-modules/7432/8 "2024-12-21T03:55:20Z")

</div>

If I am not mistaken we are in full agreement - perhaps my intro paragraph was misleading. The friction here I realized after typing out my thoughts has specifically to do with using `path` for what canonically is maintained by a third party rather than treating the vendored copy as a mirror.

---

<div class="post-metadata">

**Author:** ![msw](https://ziggit.dev/user_avatar/ziggit.dev/msw/32/2986_2.png) [@msw](https://ziggit.dev/u/msw)\
**Post date:** [December 21, 2024, 1:39pm UTC](https://ziggit.dev/t/checked-in-modules/7432/9 "2024-12-21T13:39:47Z")

</div>

> [@matklad](#):
>
> One is very bad, the other, I argue, essential.

Which one is very bad? IME, neither is, and at least one is essential while the other becomes essential over time. Specifically for a cross-build capable solution at systems level itself, IME & O, these are the bare necessities for successful, continuous and historical (cross- / systems )building.

From a professional embedded/cross building/systems providing background, I would want to be able to do both with my tool of choice. I hope zig’s open to that argument.

---

<div class="post-metadata">

**Author:** ![mocompute](https://ziggit.dev/user_avatar/ziggit.dev/mocompute/32/3226_2.png) [@mocompute](https://ziggit.dev/u/mocompute)\
**Post date:** [December 21, 2024, 8:01pm UTC](https://ziggit.dev/t/checked-in-modules/7432/10 "2024-12-21T20:01:19Z")

</div>

There is an additional use case, which is the need to supply a source code package to a customer for building on a machine with no public internet access, or access which is severely broken, like the current http proxy bug in zig (which breaks zig’s ability to fetch dependencies).

---

<div class="post-metadata">

**Author:** ![mocompute](https://ziggit.dev/user_avatar/ziggit.dev/mocompute/32/3226_2.png) [@mocompute](https://ziggit.dev/u/mocompute)\
**Post date:** [December 21, 2024, 8:12pm UTC](https://ziggit.dev/t/checked-in-modules/7432/11 "2024-12-21T20:12:12Z")

</div>

Because I couldn’t figure out an official way to do this, we hacked our own solution: a json manifest file, build steps which create fat distributions, and a build option to point to a provided assets directory:

Somewhat described in the read me here:

> **[GitHub - att/rcloud: Collaborative data analysis and visualization](https://github.com/att/rcloud?tab=readme-ov-file#preparing-a-source-distribution)**
>
> Collaborative data analysis and visualization
