From bdd2c7395d0f9f066d1f3d50ee7c277a9bffa081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enis=20Bayramo=C4=9Flu?= Date: Fri, 17 Nov 2023 09:56:51 +0100 Subject: [PATCH] Fix and modernize nix builds to flake.nix + haskell.nix (#29) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR updates the nix build infrastructure to use flakes and haskell.nix through our new `hs-nix-infra` flake and converts default.nix to be a wrapper around the new `flake.nix`. * Add a flake.nix that builds with haskell.nix * Add sha256 to deeproute source-repository-package * Fix compiler-nix-name * Set uppoer bound on aeson Otherwise the build fails with: [ 1 of 16] Compiling JsonRpc ( src/JsonRpc.hs, dist/build/chainweb-mining-client/chainweb-mining-client-tmp/JsonRpc.o, dist/build/chainweb-mining-client/chainweb-mining-client-tmp/JsonRpc.dyn_o ) src/JsonRpc.hs:162:22: error: • Expecting one more argument to ‘A.KeyValue kv’ Expected a constraint, but ‘A.KeyValue kv’ has kind ‘* -> Constraint’ • In the type signature: requestProperties :: A.KeyValue kv => A.ToJSON a => T.Text -> a -> Maybe MsgId -> [kv] | 162 | requestProperties :: A.KeyValue kv => A.ToJSON a => T.Text -> a -> Maybe MsgId -> [kv] | ^^^^^^^^^^^^^ * Convert default.nix as legacy wrapper for flake.nix * Switch to hs-nix-infra for pinning Haskell toolchain * Add Nix build and cache action * Add a recursive output * Build recursive.allDerivations in CI * Update setup-nix-with-cache * Configure Nix with recursive-nix * Switch back to hs-nix-infra/main * Bump hs-nix-infra * Turn default.nix into a function In order to maintain backwards compatibility with old consumers --- .github/workflows/nix.yml | 46 +++++ cabal.project | 3 +- chainweb-mining-client.cabal | 2 +- default.nix | 56 +------ flake.lock | 314 +++++++++++++++++++++++++++++++++++ flake.nix | 49 ++++++ 6 files changed, 421 insertions(+), 49 deletions(-) create mode 100644 .github/workflows/nix.yml create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.github/workflows/nix.yml b/.github/workflows/nix.yml new file mode 100644 index 0000000..029fb84 --- /dev/null +++ b/.github/workflows/nix.yml @@ -0,0 +1,46 @@ +name: Build and cache with Nix + +on: + workflow_dispatch: + push: + paths: + - '**' + - '!.github/**' + - '.github/workflows/nix.yml' + +jobs: + build-and-cache: + runs-on: ${{ matrix.os }} + timeout-minutes: 740 + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, macos-m1] + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Nix with caching + uses: kadena-io/setup-nix-with-cache/by-root@v3.1 + with: + cache_url: s3://nixcache.chainweb.com?region=us-east-1 + signing_private_key: ${{ secrets.NIX_CACHE_PRIVATE_KEY }} + additional_experimental_features: recursive-nix + + - name: Set up AWS credentials + uses: aws-actions/configure-aws-credentials@v2 + with: + aws-access-key-id: ${{ secrets.NIX_CACHE_AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.NIX_CACHE_AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-1 + + - name: Give root user AWS credentials + uses: kadena-io/setup-nix-with-cache/copy-root-aws-credentials@v3.1 + + - name: Build and cache artifacts + run: | + echo Building the project and its devShell + nix build --log-lines 500 --show-trace + + echo Build the recursive output + nix build .#recursive.allDerivations --accept-flake-config --log-lines 500 --show-trace diff --git a/cabal.project b/cabal.project index 3103269..d2c9303 100644 --- a/cabal.project +++ b/cabal.project @@ -5,4 +5,5 @@ allow-newer: source-repository-package type: git location: https://github.com/kadena-io/deeproute.git - tag: 856be917bf162a769d9082c26700af4bdbb87d49 \ No newline at end of file + tag: 856be917bf162a769d9082c26700af4bdbb87d49 + --sha256: sha256-FHLl0gkwr+rtu42XBdVRD9k3bnhYl01GZAE0lz0V8wI= \ No newline at end of file diff --git a/chainweb-mining-client.cabal b/chainweb-mining-client.cabal index a6cef11..45cf42f 100644 --- a/chainweb-mining-client.cabal +++ b/chainweb-mining-client.cabal @@ -72,7 +72,7 @@ executable chainweb-mining-client attoparsec >=0.14 , base >=4.10 && <5 , base16-bytestring >=1.0 - , aeson >=1.5 + , aeson >=1.5 && <2 , async >=2.2 , bytes >=0.17 , bytestring >=0.10 diff --git a/default.nix b/default.nix index 0e219fc..e1ba434 100644 --- a/default.nix +++ b/default.nix @@ -1,48 +1,10 @@ -{ compiler ? "ghc8107" -, rev ? "7a94fcdda304d143f9a40006c033d7e190311b54" -, sha256 ? "0d643wp3l77hv2pmg2fi7vyxn4rwy0iyr8djcw1h5x72315ck9ik" -, pkgs ? - import (builtins.fetchTarball { - url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz"; - inherit sha256; }) { - config.allowBroken = false; # autodocodec - config.allowUnfree = true; - } -, mkDerivation ? null +{ + ... # Maintain backwards compatibility with old consumers }: -let gitignoreSrc = import (pkgs.fetchFromGitHub { - owner = "hercules-ci"; - repo = "gitignore"; - rev = "9e80c4d83026fa6548bc53b1a6fab8549a6991f6"; - sha256 = "04n9chlpbifgc5pa3zx6ff3rji9am6msrbn1z3x1iinjz2xjfp4p"; - }) {}; -in -pkgs.haskell.packages.${compiler}.developPackage { - name = builtins.baseNameOf ./.; - root = gitignoreSrc.gitignoreSource ./.; - - overrides = self: super: with pkgs.haskell.lib; { - configuration-tools = self.callHackageDirect { - pkg = "configuration-tools"; - ver = "0.6.1"; - sha256 = "0vrml1gj6bb5f6x79m80k9wqn5qvjjzz8c6gf36mqwdqv30irxdv"; - } {}; - - streaming-events = doJailbreak (self.callHackageDirect { - pkg = "streaming-events"; - ver = "1.0.1"; - sha256 = "11v9rrhvlxlq43m5pw63hdfn6n0fkqryphvplild1y920db96wk9"; - } {}); - - autodocodec = markUnbroken super.autodocodec; - validity-aeson = markUnbroken super.validity-aeson; - }; - source-overrides = { - }; - modifier = drv: pkgs.haskell.lib.overrideCabal drv (attrs: { - buildTools = (attrs.buildTools or []) ++ [ - pkgs.haskell.packages.${compiler}.cabal-install - pkgs.haskell.packages.${compiler}.ghcid - ]; - }); -} +(import ( + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/12c64ca55c1014cdc1b16ed5a804aa8576601ff2.tar.gz"; + sha256 = "0jm6nzb83wa6ai17ly9fzpqc40wg1viib8klq8lby54agpl213w5"; } +) { + src = ./.; +}).defaultNix.packages.${builtins.currentSystem}.default diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..4b7fd5e --- /dev/null +++ b/flake.lock @@ -0,0 +1,314 @@ +{ + "nodes": { + "empty": { + "flake": false, + "locked": { + "lastModified": 1683033565, + "narHash": "sha256-UZ2dz7/RzJKTw/8uqLu6biAbb3xNk23xUHb5/oAYWsw=", + "owner": "kadena-io", + "repo": "empty", + "rev": "c30c041f692678788a294069c95677774be2dff3", + "type": "github" + }, + "original": { + "owner": "kadena-io", + "repo": "empty", + "type": "github" + } + }, + "flake-compat": { + "locked": { + "lastModified": 1699384378, + "narHash": "sha256-jI0nYllONVrNnyOYrvQMIEJQJuNeKnfJo5keRxWMcWs=", + "owner": "kadena-io", + "repo": "flake-compat", + "rev": "6ef9397736efb0f6075188aa3488022d1b85c11d", + "type": "github" + }, + "original": { + "owner": "kadena-io", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-compat_2": { + "flake": false, + "locked": { + "lastModified": 1672831974, + "narHash": "sha256-z9k3MfslLjWQfnjBtEtJZdq3H7kyi2kQtUThfTgdRk0=", + "owner": "input-output-hk", + "repo": "flake-compat", + "rev": "45f2638735f8cdc40fe302742b79f248d23eb368", + "type": "github" + }, + "original": { + "owner": "input-output-hk", + "ref": "hkm/gitlab-fix", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "hackage": { + "flake": false, + "locked": { + "lastModified": 1696379114, + "narHash": "sha256-dtax/ci3JfYvR2lLsvpvC6b3NCoEGZLrDH21/2svTps=", + "owner": "input-output-hk", + "repo": "hackage.nix", + "rev": "21eae6f46c91831741496101e541e628aadecd98", + "type": "github" + }, + "original": { + "owner": "input-output-hk", + "repo": "hackage.nix", + "type": "github" + } + }, + "haskellNix": { + "inputs": { + "HTTP": [ + "hs-nix-infra", + "empty" + ], + "cabal-32": [ + "hs-nix-infra", + "empty" + ], + "cabal-34": [ + "hs-nix-infra", + "empty" + ], + "cabal-36": [ + "hs-nix-infra", + "empty" + ], + "cardano-shell": [ + "hs-nix-infra", + "empty" + ], + "flake-compat": "flake-compat_2", + "ghc-8.6.5-iohk": [ + "hs-nix-infra", + "empty" + ], + "ghc980": [ + "hs-nix-infra", + "empty" + ], + "ghc99": [ + "hs-nix-infra", + "empty" + ], + "hackage": [ + "hs-nix-infra", + "hackage" + ], + "hls-1.10": [ + "hs-nix-infra", + "empty" + ], + "hls-2.0": [ + "hs-nix-infra", + "empty" + ], + "hls-2.2": [ + "hs-nix-infra", + "empty" + ], + "hls-2.3": "hls-2.3", + "hpc-coveralls": [ + "hs-nix-infra", + "empty" + ], + "hydra": [ + "hs-nix-infra", + "empty" + ], + "iserv-proxy": [ + "hs-nix-infra", + "empty" + ], + "nixpkgs": [ + "hs-nix-infra", + "haskellNix", + "nixpkgs-unstable" + ], + "nixpkgs-2003": [ + "hs-nix-infra", + "empty" + ], + "nixpkgs-2105": [ + "hs-nix-infra", + "empty" + ], + "nixpkgs-2111": [ + "hs-nix-infra", + "empty" + ], + "nixpkgs-2205": [ + "hs-nix-infra", + "empty" + ], + "nixpkgs-2211": [ + "hs-nix-infra", + "empty" + ], + "nixpkgs-2305": [ + "hs-nix-infra", + "empty" + ], + "nixpkgs-unstable": "nixpkgs-unstable", + "old-ghc-nix": [ + "hs-nix-infra", + "empty" + ], + "stackage": [ + "hs-nix-infra", + "empty" + ] + }, + "locked": { + "lastModified": 1697195891, + "narHash": "sha256-0L803S/wcHmVebEwFxObYCYOaB14ZtBAFCdg0aRgH70=", + "owner": "input-output-hk", + "repo": "haskell.nix", + "rev": "c6cb3ff56b001b211690da35f70827fab5bf3272", + "type": "github" + }, + "original": { + "owner": "input-output-hk", + "repo": "haskell.nix", + "type": "github" + } + }, + "hls-2.3": { + "flake": false, + "locked": { + "lastModified": 1695910642, + "narHash": "sha256-tR58doOs3DncFehHwCLczJgntyG/zlsSd7DgDgMPOkI=", + "owner": "haskell", + "repo": "haskell-language-server", + "rev": "458ccdb55c9ea22cd5d13ec3051aaefb295321be", + "type": "github" + }, + "original": { + "owner": "haskell", + "ref": "2.3.0.0", + "repo": "haskell-language-server", + "type": "github" + } + }, + "hs-nix-infra": { + "inputs": { + "empty": "empty", + "flake-compat": "flake-compat", + "hackage": "hackage", + "haskellNix": "haskellNix", + "nixpkgs": "nixpkgs", + "nixpkgs-rec": "nixpkgs-rec" + }, + "locked": { + "lastModified": 1699970998, + "narHash": "sha256-NgvBCRIB+lvcxJWMpU8Mulx8PG8s5jtqSR8K/natoTA=", + "owner": "kadena-io", + "repo": "hs-nix-infra", + "rev": "a69071dafa3f0d12edf30ecc5a562aee1f7d138d", + "type": "github" + }, + "original": { + "owner": "kadena-io", + "repo": "hs-nix-infra", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1669833724, + "narHash": "sha256-/HEZNyGbnQecrgJnfE8d0WC5c1xuPSD2LUpB6YXlg4c=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "4d2b37a84fad1091b9de401eb450aae66f1a741e", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "4d2b37a84fad1091b9de401eb450aae66f1a741e", + "type": "github" + } + }, + "nixpkgs-rec": { + "locked": { + "lastModified": 1669833724, + "narHash": "sha256-/HEZNyGbnQecrgJnfE8d0WC5c1xuPSD2LUpB6YXlg4c=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "4d2b37a84fad1091b9de401eb450aae66f1a741e", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "4d2b37a84fad1091b9de401eb450aae66f1a741e", + "type": "github" + } + }, + "nixpkgs-unstable": { + "locked": { + "lastModified": 1695318763, + "narHash": "sha256-FHVPDRP2AfvsxAdc+AsgFJevMz5VBmnZglFUMlxBkcY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "e12483116b3b51a185a33a272bf351e357ba9a99", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "hs-nix-infra": "hs-nix-infra" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..2204adc --- /dev/null +++ b/flake.nix @@ -0,0 +1,49 @@ +{ + description = "Chainweb Mining Client"; + inputs = { + hs-nix-infra.url = "github:kadena-io/hs-nix-infra"; + flake-utils.url = "github:numtide/flake-utils"; + }; + outputs = { self, hs-nix-infra, flake-utils, }: + flake-utils.lib.eachDefaultSystem (system: + let + inherit (hs-nix-infra) nixpkgs haskellNix; + overlays = [ haskellNix.overlay + (final: prev: { + # This overlay adds our project to pkgs + chainweb-mining-client = + final.haskell-nix.project' { + src = ./.; + compiler-nix-name = "ghc8107"; + # This is used by `nix develop .` to open a shell for use with + # `cabal`, `hlint` and `haskell-language-server` + shell.tools = { + cabal = {}; + hlint = {}; + haskell-language-server = {}; + }; + # Non-Haskell shell tools go here + shell.buildInputs = with pkgs; [ + nixpkgs-fmt + ]; + # This adds `js-unknown-ghcjs-cabal` to the shell. + # shell.crossPlatforms = p: [p.ghcjs]; + }; + }) + ]; + pkgs = import nixpkgs { inherit system overlays; inherit (haskellNix) config; }; + project = pkgs.chainweb-mining-client; + flake = project.flake {}; + in { + # Built by `nix build .` + packages.default = flake.packages."chainweb-mining-client:exe:chainweb-mining-client"; + packages.recursive = with hs-nix-infra.lib.recursive system; + wrapRecursiveWithMeta "chainweb-mining-client" "${wrapFlake self}.default"; + + devShell = flake.devShell; + + # Other flake outputs provided by haskellNix can be accessed through + # this project output + inherit project; + }); +}