Skip to content

Commit

Permalink
Migrate to hs-nix-infra for the Nix setup (#1778)
Browse files Browse the repository at this point in the history
* Expose pact through Nix

This commit exposes the pact input of chainweb at the Nix layer:
* Allows the pact input to be overriden as a flake input
* Exposes the pact package (with the CLI) of the Haskell package set

* Minor, fix capitalization

* Disable pact build-tool by default

* Handle  the end of file correctly in inplace awk

* Switch to hs-nix-infra for pinning Haskell toolchain

* Bump hs-nix-infra

* Fix the non-flake default.nix usage

* Add a metadata output to chainweb derivation

* Implement a recursive-nix version of chainweb drv

* Bump hs-nix-infra

* Avoid evaluating the hsNix project for flake outs

* Tmp avoid all unrelated hsNix stuff

* Bump hs-nix-infra

* Bump hs-nix-infra

* Use hs-nix-infra with special fetcher

* Avoid involving haskellNix in recursive path

* Remove unnecessary jq input

* Remove self references in recursive path

* Add flake outputs for testing purposes

* Bump hs-nix-infra

* Bump hs-nix-infra

* Update to revamped recursive build method

* Switch back to hs-nix-infra main

* Simplify and document the recursive package

* Revert back to setup-nix-with-cache v3

The customizable nix-conf features are now in it

* Refer to the 3.1 release explicitly

* Update copy-root-aws-credentials version in nix.yml

* Renove the tmp output used for experimentation

* Expose the haskell.nix project as an output

* Fix typo

* Use wrapRecursiveWithMeta

* Expose pact without relying on path caching

Trying to expose the `src` of the pact input by caching it as a nix
store path proved to be fragile. It's possible to output such paths
from recursive-nix derivations, but those paths will be very tricky to
access by downstream Nix expressions.

This  commit instead uses pure metadata to reconstruct the pact src
definition at the recursive layer.

* Build recursive.allDerivations in CI

* Switch back to hs-nix-infra/main

* Bump hs-nix-infra
  • Loading branch information
enobayram authored Dec 4, 2023
1 parent 6996c54 commit 28764eb
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 452 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ jobs:
uses: actions/checkout@v3

- name: Set up Nix with caching
uses: kadena-io/setup-nix-with-cache/by-root@v3
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
Expand All @@ -33,9 +34,12 @@ jobs:
aws-region: us-east-1

- name: Give root user AWS credentials
uses: kadena-io/setup-nix-with-cache/copy-root-aws-credentials@v3
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 .#check --log-lines 500 --show-trace --accept-flake-config
echo Build the recursive output
nix build .#recursive.allDerivations --accept-flake-config --log-lines 500 --show-trace
66 changes: 60 additions & 6 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ let flakeDefaultNix = (import (
src = ./.;
}).defaultNix;
inputs = flakeDefaultNix.inputs;
pkgsDef = import inputs.nixpkgs {
config = inputs.haskellNix.config;
overlays = [ inputs.haskellNix.overlay] ;
hs-nix-infra = inputs.hs-nix-infra;
pkgsDef = import hs-nix-infra.nixpkgs {
config = hs-nix-infra.haskellNix.config;
overlays = [ hs-nix-infra.haskellNix.overlay] ;
};
in
{ pkgs ? pkgsDef
, compiler ? "ghc963"
, flakePath ? flakeDefaultNix.outPath
, nix-filter ? inputs.nix-filter
, pact ? null
, enablePactBuildTool ? false
, ...
}:
let haskellSrc = with nix-filter.lib; filter {
Expand All @@ -31,10 +34,41 @@ let haskellSrc = with nix-filter.lib; filter {
"dist-newstyle"
];
};
chainweb = pkgs.haskell-nix.project' {
overridePact = pkgs.lib.optionalString (pact != null) ''
# Remove the source-repository-package section for pact and inject the
# override as a packages section
${pkgs.gawk}/bin/awk -i inplace '
BEGINFILE { delete_block=0; buffer = ""; }
/^$/ {
if (delete_block == 0) print buffer "\n";
buffer="";
delete_block=0;
next;
}
/location: https:\/\/github.com\/kadena-io\/pact.git/ { delete_block=1; }
{
if (delete_block == 0) {
if (buffer != "") buffer = buffer "\n";
buffer = buffer $0;
}
}
ENDFILE { if (delete_block == 0) print buffer "\n"; }
' $out
echo 'packages: ${pact}' >> $out
'';
overridePactBuildTool = pkgs.lib.optionalString enablePactBuildTool ''
# Remove the -build-tool flag from the pact package section, this allows
# us to build the pact executable through the chainweb project
sed -i '/package pact/,/^[^\ ]/{/^[ \t]/!b; s/-build-tool//}' $out
'';
chainweb = pkgs.haskell-nix.cabalProject' {
src = haskellSrc;
compiler-nix-name = compiler;
projectFileName = "cabal.project";
cabalProject = builtins.readFile (pkgs.runCommand "cabal.project" {} ''
cat ${./cabal.project} > $out
${overridePact}
${overridePactBuildTool}
'').outPath;
shell.tools = {
cabal = {};
haskell-language-server = {};
Expand All @@ -50,7 +84,25 @@ let haskellSrc = with nix-filter.lib; filter {
];
};
flake = chainweb.flake {};
default = pkgs.runCommandCC "chainweb" {} ''
pactFromCached = pkgs: pactInput: cached: {
version = cached.meta.pact.version;
src = if pactInput == null then pkgs.fetchgit cached.meta.pact.src else pactInput;
};
passthru = {
version = flake.packages."chainweb:exe:chainweb-node".version;
# cached.meta gets propagated through the recursive outputs
cached.paths.pactSrc = chainweb.hsPkgs.pact.src;
cached.meta.pact = {
version = chainweb.hsPkgs.pact.identifier.version;
src = if pact != null then {} else with chainweb.hsPkgs.pact.src; {
url = gitRepoUrl;
hash = outputHash;
inherit rev;
};
};
pact = pactFromCached pkgs pact passthru.cached;
};
default = pkgs.runCommandCC "chainweb" { inherit passthru; } ''
mkdir -pv $out/bin
cp ${flake.packages."chainweb:exe:chainweb-node"}/bin/chainweb-node $out/bin/chainweb-node
cp ${flake.packages."chainweb:exe:cwtool"}/bin/cwtool $out/bin/cwtool
Expand All @@ -75,6 +127,8 @@ in {
# $ ls $(nix-instantiate default.nix -A haskellSrc --eval)
inherit haskellSrc;

inherit pactFromCached;

# The haskell.nix Haskell project (executables, libraries, etc)
# Also contains the `flake` attribute, and many useful things.
#
Expand Down
Loading

0 comments on commit 28764eb

Please sign in to comment.