Cloudef / zig2nix

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

How to build for AWS lambda? #16

Closed vytskalt closed 3 months ago

vytskalt commented 3 months ago

Hello, I have an AWS lambda function written in Zig and I want to use the lambda bundler provided by zig2nix. This is the relevant code of my flake.nix:

    packages = forAllSystems (pkgs: let
      env = zig2nix.outputs.zig-env.${pkgs.system} {};
      target = "aarch64-linux";
      skia = ((env.pkgsForTarget target).callPackage ./nix/minimal-skia.nix {});
    in rec {
      exporter-lambda = env.packageForTarget target {
        src = ./.;
        zigPreferMusl = false;
        zigDisableWrap = false;
        buildInputs = [ skia ];
        zigBuildFlags = [
          "-Dskia-include=${skia.dev}/include"
          "-Dskia-lib=${skia.out}/lib"
        ];
      };
      aws = env.bundle.aws.lambda {
        package = exporter-lambda;
        arch = "aarch64";
      };
    });

(minimal-skia.nix is essentially a modified version of this package with useless dependencies removed)

in my build.zig I'm doing something like:

const skia_include = b.option([]const u8, "skia-include", "path to skia include dir");
const skia_lib = b.option([]const u8, "skia-lib", "path to skia lib dir");
// ...
if (skia_include) |include| {
  exe.addIncludePath(.{ .cwd_relative = include });
}
if (skia_lib) |lib| {
  exe.addLibraryPath(.{ .cwd_relative = lib });
}

But when trying to nix build .#aws I'm getting this error:

error:
       … while calling the 'derivationStrict' builtin

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

       … while evaluating the derivation attribute 'name'

         at /nix/store/0jr2kk95c34c0b6yxi75q4fqgb43kqkm-source/pkgs/stdenv/generic/make-derivation.nix:334:7:

          333|     // (optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) {
          334|       name =
             |       ^
          335|         let

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

       error: architecture  is not supported

What could I be doing wrong? Thanks

Cloudef commented 3 months ago

The arch expects aws arch so arm64 in this case. Usually it should be autodetected by omitting it though.

vytskalt commented 3 months ago

I had the same error without specifying arch.

Cloudef commented 3 months ago

Ah sorry, it indeed should be aarch64. Will it work if you give your package a name?

vytskalt commented 3 months ago

Sorry for late reply. Giving the package a name and setting the meta.mainProgram for my zig package fixed it. Thanks!