Cloudef / zig2nix

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

Function to build a zig project repo externally #12

Closed Arya-Elfren closed 4 months ago

Arya-Elfren commented 4 months ago

Would it be possible / reasonable to have a function that just takes the source and returns the exe?

For example, I would like to add hevi and flow to my nixos config without forking and adding a flake. Is there a way to do that with this flake? Either as a snippet or a feature?

{
  inputs = {
    # ...

    zig2nix.url = "github:Cloudef/zig2nix";
    hevi = {
      url = "github:Arnau478/hevi";
      flake = false;
    };
    flow = {
      url = "github:neurocyte/flow";
      flake = false;
    };
  };

  outputs = { self, nixpkgs, hevi, flow, ... }@inputs:
    let
      inherit (self) outputs;

      hevi-bin = zig2nix.lib.build hevi;
      flow-bin = zig2nix.lib.build flow;

      forAllSystems = nixpkgs.lib.genAttrs [
        "x86_64-linux"
        "aarch64-linux"
        "i686-linux"
        "x86_64-linux"
        "aarch64-darwin"
        "x86_64-darwin"
      ];
    in { ... };
}
Cloudef commented 4 months ago

The package should be able to do that. You still have to construct the env to pick the correct zig version. The package takes src argument which isnt any different from typical nix package derivation.

Note that for sandboxed builds you will need a lock file, if the project uses build.zig.zon dependencies. You can provide this lock file from outside the project. See #11 https://github.com/Cloudef/zig2nix/blob/master/src%2Fpackage.nix#L32

Im currently on travels so i cant provide examples right now.

Arya-Elfren commented 4 months ago

I currently have this working for hevi. Because of #14 I don't have this working with flow and zfe yet. (using flake-parts)

packages = {
  hevi = let
      env = inputs.zig2nix.outputs.zig-env.${system} {
        zig = inputs.zig2nix.outputs.packages.${system}.zig.master.bin;
      };
    in
      (pkgs.lib.genAttrs env.lib.allTargetTriples (target: env.packageForTarget target ({
        src = pkgs.lib.cleanSource inputs.hevi;
        zigBuildZonLock = ./packages/hevi/build.zig.zon2json-lock; # manually generated file
      }))).${env.lib.zigTripleFromString system};
};

Anything I could improve about it?

Cloudef commented 4 months ago

Thanks, I'll track your issue in the #14 and close this.