diff --git a/README.md b/README.md index 3dba3cab..1327cbc7 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,9 @@ This is an experimental Nix project for integrating the Rust and Go projects in Cosmos as Nix packages. Use this at your own risk. -## Setup Non-NixOS +## Setup + +### Non-NixOS This project is developed entirely in [Nix Flakes](https://nixos.wiki/wiki/Flakes). To get started, run the following: @@ -29,7 +31,8 @@ echo 'experimental-features = nix-command flakes' >> ~/.config/nix/nix.conf [Setup Caches](https://nixos.org/manual/nix/unstable/package-management/sharing-packages.html) -Add these lines to your Nix config (either ~/.config/nix/nix.conf [for MacOS] or /etc/nix/nix.conf [for flavors of Linux, depending on your distro]): +Add these lines to your Nix config (either ~/.config/nix/nix.conf [for MacOS] or +/etc/nix/nix.conf [for flavors of Linux, depending on your distro]): ``` extra-substituters = https://cache.nixos.org https://nix-community.cachix.org https://pre-commit-hooks.cachix.org @@ -37,9 +40,12 @@ trusted-public-keys = nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/r cores = 4 # NB: You may want to increase this on machines with more cores ``` -### Setup NixOS +### NixOS -In your `configuration.nix` file add this. Note, you can add the suggested binary caches in addition to your existing ones. +In your `configuration.nix` file you can add code below. It does 2 things, the +first is that it enables nix experimental features (which enables flakes) and +second it adds cache information so you don't have to build everything yourself. +Note, you can add the suggested binary caches in addition to your existing ones. ```nix nix = { @@ -61,8 +67,8 @@ In your `configuration.nix` file add this. Note, you can add the suggested binar ## Sources -Right now only the sources of the upstream projects are given as -a Nix derivation. You can build them by running: +Right now only the sources of the upstream projects are given as a Nix +derivation. You can build them by running: ```bash $ nix build @@ -71,9 +77,14 @@ cosmos-sdk gaia ibc-go ibc-rs tendermint-rs ``` This is just to show that our Nix derivations now have access to the source code -of the upstream projects we want to support. The exact versions of the source code -are pinned inside the `flake.lock` file, and can be updated using the -`nix flake lock` command. +of the upstream projects we want to support. The exact versions of the source +code are pinned inside the `flake.lock` file, and can be updated using the `nix +flake lock` command. + +We will then build actual Nix derivations based on these source files in +upcoming development. + +## Applications -We will then build actual Nix derivations based on these source files -in upcoming development. +Here is a list of the commands you can invoke to run specific packges. +- [hermes](https://hermes.informal.systems/): `nix run .#hermes` diff --git a/flake.lock b/flake.lock index d2067121..96bb8a7d 100644 --- a/flake.lock +++ b/flake.lock @@ -3,11 +3,11 @@ "cosmos-sdk-src": { "flake": false, "locked": { - "lastModified": 1626188694, - "narHash": "sha256-ynSwho5S63Qo+oqzpZHiZEHa2ouIf9nEzbYzVaGNfUo=", + "lastModified": 1626794131, + "narHash": "sha256-g6dJz/6tTtrl5Z7jrxK+X78KB5UadlmqBVZT/f5DA0M=", "owner": "cosmos", "repo": "cosmos-sdk", - "rev": "7040387c184869cb45aeb681e95c4c347bf26e26", + "rev": "12e4be34fbd2414b10756997ca3769ebaaf6bdea", "type": "github" }, "original": { @@ -16,6 +16,23 @@ "type": "github" } }, + "crate2nix": { + "flake": false, + "locked": { + "lastModified": 1627937495, + "narHash": "sha256-kRGfg8aQKrufKlJaXiQqAnodoFf76LjVJ0UGRwRvarY=", + "owner": "yusdacra", + "repo": "crate2nix", + "rev": "7b340c5e90bddf9f9fd08df12e7371168c3c607d", + "type": "github" + }, + "original": { + "owner": "yusdacra", + "ref": "feat/builtinfetchgit", + "repo": "crate2nix", + "type": "github" + } + }, "flake-utils": { "locked": { "lastModified": 1623875721, @@ -46,14 +63,29 @@ "type": "github" } }, + "flake-utils_3": { + "locked": { + "lastModified": 1623875721, + "narHash": "sha256-A8BU7bjS5GirpAUv4QA+QnJ4CceLHkcXdRp4xITDB0s=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "f7e004a55b120c02ecb6219596820fcd32ca8772", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, "gaia-src": { "flake": false, "locked": { - "lastModified": 1626172714, - "narHash": "sha256-/7aG+aoh7whSdRoQjUwY32K3DO4e8PGmC9Th5abd44w=", + "lastModified": 1626289329, + "narHash": "sha256-aPQPoLzM/3ahQqscDFzqI1N/6bxXWduGghNMTgo5dz8=", "owner": "cosmos", "repo": "gaia", - "rev": "397391e371937d8b6168cd000b8dbf68269a704e", + "rev": "64ae891c9a77ea88e08d7c5056781388d6892e42", "type": "github" }, "original": { @@ -65,11 +97,11 @@ "ibc-go-src": { "flake": false, "locked": { - "lastModified": 1626260024, - "narHash": "sha256-AmyS0fUIHxG+PyH3G7gV2xnfW2L8YeFRM+ervgNIRuM=", + "lastModified": 1626799724, + "narHash": "sha256-uvzdzHxrHyWGOQ6/GsVLHDSIZ/paZrcXold7loXaBLs=", "owner": "cosmos", "repo": "ibc-go", - "rev": "484cedcac875e8f96457d245d2eff816901976d5", + "rev": "0afa15af41997cfb4c5dd5f98de8043961e26195", "type": "github" }, "original": { @@ -81,11 +113,11 @@ "ibc-rs-src": { "flake": false, "locked": { - "lastModified": 1626256293, - "narHash": "sha256-T1juEVy68x/lE3tX+DVG94mPvYQvuAMDfFFLB1Xv82I=", + "lastModified": 1628083286, + "narHash": "sha256-d5ceOvZPzhNOLK8r0ZPlKFjv4SuUzC02/lO6PjdpPNc=", "owner": "informalsystems", "repo": "ibc-rs", - "rev": "a0c9a782faf0fb2165de9bb300644493e08b9055", + "rev": "142c7dc29c7636d1c9ece51ce8fd6f7c50ca3477", "type": "github" }, "original": { @@ -96,11 +128,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1626046891, - "narHash": "sha256-Zt8saH+hAehXskW0iFAzk+iMillYoFBxvLReYNqGT9E=", + "lastModified": 1626852498, + "narHash": "sha256-lOXUJvi0FJUXHTVSiC5qsMRtEUgqM4mGZpMESLuGhmo=", "owner": "nixos", "repo": "nixpkgs", - "rev": "d8f8f31af9d77a48220e4e8a301d1e79774cb7d2", + "rev": "16105403bdd843540cbef9c63fc0f16c1c6eaa70", "type": "github" }, "original": { @@ -124,6 +156,21 @@ "type": "indirect" } }, + "nixpkgs_3": { + "locked": { + "lastModified": 1628186154, + "narHash": "sha256-r2d0wvywFnL9z4iptztdFMhaUIAaGzrSs7kSok0PgmE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "06552b72346632b6943c8032e57e702ea12413bf", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, "pre-commit-hooks": { "inputs": { "flake-utils": "flake-utils_2", @@ -146,23 +193,44 @@ "root": { "inputs": { "cosmos-sdk-src": "cosmos-sdk-src", + "crate2nix": "crate2nix", "flake-utils": "flake-utils", "gaia-src": "gaia-src", "ibc-go-src": "ibc-go-src", "ibc-rs-src": "ibc-rs-src", "nixpkgs": "nixpkgs", "pre-commit-hooks": "pre-commit-hooks", + "rust-overlay": "rust-overlay", "tendermint-rs-src": "tendermint-rs-src" } }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils_3", + "nixpkgs": "nixpkgs_3" + }, + "locked": { + "lastModified": 1628302495, + "narHash": "sha256-FmqzWLoxxUpCaU4uvXkonSo/ftS6ebrTGD7PBEQyHko=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "01dd01693bd7896529841d55c4d21a98897ef26e", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, "tendermint-rs-src": { "flake": false, "locked": { - "lastModified": 1626212265, - "narHash": "sha256-oUicZT0tCQGlCbNOlgUzYHQvu2cySFKKkYr+RqeJJJU=", + "lastModified": 1626881756, + "narHash": "sha256-7fnTTtjHQ9h+p3X/3iSlU+IJyz+26CE60XogeGEAAJs=", "owner": "informalsystems", "repo": "tendermint-rs", - "rev": "1a7c16056906c21b35d5112edf65c287c2ab692a", + "rev": "d8e18c647cd8695d16610c4292b15ec6d1b45fbc", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 5273f89b..c98dd2cd 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,5 @@ { - description = "A flake for building Hello World"; + description = "A reproducible Cosmos"; inputs = { # Nix Inputs @@ -7,10 +7,17 @@ pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix"; flake-utils.url = "github:numtide/flake-utils"; + # Rust Inputs + rust-overlay.url = "github:oxalica/rust-overlay"; + crate2nix = { + url = "github:yusdacra/crate2nix/feat/builtinfetchgit"; + flake = false; + }; + # Cosmos Sources ibc-rs-src = { - flake = false; url = github:informalsystems/ibc-rs; + flake = false; }; tendermint-rs-src = { @@ -39,16 +46,43 @@ , nixpkgs , pre-commit-hooks , flake-utils + , rust-overlay + , crate2nix , ibc-rs-src , tendermint-rs-src , gaia-src , cosmos-sdk-src , ibc-go-src }: - let utils = flake-utils.lib; in + let + utils = flake-utils.lib; + overlays = [ + rust-overlay.overlay + (final: _: { + # Because rust-overlay bundles multiple rust packages into one + # derivation, specify that mega-bundle here, so that crate2nix + # will use them automatically. + rustc = final.rust-bin.stable.latest.default; + cargo = final.rust-bin.stable.latest.default; + }) + ]; + in utils.eachDefaultSystem (system: - let pkgs = nixpkgs.legacyPackages.${system}; in + let + pkgs = import nixpkgs { inherit system overlays; }; + evalPkgs = import nixpkgs { system = "x86_64-linux"; inherit overlays; }; + # Note: below is the only use of eval pkgs. This is due to an issue with import from + # derivation (IFD), which requires nix derivations to be built at evaluation time. + # Since we can't build on all system types (`utils.eachDefaultSystem` requires us + # to evaluate all possible systems) we need to pick a system for building during + # evaluation. With proper caching this flake should still work for running on all + # system types. + # + # Github Issue: https://github.com/NixOS/nix/issues/4265 + generateCargoNix = (import "${crate2nix}/tools.nix" { pkgs = evalPkgs; }).generatedCargoNix; + in rec { + # nix build .# packages = utils.flattenTree { sources = pkgs.stdenv.mkDerivation { @@ -58,13 +92,13 @@ installPhase = '' mkdir -p $out ls -la $out - ln -s ${ibc-rs-src} $out/ibc-rs ln -s ${tendermint-rs-src} $out/tendermint-rs ln -s ${gaia-src} $out/gaia ln -s ${cosmos-sdk-src} $out/cosmos-sdk ln -s ${ibc-go-src} $out/ibc-go ''; }; + hermes = (import ./hermes) { inherit pkgs ibc-rs-src generateCargoNix; }; }; # nix flake check @@ -81,11 +115,20 @@ # nix develop devShell = pkgs.mkShell { inherit (self.checks.${system}.pre-commit-check) shellHook; - # Add dev shell dependencies here - # buildInputs = with pkgs; [ ]; + nativeBuildInputs = with pkgs; [ + rustc + cargo + pkg-config + ]; + buildInputs = with pkgs; [ + openssl + pkgs.crate2nix + ]; }; - # nix build - defaultPackage = packages.sources; + # nix run .# + apps = { + hermes = utils.mkApp { name = "hermes"; drv = packages.hermes; }; + }; }); } diff --git a/hermes/default.nix b/hermes/default.nix new file mode 100644 index 00000000..35433ca6 --- /dev/null +++ b/hermes/default.nix @@ -0,0 +1,25 @@ +{ pkgs, ibc-rs-src, generateCargoNix }: +let + name = "ibc-rs"; + + # Create the cargo2nix project + cargoNix = pkgs.callPackage + (generateCargoNix { + inherit name; + src = ibc-rs-src; + }) + { + # Individual crate overrides + defaultCrateOverrides = pkgs.defaultCrateOverrides // { + ${name} = _: { + nativeBuildInputs = with pkgs; [ rustc cargo pkgconfig ]; + buildInputs = with pkgs; [ openssl ]; + }; + openssl-sys = _: { + nativeBuildInputs = with pkgs; [ rustc cargo pkgconfig ]; + buildInputs = with pkgs; [ openssl ]; + }; + }; + }; +in +cargoNix.workspaceMembers.ibc-relayer-cli.build