|
| 1 | +{ |
| 2 | + description = "MoonBit compiler with latest moon and current moonc"; |
| 3 | + |
| 4 | + inputs = { |
| 5 | + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; |
| 6 | + flake-utils.url = "github:numtide/flake-utils"; |
| 7 | + }; |
| 8 | + |
| 9 | + outputs = { self, nixpkgs, flake-utils }: |
| 10 | + flake-utils.lib.eachDefaultSystem (system: |
| 11 | + let |
| 12 | + pkgs = nixpkgs.legacyPackages.${system}; |
| 13 | + in |
| 14 | + { |
| 15 | + packages.default = pkgs.stdenv.mkDerivation rec { |
| 16 | + pname = "moonbit-hybrid"; |
| 17 | + version = "latest-moon-plctlab-moonc"; |
| 18 | + |
| 19 | + # Use latest moon binary |
| 20 | + moonSrc = pkgs.fetchzip { |
| 21 | + url = "https://cli.moonbitlang.com/binaries/latest/moonbit-linux-x86_64.tar.gz"; |
| 22 | + sha256 = "sha256-8AwukSHQ9NlFncAJAdM3ZJOYHWMqzw39L6bcYWw+0rM="; |
| 23 | + stripRoot = false; |
| 24 | + }; |
| 25 | + |
| 26 | + # Use current GitHub repo for moonc |
| 27 | + src = ./.; |
| 28 | + |
| 29 | + nativeBuildInputs = with pkgs; [ |
| 30 | + autoPatchelfHook |
| 31 | + patchelf |
| 32 | + makeWrapper |
| 33 | + dune_3 |
| 34 | + ocaml |
| 35 | + pkg-config |
| 36 | + libffi |
| 37 | + gmp |
| 38 | + ]; |
| 39 | + |
| 40 | + buildInputs = with pkgs; [ |
| 41 | + stdenv.cc.cc.lib |
| 42 | + ]; |
| 43 | + |
| 44 | + buildPhase = '' |
| 45 | + runHook preBuild |
| 46 | +
|
| 47 | + # Build moonc from current repo |
| 48 | + dune build --root . |
| 49 | +
|
| 50 | + # Prepare moon from latest release |
| 51 | + cp -r ${moonSrc}/* ./ |
| 52 | + chmod -R u+w ./bin || echo "Failed to set write permissions on bin" |
| 53 | + chmod +x ./bin/moon || echo "moon binary not found" |
| 54 | + patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" ./bin/moon || echo "Failed to patch moon" |
| 55 | + [ -f ./bin/internal/tcc ] && patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" ./bin/internal/tcc || echo "tcc not found or failed to patch" |
| 56 | +
|
| 57 | + # Replace moonc with our built version |
| 58 | + cp _build/install/default/bin/moonc ./bin/moonc |
| 59 | + chmod +x ./bin/moonc |
| 60 | +
|
| 61 | + runHook postBuild |
| 62 | + ''; |
| 63 | + |
| 64 | + installPhase = '' |
| 65 | + runHook preInstall |
| 66 | +
|
| 67 | + mkdir -p $out/bin |
| 68 | + mkdir -p $out/lib |
| 69 | +
|
| 70 | + # Install binaries |
| 71 | + cp ./bin/moon $out/bin/ |
| 72 | + cp ./bin/moonc $out/bin/ |
| 73 | + cp -r ./bin/internal $out/bin/ |
| 74 | +
|
| 75 | + # Install lib files if they exist |
| 76 | + if [ -d "./lib" ]; then |
| 77 | + cp -r ./lib/* $out/lib/ |
| 78 | + fi |
| 79 | +
|
| 80 | + runHook postInstall |
| 81 | + ''; |
| 82 | + |
| 83 | + postFixup = '' |
| 84 | + wrapProgram $out/bin/moon --set MOON_HOME $out |
| 85 | + ''; |
| 86 | + |
| 87 | + meta = with pkgs.lib; { |
| 88 | + description = "MoonBit toolchain with latest moon and current moonc"; |
| 89 | + homepage = "https://www.moonbitlang.com"; |
| 90 | + license = licenses.asl20; |
| 91 | + mainProgram = "moon"; |
| 92 | + platforms = platforms.linux; |
| 93 | + }; |
| 94 | + }; |
| 95 | + |
| 96 | + devShells.default = pkgs.mkShell { |
| 97 | + buildInputs = with pkgs; [ |
| 98 | + ocaml |
| 99 | + dune_3 |
| 100 | + ocamlPackages.findlib |
| 101 | + ocamlPackages.menhir |
| 102 | + ocamlPackages.sedlex |
| 103 | + ocamlPackages.cmdliner |
| 104 | + ocamlPackages.yojson |
| 105 | + ocamlPackages.ppx_deriving_yojson |
| 106 | + ocamlPackages.zarith |
| 107 | + pkg-config |
| 108 | + libffi |
| 109 | + gmp |
| 110 | + ]; |
| 111 | + |
| 112 | + shellHook = '' |
| 113 | + echo "MoonBit development shell" |
| 114 | + echo "OCaml version: $(ocaml -version)" |
| 115 | + echo "Dune version: $(dune --version)" |
| 116 | + ''; |
| 117 | + }; |
| 118 | + }); |
| 119 | +} |
0 commit comments