Cloudef / zig2nix

Flake for packaging, building and running Zig projects.
MIT License
83 stars 1 forks source link

Creating a flake for `flow` #18

Closed aidanaden closed 1 month ago

aidanaden commented 1 month ago

I'm trying to create a flake for https://github.com/neurocyte/flow to allow nix users to test out the app with the command: nix run github:neurocyte/flow

I've managed to get a working flake on m1 macbook air, but nix run . fails on x86-64 nixos. The flake is as below:

{
  description = "flow project flake";

  inputs = {
    zig2nix.url = "github:Cloudef/zig2nix";
  };

  outputs =
    { zig2nix, ... }:
    let
      flake-utils = zig2nix.inputs.flake-utils;
    in
    (flake-utils.lib.eachDefaultSystem (
      system:
      let
        # Zig flake helper
        # Check the flake.nix in zig2nix project for more options:
        # <https://github.com/Cloudef/zig2nix/blob/master/flake.nix>
        env = zig2nix.outputs.zig-env.${system} {
          zig = zig2nix.outputs.packages.${system}.zig."0.13.0".bin;
        };
        system-triple = env.lib.zigTripleFromString system;
      in
      with builtins;
      with env.lib;
      with env.pkgs.lib;
      rec {
        # nix build .#target.{zig-target}
        # e.g. nix build .#target.x86_64-linux-gnu
        packages.target = genAttrs allTargetTriples (
          target:
          env.packageForTarget target (
            {
              src = cleanSource ./.;

              nativeBuildInputs = with env.pkgs; [ ];
              buildInputs = with env.pkgsForTarget target; [ ];

              # Smaller binaries and avoids shipping glibc.
              zigPreferMusl = true;

              # This disables LD_LIBRARY_PATH mangling, binary patching etc...
              # The package won't be usable inside nix.
              zigDisableWrap = true;

              zigBuildFlags = [ "-Dcpu=baseline" ];
            }
            // optionalAttrs (!pathExists ./build.zig.zon) {
              pname = "flow";
              version = "0.1.0";
            }
          )
        );

        # nix build .
        packages.default = packages.target.${system-triple}.override {
          # Prefer nix friendly settings.
          zigPreferMusl = false;
          zigDisableWrap = false;
        };

        # nix run .
        apps.default =
          let
            pkg = packages.target.${system-triple};
          in
          {
            type = "app";
            program = "${pkg}/bin/flow";
          };

        # nix run .#build
        apps.build = env.app [ ] "zig build \"$@\"";
      }
    ));
}

The error i receive when running nix run . on my x86-64 nixos is:

error: builder for '/nix/store/2cjssqfd8580m4lj1lv975m41sivdx6c-zig-linux-x86_64-0.14.0-dev.1646+b19d0fb0f.tar.xz.drv' failed with exit code 1;
       last 7 log lines:
       >
       > trying https://ziglang.org/builds/zig-linux-x86_64-0.14.0-dev.1646+b19d0fb0f.tar.xz
       >   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
       >                                  Dload  Upload   Total   Spent    Left  Speed
       >   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
       > curl: (22) The requested URL returned error: 404
       > error: cannot download zig-linux-x86_64-0.14.0-dev.1646+b19d0fb0f.tar.xz from any mirror
       For full logs, run 'nix log /nix/store/2cjssqfd8580m4lj1lv975m41sivdx6c-zig-linux-x86_64-0.14.0-dev.1646+b19d0fb0f.tar.xz.drv'.
error: 1 dependencies of derivation '/nix/store/gylj2kj09p2wn7jd5dhvm4vjvvhwhmvb-zig-0.14.0-dev.1646+b19d0fb0f.drv' failed to build
error: 1 dependencies of derivation '/nix/store/m4f6hrhvsnk2rgf970m72yvqyg0gbbgs-zig-hook.drv' failed to build
error: 1 dependencies of derivation '/nix/store/306xcrqxzhmngib9m1a90kb7vs04yqm0-zon2json.drv' failed to build
error:
       … while calling the 'getAttr' builtin

         at /builtin/derivation.nix:19:19: (source not available)

       … while calling the 'derivationStrict' builtin

         at /builtin/derivation.nix:9:12: (source not available)

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: 1 dependencies of derivation '/nix/store/vf68ggmg0a9rl3lbnv0pvn0xik0vv5jx-fromZON.drv' failed to build

is there a way to enforce the use of 0.13.0 to circumvent having to download/build zig-0.14.0?

Cloudef commented 1 month ago

Ah, this might be the fact zig2nix uses the master to build some internal tools. Good catch. I'll switch the internal tools to use default zig to avoid link rot.

aidanaden commented 1 month ago

Ah, this might be the fact zig2nix uses the master to build some internal tools. Good catch. I'll switch the internal tools to use default zig to avoid link rot.

thank u so much !! i'll be honest i'm pretty new to nix and barely know what im doing LOL

Cloudef commented 1 month ago

Should be fixed in 23d877ada261e3a6da9520c22fcd08859950b786