|
| 1 | +# Copyright (c) The mlkem-native project authors |
| 2 | +# Copyright (c) The slhdsa-c project authors |
| 3 | +# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 | + |
| 5 | +name: Setup nix |
| 6 | +description: Setup nix |
| 7 | + |
| 8 | +inputs: |
| 9 | + script: |
| 10 | + description: The script to be run in the nix shell |
| 11 | + required: false |
| 12 | + devShell: |
| 13 | + description: The name of the devShell |
| 14 | + required: true |
| 15 | + cache: |
| 16 | + description: Determine whether to enable nix cache |
| 17 | + default: 'false' |
| 18 | + verbose: |
| 19 | + description: Determine wether to suppress nix log or not |
| 20 | + default: 'false' |
| 21 | + cache_prefix: |
| 22 | + description: Fixed prefix of ID of Github cache entries that should be removed. |
| 23 | + required: false |
| 24 | + save_cache: |
| 25 | + description: Determine whether to save cache with primary key or not |
| 26 | + required: false |
| 27 | + default: 'false' |
| 28 | + gh_token: |
| 29 | + description: Github access token to use |
| 30 | + required: true |
| 31 | + install: |
| 32 | + required: false |
| 33 | + description: Determine how to install nix ('installer' | 'apt') |
| 34 | + default: 'installer' |
| 35 | + |
| 36 | +runs: |
| 37 | + using: composite |
| 38 | + steps: |
| 39 | + - name: Pre-check nix |
| 40 | + id: nix-pre-check |
| 41 | + if: ${{ env.NIX_SHELL == '' }} |
| 42 | + shell: bash -lo pipefail {0} |
| 43 | + run: | |
| 44 | + suppress() { |
| 45 | + local exit_code="$?" |
| 46 | + local line_no="$1" |
| 47 | + echo "Nix check failed at $line_no: $exit_code" |
| 48 | + echo "installed=false" >> $GITHUB_OUTPUT |
| 49 | + exit 0 |
| 50 | + } |
| 51 | +
|
| 52 | + trap 'suppress $LINENO' ERR |
| 53 | +
|
| 54 | + if [[ $USER == 'root' ]]; then |
| 55 | + mkdir -p /root |
| 56 | + echo "HOME=/root" >> $GITHUB_ENV |
| 57 | + fi |
| 58 | +
|
| 59 | + nix --version |
| 60 | + - name: Install nix via apt |
| 61 | + if: ${{ steps.nix-pre-check.outputs.installed == 'false' && inputs.install == 'apt' }} |
| 62 | + shell: bash |
| 63 | + run: | |
| 64 | + if [[ -f /.dockerenv ]]; then |
| 65 | + apt install nix -y |
| 66 | + else |
| 67 | + sudo apt install nix -y |
| 68 | + fi |
| 69 | + mkdir -p ~/.config/nix |
| 70 | + cat >> ~/.config/nix/nix.conf << EOF |
| 71 | + experimental-features = nix-command flakes |
| 72 | + EOF |
| 73 | +
|
| 74 | + if [[ ! -z $GH_TOKEN ]]; then |
| 75 | + echo "access-tokens = github.com=$GH_TOKEN" >> ~/.config/nix/nix.conf |
| 76 | + fi |
| 77 | + - uses: cachix/install-nix-action@08dcb3a5e62fa31e2da3d490afc4176ef55ecd72 # v30 |
| 78 | + if: ${{ steps.nix-pre-check.outputs.installed == 'false' }} |
| 79 | + with: |
| 80 | + github_access_token: ${{ inputs.gh_token }} |
| 81 | + - name: Post-check nix |
| 82 | + id: nix-post-check |
| 83 | + continue-on-error: true |
| 84 | + shell: bash -lo pipefail {0} |
| 85 | + run: | |
| 86 | + echo "::group::nix config" |
| 87 | + if [[ -z "${{ inputs.cache_prefix }}" ]]; then |
| 88 | + cache_prefix="${{ runner.os }}-${{ runner.arch }}" |
| 89 | + else |
| 90 | + cache_prefix="${{ inputs.cache_prefix }}" |
| 91 | + fi |
| 92 | +
|
| 93 | + echo "cache_prefix=$cache_prefix" >> $GITHUB_OUTPUT |
| 94 | +
|
| 95 | + if [[ "${{ steps.nix-pre-check.outputs.installed }}" == 'false' ]]; then |
| 96 | + sudo chown -R $USER:nixbld /nix |
| 97 | + fi |
| 98 | +
|
| 99 | + nix profile install nixpkgs/nixos-24.11#sqlite |
| 100 | + nix config show |
| 101 | + echo "::endgroup::" |
| 102 | + - uses: nix-community/cache-nix-action@dab0514428ae3988852b7787a6d86a6fc571cc9d #v6.0.0 |
| 103 | + id: cache |
| 104 | + if: ${{ env.NIX_CACHE_ENABLED != 1 && inputs.cache == 'true' }} |
| 105 | + continue-on-error: true |
| 106 | + with: |
| 107 | + primary-key: ${{ steps.nix-post-check.outputs.cache_prefix }}-${{ hashFiles('**/*.nix') }} |
| 108 | + restore-prefixes-first-match: ${{ steps.nix-post-check.outputs.cache_prefix }} |
| 109 | + gc-max-store-size-linux: 536870912 |
| 110 | + purge: ${{ inputs.save_cache == 'true' }} |
| 111 | + save: ${{ inputs.save_cache == 'true' }} |
| 112 | + purge-prefixes: cache-${{ steps.nix-post-check.outputs.cache_prefix }} |
| 113 | + purge-created: 0 |
| 114 | + purge-primary-key: ${{ inputs.save_cache == 'true' && 'always' || 'never' }} |
| 115 | + token: ${{ inputs.gh_token }} |
| 116 | + - name: Set Shell |
| 117 | + shell: bash -lo pipefail {0} |
| 118 | + run: | |
| 119 | + echo "::group::set nix shell" |
| 120 | + if [[ "${{ steps.cache.outputs.hit-primary-key }}" == "true" ]]; then |
| 121 | + echo NIX_CACHE_ENABLED=1 >> $GITHUB_ENV |
| 122 | + fi |
| 123 | +
|
| 124 | + echo NIX_SHELL="${{ inputs.devShell }}" >> $GITHUB_ENV |
| 125 | + nix_extra_flags="${{ inputs.verbose == 'false' && '--quiet' || '' }}" |
| 126 | + if [[ ${{ inputs.install }} == 'apt' && ! -f /.dockerenv ]]; then |
| 127 | + echo SHELL="sudo $(which nix) develop $nix_extra_flags .#${{ inputs.devShell }} -c bash -e {0}" >> $GITHUB_ENV |
| 128 | + else |
| 129 | + echo SHELL="$(which nix) develop $nix_extra_flags .#${{ inputs.devShell }} -c bash -e {0}" >> $GITHUB_ENV |
| 130 | + fi |
| 131 | + echo "::endgroup::" |
| 132 | + - name: Prepare nix dev shell |
| 133 | + shell: ${{ env.SHELL }} |
| 134 | + run: | |
| 135 | + - name: Dependency check |
| 136 | + shell: ${{ env.SHELL }} |
| 137 | + if: inputs.script != '' |
| 138 | + run: eval ${{ inputs.script }} |
0 commit comments