Skip to content

Commit

Permalink
Nix shell updates (#7)
Browse files Browse the repository at this point in the history
* Add ci specific nix environment to make run times for it smaller

* merge fixes

* add attribute selector to load nix action
  • Loading branch information
tateexon authored Jul 31, 2024
1 parent ab1aa51 commit 1a2a80d
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 20 deletions.
14 changes: 9 additions & 5 deletions .github/actions/go-change-delta/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ inputs:
description: Should nix be used under the hood, requires this projects load-nix action
required: false
default: 'false'
nix-flake-attribute:
description: The nix flake attribute to select a specific environment to use for commands
required: false
default: ''
base-ref:
description: The base ref to compare results against, typically this is main
required: false
Expand All @@ -24,11 +28,11 @@ inputs:
path:
description: The path to the root of the go module, if this is a a go module not at the root this is helpful
required: false
default: ""
default: ''
exclude:
description: A comma separated list of paths to exclude from the checks
required: false
default: ""
default: ''
include-test:
description: Should include test only package changes
required: false
Expand All @@ -47,7 +51,7 @@ runs:
if [ "${{ inputs.use-nix }}" == "false" ]; then
go install github.com/tateexon/go-change-delta@${{ inputs.version }}
else
nix develop -c sh -c "go install github.com/tateexon/go-change-delta@${{ inputs.version }}"
nix develop ${{inputs.nix-flake-attribute}} -c sh -c "go install github.com/tateexon/go-change-delta@${{ inputs.version }}"
fi
- name: Get Packages Delta
id: delta
Expand All @@ -60,7 +64,7 @@ runs:
git fetch origin ${BASE_REF}:${BASE_REF}
packages=$(go-change-delta -b=${BASE_REF} -l=${{inputs.depth}} -p=${{inputs.path}} -e=${{inputs.exclude}} -t=${{inputs.include-test}})
else
nix develop -c sh -c "git fetch origin ${BASE_REF}:${BASE_REF}"
packages=$(nix develop -c sh -c "go-change-delta -b=${BASE_REF} -l=${{inputs.depth}} -p=${{inputs.path}} -e=${{inputs.exclude}} -t=${{inputs.include-test}}")
nix develop ${{inputs.nix-flake-attribute}} -c sh -c "git fetch origin ${BASE_REF}:${BASE_REF}"
packages=$(nix develop ${{inputs.nix-flake-attribute}} -c sh -c "go-change-delta -b=${BASE_REF} -l=${{inputs.depth}} -p=${{inputs.path}} -e=${{inputs.exclude}} -t=${{inputs.include-test}}")
fi
echo "packages=${packages}" >> $GITHUB_OUTPUT
7 changes: 6 additions & 1 deletion .github/actions/load-nix/action.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: Load Nix
description: Load nix and make a call to clear out all the output on first run
inputs:
nix-flake-attribute:
description: The nix flake attribute to select a specific environment to use for commands
required: false
default: ''
runs:
using: 'composite'
steps:
Expand All @@ -10,4 +15,4 @@ runs:
- name: Pre-Load Nix
shell: bash
run: |
nix develop -c sh -c "echo \"load\""
nix develop ${{inputs.nix-flake-attribute}} -c sh -c "echo \"load\""
11 changes: 9 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ jobs:
fetch-depth: 0 # needed for go-change-delta to work correctly
- name: Load Nix
uses: ./.github/actions/load-nix
with:
nix-flake-attribute: ".#ci-runtests"
- name: Get previous commit SHA
if: github.ref == 'refs/heads/main' # Ensures this job only runs on pushes to main
id: previous_commit
run: echo "prev_sha=$(git rev-parse HEAD~1)" >> $GITHUB_OUTPUT
- name: Get Affected Packages
uses: ./.github/actions/go-change-delta
id: delta
with:
use-nix: 'true'
base-ref: ${{ github.base_ref || 'main' }}
base-ref: ${{ steps.previous_commit.outputs.prev_sha || github.base_ref }}
nix-flake-attribute: ".#ci-runtests"
- name: Run Tests In Nix
if: steps.delta.outputs.packages != ''
run: nix develop -c sh -c "go test -timeout 5m -cover -covermode=count ${{ steps.delta.outputs.packages }}"
run: nix develop .#ci-runtests -c sh -c "go test -timeout 5m -cover -covermode=count ${{ steps.delta.outputs.packages }}"
18 changes: 15 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@
];
};
};

scriptDir = toString ./.; # Converts the flake's root directory to a string

# Importing the shell environments from separate files
fullEnv = pkgs.callPackage ./nix/devshell.nix {
inherit pkgs scriptDir;
};

ciEnv = pkgs.callPackage ./nix/ci-runtests.nix {
inherit pkgs scriptDir;
};
in rec {
devShell = pkgs.callPackage ./shell.nix {
inherit pkgs;
scriptDir = toString ./.; # This converts the flake's root directory to a string
devShell = fullEnv;
devShells = {
ci-runtests = ciEnv;
};

formatter = pkgs.nixpkgs-fmt;
});
}
23 changes: 23 additions & 0 deletions nix/ci-runtests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{ pkgs, scriptDir }:
with pkgs;
let
go = pkgs.go_1_22;
in
mkShell {
nativeBuildInputs = [
bash
git
gnumake
go
];

CGO_ENABLED = "0";
GOROOT = "${go}/share/go";

shellHook = ''
# setup go bin
export GOBIN=$HOME/.nix-go/bin
mkdir -p $GOBIN
export PATH=$GOBIN:$PATH
'';
}
10 changes: 1 addition & 9 deletions shell.nix → nix/devshell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
with pkgs;
let
go = pkgs.go_1_22;

mkShell' = mkShell.override {
# The current nix default sdk for macOS fails to compile go projects, so we use a newer one for now.
stdenv = if stdenv.isDarwin then overrideSDK stdenv "11.0" else stdenv;
};
in
mkShell' {
mkShell {
nativeBuildInputs = [
# basics
bash
Expand Down Expand Up @@ -51,8 +46,5 @@ mkShell' {
export GOBIN=$HOME/.nix-go/bin
mkdir -p $GOBIN
export PATH=$GOBIN:$PATH
# install gotestloghelper
go install github.com/smartcontractkit/chainlink-testing-framework/tools/gotestloghelper@latest
'';
}

0 comments on commit 1a2a80d

Please sign in to comment.