Something went wrong. Try again.
Nix flake packaging the official Zig compiler binaries, with tagged releases and daily nightlies updated automatically.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
// const http = @import("http");const http_io = @import("http_io");const zqlite = @import("zqlite");const uri = @import("uri");const dns_client = @import("dns_client");
const log = std.log.scoped(.update);
const ZigIndex = @import("ZigIndex.zig");const NixSources = @import("NixSources.zig");const nix32 = @import("nix32.zig");
/// The Nix systems that packages are generated for. Zig also publishes/// Windows and BSD builds, which stay in the database but are not usable as a/// Nix host system.const systems = [_][]const u8{ "aarch64-darwin", "aarch64-linux", "armv7l-linux", "i686-linux", "loongarch64-linux", "powerpc64le-linux", "riscv64-linux", "s390x-linux", "x86_64-linux",};
/// Zig's names for a target, where they differ from the Nix system. The Zig/// index uses the former, the imported `sources.json` history the latter, and/// the database stores only the Nix name so that the two land together.const arch_aliases: std.StaticStringMap([]const u8) = .initComptime(.{ .{ "aarch64-macos", "aarch64-darwin" }, .{ "x86_64-macos", "x86_64-darwin" }, .{ "armv7a-linux", "armv7l-linux" }, .{ "i386-linux", "i686-linux" }, .{ "x86-linux", "i686-linux" },});
fn normalizeArch(arch: []const u8) []const u8 { return arch_aliases.get(arch) orelse arch;}
const generated_header = "# Generated by `nix run .#update`; do not edit.\n";
/// Write `str` as a double-quoted Nix string.fn writeNixString(writer: *std.Io.Writer, str: []const u8) !void { try writer.writeByte('"'); for (str, 0..) |c, i| switch (c) { '"', '\\' => try writer.print("\\{c}", .{c}), '\n' => try writer.writeAll("\\n"), '$' => if (i + 1 < str.len and str[i + 1] == '{') try writer.writeAll("\\$") else try writer.writeByte(c), else => try writer.writeByte(c), }; try writer.writeByte('"');}
/// Write one artifact row -- version, date, hex sha256, url -- as a Nix/// attribute set, with the hash turned into SRI form for `fetchurl`.fn writeArtifact(writer: *std.Io.Writer, row: zqlite.Row, indent: []const u8) !void { var bytes_buf: [std.crypto.hash.sha2.Sha256.digest_length]u8 = undefined; const bytes = try std.fmt.hexToBytes(&bytes_buf, row.text(2)); if (bytes.len != bytes_buf.len) return error.InvalidHash; var base64_buf: [std.base64.standard.Encoder.calcSize(bytes_buf.len)]u8 = undefined; const base64 = std.base64.standard.Encoder.encode(&base64_buf, bytes);
try writer.writeAll("{\n"); try writer.print("{s} version = ", .{indent}); try writeNixString(writer, row.text(0)); try writer.print(";\n{s} date = ", .{indent}); try writeNixString(writer, row.text(1)); try writer.print(";\n{s} hash = \"sha256-{s}\";\n", .{ indent, base64 }); try writer.print("{s} url = ", .{indent}); try writeNixString(writer, row.text(3)); try writer.print(";\n{s}}}", .{indent});}
pub fn main(init: std.process.Init) !void { const io = init.io; const alloc = init.gpa;
const cwd: std.Io.Dir = .cwd();
var stdout_buffer: [1024]u8 = undefined; var stdout_file_writer: std.Io.File.Writer = .init(.stdout(), io, &stdout_buffer); const stdout = &stdout_file_writer.interface;
const flags = zqlite.OpenFlags.Create | zqlite.OpenFlags.EXResCode; var conn = try zqlite.open("sources.sqlite3", flags); defer conn.close();
try conn.exec("create table if not exists mirrors (url text unique)", .{}); // An artifact is a URL and the bytes behind it, not the bytes alone: on // the day of a release the index lists the same tarball twice, as the // release under /download/ and as master under /builds/, and both have to // be kept for the release to be published at all. try conn.exec("create table if not exists artifacts (hash text not null, date text not null, version text not null, arch text not null, url text not null, unique (hash, url))", .{});
// Databases written when the hash alone was unique. SQLite cannot drop a // constraint, so the table is rebuilt. if (try conn.row("select 1 from sqlite_master where name = 'artifacts' and sql like '%hash text unique%';", .{})) |row| { row.deinit(); try conn.transaction(); errdefer conn.rollback(); try conn.exec("create table artifacts_new (hash text not null, date text not null, version text not null, arch text not null, url text not null, unique (hash, url))", .{}); try conn.exec("insert into artifacts_new select hash, date, version, arch, url from artifacts", .{}); try conn.exec("drop table artifacts", .{}); try conn.exec("alter table artifacts_new rename to artifacts", .{}); try conn.commit(); }
// Databases written before the names were normalized. for (arch_aliases.keys(), arch_aliases.values()) |zig_name, nix_name| { if (try conn.row("select 1 from artifacts where arch = ?1 limit 1;", .{zig_name})) |row| { row.deinit(); try conn.exec("update artifacts set arch = ?1 where arch = ?2", .{ nix_name, zig_name }); } }
var client: http_io.Client = .{ .gpa = alloc, .io = io, .resolver = dns_client.HostLookup.lookup, }; defer client.deinit();
// try client.initDefaultProxies(init.arena.allocator(), init.environ_map);
{ log.info("downloading new community-mirrors.txt", .{}); var mirror_writer: std.Io.Writer.Allocating = .init(alloc); defer mirror_writer.deinit();
var result = try client.fetch(.{ .location = .{ .url = "https://ziglang.org/download/community-mirrors.txt" }, .response_writer = &mirror_writer.writer, }); defer result.deinit();
if (result.status != .ok) { log.err("unable to get list of mirrors: {t}", .{result.status}); return error.NoMirrorList; }
var wanted: std.StringArrayHashMapUnmanaged(void) = .empty; defer wanted.deinit(alloc);
var mirror_it = std.mem.splitScalar(u8, mirror_writer.written(), '\n'); while (mirror_it.next()) |line| { const raw = std.mem.trim(u8, line, &std.ascii.whitespace); if (raw.len == 0) continue; const mirror: uri.Uri = try .parse(alloc, raw); defer mirror.deinit(); try wanted.put(alloc, raw, {}); }
// Only touch the rows that changed: the database is committed by the // scheduled job, and rewriting it when nothing is new would still // change its bytes and make a commit every run. var stale: std.ArrayList([]const u8) = .empty; defer { for (stale.items) |url| alloc.free(url); stale.deinit(alloc); } { var rows = try conn.rows("select url from mirrors;", .{}); defer rows.deinit(); while (rows.next()) |row| { const url = row.text(0); if (wanted.swapRemove(url)) continue; try stale.append(alloc, try alloc.dupe(u8, url)); } if (rows.err) |err| return err; }
if (stale.items.len > 0 or wanted.count() > 0) { try conn.transaction(); errdefer conn.rollback(); for (stale.items) |url| { try conn.exec("delete from mirrors where url = ?1", .{url}); } for (wanted.keys()) |url| { try conn.exec("insert into mirrors (url) values (?1)", .{url}); } try conn.commit(); } }
{ log.info("downloading new index.json", .{}); var index_writer: std.Io.Writer.Allocating = .init(alloc); defer index_writer.deinit();
var result = try client.fetch(.{ .location = .{ .url = "https://ziglang.org/download/index.json" }, .response_writer = &index_writer.writer, }); defer result.deinit();
if (result.status != .ok) { log.err("unable to download index: {t}", .{result.status}); return error.NoIndex; }
var scanner: std.json.Scanner = .initCompleteInput(alloc, index_writer.written()); defer scanner.deinit();
var diag: std.json.Diagnostics = .{}; scanner.enableDiagnostics(&diag);
const parsed = std.json.parseFromTokenSource(ZigIndex, alloc, &scanner, .{}) catch |err| { log.err("parsing failed at {d}:{d} - {t}", .{ diag.getLine(), diag.getColumn(), err }); return; }; defer parsed.deinit();
var release_it = parsed.value.releases.iterator(); while (release_it.next()) |release_entry| { const key = release_entry.key_ptr.*; const release = release_entry.value_ptr; { var artifact_it = release.artifacts.iterator(); while (artifact_it.next()) |artifact_entry| { const arch = artifact_entry.key_ptr.*; if (std.mem.eql(u8, arch, "src")) continue; if (std.mem.eql(u8, arch, "bootstrap")) continue; const artifact = artifact_entry.value_ptr;
artifact: { if (try conn.row("select 1 from artifacts where hash = ?1 and url = ?2;", .{ artifact.shasum, artifact.tarball })) |row| { defer row.deinit(); break :artifact; }
try conn.transaction(); errdefer conn.rollback();
try conn.exec("insert into artifacts (hash, date, version, arch, url) values (?1, ?2, ?3, ?4, ?5)", .{ artifact.shasum, release.date, release.version orelse key, normalizeArch(arch), artifact.tarball, });
try conn.commit(); } } } } }
{ log.info("downloading new sources.json", .{}); var index_writer: std.Io.Writer.Allocating = .init(alloc); defer index_writer.deinit();
var result = try client.fetch(.{ .location = .{ .url = "https://raw.githubusercontent.com/mitchellh/zig-overlay/refs/heads/main/sources.json" }, .response_writer = &index_writer.writer, }); defer result.deinit();
if (result.status != .ok) { log.err("unable to download index: {t}", .{result.status}); return error.NoIndex; }
var scanner: std.json.Scanner = .initCompleteInput(alloc, index_writer.written()); defer scanner.deinit();
var diag: std.json.Diagnostics = .{}; scanner.enableDiagnostics(&diag);
const parsed = std.json.parseFromTokenSource(NixSources, alloc, &scanner, .{}) catch |err| { log.err("parsing failed at {d}:{d} - {t}", .{ diag.getLine(), diag.getColumn(), err }); return; }; defer parsed.deinit();
{ var release_it = parsed.value.master.releases.iterator(); while (release_it.next()) |release_entry| { const key = release_entry.key_ptr.*; if (std.mem.eql(u8, key, "latest")) continue; const release = release_entry.value_ptr; var artifacts_it = release.artifacts.iterator(); while (artifacts_it.next()) |artifact_entry| { const arch = artifact_entry.key_ptr.*; if (std.mem.eql(u8, arch, "src")) continue; if (std.mem.eql(u8, arch, "bootstrap")) continue; const artifact = artifact_entry.value_ptr.*; if (artifact.broken) continue; const version = artifact.version orelse continue; const hash = artifact.sha256 orelse continue; const url = artifact.url orelse continue; artifact: { if (try conn.row("select 1 from artifacts where hash = ?1 and url = ?2;", .{ hash, url })) |row| { defer row.deinit(); break :artifact; } try conn.transaction(); errdefer conn.rollback();
try conn.exec("insert into artifacts (hash, date, version, arch, url) values (?1, ?2, ?3, ?4, ?5)", .{ hash, key, version, normalizeArch(arch), url, });
try conn.commit(); } } } } }
{ log.info("writing nix/mirrors.nix", .{}); var file = try cwd.createFileAtomic(io, "nix/mirrors.nix", .{ .make_path = true, .permissions = .default_file, .replace = true, }); defer file.deinit(io); var buf: [1024]u8 = undefined; var file_writer = file.file.writer(io, &buf); const writer = &file_writer.interface;
try writer.writeAll(generated_header ++ "[\n"); var mirror_rows = try conn.rows("select distinct url from mirrors order by url;", .{}); defer mirror_rows.deinit(); while (mirror_rows.next()) |mirror_row| { try writer.writeAll(" "); try writeNixString(writer, mirror_row.text(0)); try writer.writeAll("\n"); } if (mirror_rows.err) |err| return err; try writer.writeAll("]\n");
try writer.flush(); try file.replace(io); }
for (systems) |system| { log.info("writing nix/sources-{s}.nix", .{system}); var name_buf: [64]u8 = undefined; const name = try std.fmt.bufPrint(&name_buf, "nix/sources-{s}.nix", .{system}); var file = try cwd.createFileAtomic(io, name, .{ .make_path = true, .permissions = .default_file, .replace = true, }); defer file.deinit(io); var buf: [1024]u8 = undefined; var file_writer = file.file.writer(io, &buf); const writer = &file_writer.interface;
try writer.writeAll(generated_header ++ "{\n");
// Tagged releases are the ones published under /download/. A few // versions also turn up once under /builds/, as the master build made // on the day of the release, and those belong with the nightlies. try writer.writeAll(" releases = {\n"); { var rows = try conn.rows( "select version, date, hash, url from artifacts where arch = ?1 and url like '%/download/%' order by version, date, url;", .{system}, ); defer rows.deinit(); while (rows.next()) |row| { try writer.writeAll(" "); try writeNixString(writer, row.text(0)); try writer.writeAll(" = "); try writeArtifact(writer, row, " "); try writer.writeAll(";\n"); } if (rows.err) |err| return err; } try writer.writeAll(" };\n");
// Nightlies are a list rather than an attribute set because the same // version was occasionally rebuilt and published again the next day. try writer.writeAll(" builds = [\n"); { var rows = try conn.rows( "select version, date, hash, url from artifacts where arch = ?1 and url not like '%/download/%' order by date, version, url;", .{system}, ); defer rows.deinit(); while (rows.next()) |row| { try writer.writeAll(" "); try writeArtifact(writer, row, " "); try writer.writeAll("\n"); } if (rows.err) |err| return err; } try writer.writeAll(" ];\n}\n");
try writer.flush(); try file.replace(io); }
try stdout.flush(); // Don't forget to flush!}
test { _ = ZigIndex; _ = NixSources; _ = nix32;}