Skip to content

PSA: lock file format changes #54

@Cloudef

Description

@Cloudef

With the release of zig 0.16, the way zig handles packages have changed quite a bit. zig fetch now fetches and recompresses the fetched dependencies to the zig global path.

The way zig2nix used to do was basically reimplement zig fetch using nixpkgs utilities like fetchgit, fetchurl and so on.. However with the zig 0.16 changes this no longer worked, and re-implementing how the 0.16 zig fetch works seems like wasted effort, especially when trying to be backwards compatible.

The way zig2nix does package fetching is very much simplified.

It basically looks like this now:

# generated by zon2nix (https://github.com/Cloudef/zig2nix)

{
  linkFarm,
  runCommand,
  zig,
  name ? "zig-packages",
}:

let
  fetchZig =
    {
      name,
      url,
      hash,
    }:
    runCommand name
      {
        src = url;
        nativeBuildInputs = [ zig ];
        outputHash = hash;
        outputHashMode = "flat";
      }
      ''
        touch "$TMPDIR/build.zig" # workaround <https://codeberg.org/ziglang/zig/issues/31866>
        hash="$(cd $TMPDIR && zig fetch --global-cache-dir "$TMPDIR" ${url})"
        mv "$TMPDIR/p/$hash.tar.gz" "$out"
      '';
in
linkFarm name [
  {
    name = "wayland-0.6.0-dev-lQa1krD8AQBlMqwuhAMJjPQKXvpRByZBxxqMVAZ7yzbG.tar.gz";
    path = fetchZig {
      name = "wayland";
      url = "git+https://github.com/ifreund/zig-wayland.git#4a150a04f76f7329e80280661355c04328369d1f";
      hash = "sha256-BkJ+MRo2c6RJRcxmA9CemyvlQDFJ/T9NvJ07YHEtFFA=";
    };
  }
]

For zig 0.15 the fetchZig function is only slightly different. You still get deduplication and seperate dependency caching, so you aren't pinning the whole hash for the zig project dependencies, but the individual hashes. But the main and most important difference is that we fully leverage zig fetch so we don't have to maintain a system that essentially replicated it.

This does mean that I broke the zon2json-lock file format slightly again. The rev field is removed, because it is no longer needed. In addition since the hash calculation is now different, the hashes need to be regenerated.

If you get error during build please re-generate your lock file.

The ghostty zig2nix example repo has been updated with updated lock file:
https://github.com/Cloudef/zig2nix-ghostty-example

Packaging zig 0.16 software

It is now possible to package and build zig 0.16 software, however you may not be able to run the packaged software under nix without nix-ld or similar. The reason is that -Ddynamic-linker setting seems to be broken on zig 0.16 and patchelf has tendency to break zig produced binaries. It would be possible for me to generate wrapper script that launches dynamic binary using the correct dynamic linker, but I think it's better to wait upstream to fix these issues instead of piling temporary hacks. There are few ugly hacks regarding zig fetch for zig 0.16 (see the issue above).

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions