diff --git a/flake.nix b/flake.nix index fd8d1a2..d43764e 100644 --- a/flake.nix +++ b/flake.nix @@ -20,6 +20,7 @@ inputs.devshell.flakeModule ./flakeModules/default.nix ./flakeModules/ethereum-development/default.nix + ./flakeModules/neutron-darwin.nix ./nixosModules/default.nix ./packages/default.nix ./tools/default.nix diff --git a/flakeModules/neutron-darwin.nix b/flakeModules/neutron-darwin.nix new file mode 100644 index 0000000..0bc7fbe --- /dev/null +++ b/flakeModules/neutron-darwin.nix @@ -0,0 +1,12 @@ +# Purpose: Override neutron package with Darwin support when on macOS +{ self, inputs, ... }: +{ + perSystem = { pkgs, config, lib, system, ... }: { + # Override cosmos-nix neutron package with our Darwin-enabled version on macOS + _module.args = { + cosmos-nix-overrides = lib.optionalAttrs pkgs.stdenv.isDarwin { + neutron = config.packages.neutrond; + }; + }; + }; +} \ No newline at end of file diff --git a/packages/default.nix b/packages/default.nix index ad29045..c208192 100644 --- a/packages/default.nix +++ b/packages/default.nix @@ -32,6 +32,11 @@ inherit (config.packages) sp1-rust; }; + # Neutron with Darwin support + neutron-darwin = (pkgs.callPackage ./neutron-darwin.nix {}).neutron; + libwasmvm = (pkgs.callPackage ./neutron-darwin.nix {}).libwasmvm; + neutrond = (pkgs.callPackage ./neutron-darwin.nix {}).neutrond; + # Solana packages from solana-tools.nix inherit (solana-tools) solana-node anchor setup-solana nightly-rust anchor-wrapper solana-tools; }; @@ -53,6 +58,9 @@ # Include our custom packages config.packages.upload-contract + # Neutron with Darwin support + config.packages.neutron-darwin + # Solana development tools config.packages.solana-tools @@ -71,6 +79,7 @@ shellHook = '' echo "Welcome to zero.nix development environment" echo "Available packages: upload-contract, local-ic, sp1-rust, sp1" + echo "Neutron: neutrond (with macOS support via libwasmvm ${if pkgs.stdenv.isDarwin then "2.1.5" else "from cosmos-nix"})" echo "Solana tools: solana-node, anchor, setup-solana, nightly-rust" echo "" echo "Run 'setup-solana' to initialize Solana development environment" diff --git a/packages/neutron-darwin.nix b/packages/neutron-darwin.nix new file mode 100644 index 0000000..1601f23 --- /dev/null +++ b/packages/neutron-darwin.nix @@ -0,0 +1,155 @@ +{ lib +, stdenv +, fetchFromGitHub +, buildGoModule +, rustPlatform +, fetchurl +, darwin +, libiconv +, writeShellScriptBin +, makeWrapper +}: + +let + # Build libwasmvm for Darwin following the quartz approach + libwasmvm = rustPlatform.buildRustPackage rec { + pname = "libwasmvm"; + version = "2.1.5"; + + src = fetchFromGitHub { + owner = "CosmWasm"; + repo = "wasmvm"; + rev = "v${version}"; + hash = "sha256-qxrAYLUsVSLBZ1GqS5/Yq+K1ZCLqBSq3M0ws1mmWMqs="; + }; + + sourceRoot = "${src.name}/libwasmvm"; + + # Use the newer fetch method for git dependencies + useFetchCargoVendor = true; + cargoHash = "sha256-Q7Enw2pwZB7kzdf/tde/Q/dZhiDCUp19YWd2UY0Hq8w="; + + nativeBuildInputs = lib.optionals stdenv.isDarwin [ + darwin.cctools + ]; + + buildInputs = lib.optionals stdenv.isDarwin [ + darwin.apple_sdk.frameworks.Security + darwin.apple_sdk.frameworks.SystemConfiguration + libiconv + ]; + + # Set macOS deployment target for compatibility + preBuild = lib.optionalString stdenv.isDarwin '' + export MACOSX_DEPLOYMENT_TARGET="11.0" + ''; + + postInstall = '' + cp target/${stdenv.hostPlatform.config}/release/libwasmvm.${if stdenv.isDarwin then "dylib" else "so"} $out/lib/ + ''; + + doCheck = false; + + meta = { + description = "WebAssembly VM for Cosmos chains"; + homepage = "https://github.com/CosmWasm/wasmvm"; + license = lib.licenses.asl20; + platforms = lib.platforms.unix; + }; + }; + + # Build neutrond with Darwin support + neutrond = buildGoModule rec { + pname = "neutrond"; + version = "6.0.3"; + + src = fetchFromGitHub { + owner = "neutron-org"; + repo = "neutron"; + rev = "v${version}"; + hash = "sha256-kduM7WzJLGmw4Yg450UQZvv3s86jc3VXmion/CAdg+Y="; + }; + + vendorHash = "sha256-wJvvogrVr+rpdZkkHnqwwRlbbuTrlJhKwQ0e71eYXJc="; + + # Enable CGO for libwasmvm + env.CGO_ENABLED = "1"; + + # Set build tags + tags = [ "netgo" ]; + + # Only build the main binary, not test packages + subPackages = [ "cmd/neutrond" ]; + + # Darwin-specific build inputs + nativeBuildInputs = [ makeWrapper ] ++ lib.optionals stdenv.isDarwin [ + darwin.cctools + ]; + + buildInputs = [ libwasmvm ] ++ lib.optionals stdenv.isDarwin [ + darwin.apple_sdk.frameworks.Security + darwin.apple_sdk.frameworks.SystemConfiguration + darwin.apple_sdk.frameworks.CoreFoundation + darwin.apple_sdk.frameworks.CoreServices + libiconv + ]; + + # Set environment variables for build + preBuild = '' + export CGO_CFLAGS="-I${libwasmvm}/include" + export CGO_LDFLAGS="-L${libwasmvm}/lib -lwasmvm" + ${lib.optionalString stdenv.isDarwin '' + export MACOSX_DEPLOYMENT_TARGET="11.0" + ''} + ''; + + # Fix dynamic library paths on Darwin + postInstall = lib.optionalString stdenv.isDarwin '' + install_name_tool -add_rpath ${libwasmvm}/lib $out/bin/neutrond + install_name_tool -change @rpath/libwasmvm.dylib ${libwasmvm}/lib/libwasmvm.dylib $out/bin/neutrond + ''; + + # Create wrapper with proper library paths + postFixup = '' + wrapProgram $out/bin/neutrond \ + --prefix ${if stdenv.isDarwin then "DYLD_LIBRARY_PATH" else "LD_LIBRARY_PATH"} : "${libwasmvm}/lib" + ''; + + doCheck = false; + + meta = { + description = "The most secure permissionless smart contract platform"; + homepage = "https://neutron.org"; + license = lib.licenses.asl20; + platforms = lib.platforms.unix; + }; + }; + + # Create a smart wrapper following the quartz pattern + neutronWrapper = writeShellScriptBin "neutron" '' + # Function to check if a command exists + command_exists() { + command -v "$1" >/dev/null 2>&1 + } + + # First, try the Nix-built neutrond + if [ -x "${neutrond}/bin/neutrond" ]; then + export ${if stdenv.isDarwin then "DYLD_LIBRARY_PATH" else "LD_LIBRARY_PATH"}="${libwasmvm}/lib:''${${if stdenv.isDarwin then "DYLD_LIBRARY_PATH" else "LD_LIBRARY_PATH"}:+:''${${if stdenv.isDarwin then "DYLD_LIBRARY_PATH" else "LD_LIBRARY_PATH"}}}" + exec "${neutrond}/bin/neutrond" "$@" + # Fall back to system neutrond if available + elif command_exists neutrond; then + echo "Warning: Using system neutrond. Some features may not work as expected." >&2 + exec neutrond "$@" + else + echo "Error: neutrond not found. Please install neutron or ensure it's in your PATH." >&2 + exit 1 + fi + ''; + +in +{ + inherit libwasmvm neutrond neutronWrapper; + + # Export the main wrapper as the default + neutron = neutronWrapper; +} \ No newline at end of file