golemcloud / golem

Golem is an open source durable computing platform that makes it easy to build and deploy highly reliable distributed systems.
https://learn.golem.cloud/
Apache License 2.0
530 stars 59 forks source link

Development Env Using Nix Shell #1055

Closed khajavi closed 1 week ago

mschuwalow commented 1 week ago

I used these two when working on golem (on macos), maybe you can take something useful from there:

For native compilation + wasm:

{
  inputs = {
    utils.url = "github:numtide/flake-utils";
    nixpkgs.url = "nixpkgs/nixpkgs-23.11-darwin";
    rust-overlay = {
      url = "github:oxalica/rust-overlay";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
  outputs = { nixpkgs, utils, rust-overlay, ... }: utils.lib.eachDefaultSystem (system:
    let
      pkgs = import nixpkgs { inherit system; overlays = [ (import ./overlay) ]; };

      rust-bin = rust-overlay.lib.mkRustBin { } pkgs.buildPackages;
      rust-toolchains = rust-bin.stable.latest.default.override {
        extensions = [
          "rust-analyzer"
          "rust-src"
        ];
        targets = [
          "wasm32-wasip2"
          "wasm32-wasi"
        ];
      };
    in
    {
      formatter = pkgs.nixpkgs-fmt;
      devShell = pkgs.callPackage (
        {
          mkShell,
          stdenv,
          darwin,
          lib,
          protobuf,
          libiconv,
          redis,
          pkg-config,
          cargo-make,
          cargo-component,
          openssl,
          fontconfig,
          freetype,
          wasmtime
        }:
        let
          darwinNativeBuildInputs = lib.optionals stdenv.buildPlatform.isDarwin [
            darwin.apple_sdk.frameworks.SystemConfiguration
            darwin.apple_sdk.frameworks.Foundation
          ];
        in
        mkShell {

          nativeBuildInputs = [
            pkg-config
            cargo-make
            redis
            protobuf
            libiconv
            rust-toolchains
            pkg-config
            wasmtime
            cargo-component
          ] ++ darwinNativeBuildInputs;

          buildInputs = [
            openssl
            fontconfig
            freetype
          ];
        }
      ) {};
    }
  );
}

for cross-compilation to xxx-unknown-linux-gnu:

{
  inputs = {
    utils.url = "github:numtide/flake-utils";
    nixpkgs.url = "nixpkgs/nixpkgs-23.11-darwin";
    rust-overlay = {
      url = "github:oxalica/rust-overlay";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
  outputs = { nixpkgs, utils, rust-overlay, ... }: utils.lib.eachDefaultSystem (system:
    let
      pkgs = import nixpkgs { inherit system; };
      pkgsCross = pkgs.pkgsCross.aarch64-multiplatform;
      pkgsCrossStatic = pkgsCross.pkgsStatic;

      rust-bin = rust-overlay.lib.mkRustBin { } pkgsCross.buildPackages;
    in
    {
      formatter = pkgs.nixpkgs-fmt;
      devShell = pkgsCross.callPackage (
        {
          mkShell,
          qemu,
          stdenv,
          darwin,
          lib,
          protobuf,
          libiconv,
          redis,
          pkg-config,
          sqlite,
          cargo-make
        }:
        let
          darwinNativeBuildInputs = lib.optionals stdenv.buildPlatform.isDarwin [
            darwin.apple_sdk.frameworks.SystemConfiguration
            darwin.apple_sdk.frameworks.Foundation
          ];
        in
        mkShell {

          depsBuildBuild = [
            qemu
            pkg-config
          ];

          nativeBuildInputs = [
            cargo-make
            sqlite
            redis
            protobuf
            libiconv
            rust-bin.stable.latest.default
            pkg-config
          ] ++ darwinNativeBuildInputs;

          buildInputs = [
            pkgsCrossStatic.openssl
            pkgsCrossStatic.fontconfig
            pkgsCrossStatic.freetype
            pkgsCrossStatic.sqlite
          ];

          env = {
            CARGO_BUILD_TARGET = stdenv.hostPlatform.rust.rustcTargetSpec;
            "CARGO_TARGET_${stdenv.hostPlatform.rust.cargoEnvVarTarget}_LINKER" = "${stdenv.cc.targetPrefix}cc";
            "CARGO_TARGET_${stdenv.hostPlatform.rust.cargoEnvVarTarget}_RUNNER" = "qemu-aarch64";

            HOST_PKG_CONFIG = "pkg-config";
            TARGET_PKG_CONFIG = "${stdenv.cc.targetPrefix}pkg-config";

            LIBSQLITE3_SYS_USE_PKG_CONFIG = "1";
          };
        }
      ) {};
    }
  );
}
vigoo commented 1 week ago

See https://github.com/golemcloud/golem/pull/536#issuecomment-2372277658

jdegoes commented 1 week ago

Closing in favor of this, but if we need to do something here to accomodate the Nix pkg PR, then we should discuss that.

marijanp commented 1 week ago

@jdegoes @vigoo I need a way to inject the version other than using sed to not complicate the packaging: https://github.com/golemcloud/golem/blob/b861a50c1a9bcab8a45eec4ad70acb9ac59802e1/Makefile.toml#L395-L400