|
1 | | -name: 'Install Nix on ephemeral runners' |
2 | | -description: 'Installs Nix and sets up AWS credentials to push to the Nix binary cache' |
| 1 | +name: Install Nix on ephemeral runners |
| 2 | +description: Installs Nix and sets up AWS credentials to push to the Nix binary cache |
3 | 3 | inputs: |
| 4 | + aws-region: |
| 5 | + description: AWS region for the Nix binary cache S3 bucket |
| 6 | + required: false |
| 7 | + default: us-east-1 |
| 8 | + nix-signing-key: |
| 9 | + description: Key used to sign uploads to binary cache, required if push-to-cache is true |
| 10 | + required: false |
4 | 11 | push-to-cache: |
5 | | - description: 'Whether to push build outputs to the Nix binary cache' |
| 12 | + description: Whether to push build outputs to the Nix binary cache |
6 | 13 | required: false |
7 | | - default: 'false' |
8 | | - aws-region: |
9 | | - description: 'AWS region for the Nix binary cache S3 bucket' |
| 14 | + default: false |
| 15 | + role-to-assume: |
| 16 | + description: AWS Role to assume when configuring aws credentials, required if push-to-cache is true |
10 | 17 | required: false |
11 | | - default: 'us-east-1' |
12 | 18 | runs: |
13 | | - using: 'composite' |
| 19 | + using: composite |
14 | 20 | steps: |
15 | | - - name: aws-creds |
| 21 | + - name: validate push-to-cache required inputs |
| 22 | + if: inputs.push-to-cache == 'true' |
| 23 | + shell: bash |
| 24 | + run: | |
| 25 | + abort() { echo "$@" >&2; exit 1; } |
| 26 | + [[ -n ${NIX_SIGNING_KEY:+ok} ]] || abort "nix-signing-key is required when push-to-cache is true" |
| 27 | + [[ -n ${ROLE_TO_ASSUME:+ok} ]] || abort "role-to-assume is required when push-to-cache is true" |
| 28 | + env: |
| 29 | + NIX_SIGNING_KEY: ${{ inputs.nix-signing-key }} |
| 30 | + ROLE_TO_ASSUME: ${{ inputs.role-to-assume }} |
| 31 | + |
| 32 | + - name: configure aws credentials |
16 | 33 | uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a # v4.3.1 |
17 | | - if: ${{ inputs.push-to-cache == 'true' }} |
| 34 | + if: inputs.push-to-cache == 'true' |
18 | 35 | with: |
19 | | - role-to-assume: ${{ env.DEV_AWS_ROLE }} |
| 36 | + role-to-assume: ${{ inputs.role-to-assume }} |
20 | 37 | aws-region: ${{ inputs.aws-region }} |
21 | 38 | output-credentials: true |
22 | 39 | role-duration-seconds: 7200 |
23 | | - - name: Setup AWS credentials for Nix |
24 | | - if: ${{ inputs.push-to-cache == 'true' }} |
25 | | - shell: bash |
26 | | - run: | |
27 | | - sudo -H aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID |
28 | | - sudo -H aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY |
29 | | - sudo -H aws configure set aws_session_token $AWS_SESSION_TOKEN |
30 | | - sudo -H aws configure set region ${{ inputs.aws-region }} |
31 | | - sudo mkdir -p /etc/nix |
32 | | - sudo -E python -c "import os; file = open('/etc/nix/nix-secret-key', 'w'); file.write(os.environ['NIX_SIGN_SECRET_KEY']); file.close()" |
33 | | - cat << 'EOF' | sudo tee /etc/nix/upload-to-cache.sh > /dev/null |
34 | | - #!/usr/bin/env bash |
35 | | - set -euo pipefail |
36 | | - set -f |
37 | 40 |
|
38 | | - export IFS=' ' |
39 | | - /nix/var/nix/profiles/default/bin/nix copy --max-jobs 5 --to 's3://nix-postgres-artifacts?secret-key=/etc/nix/nix-secret-key' $OUT_PATHS |
40 | | - EOF |
41 | | - sudo chmod +x /etc/nix/upload-to-cache.sh |
42 | | - env: |
43 | | - NIX_SIGN_SECRET_KEY: ${{ env.NIX_SIGN_SECRET_KEY }} |
44 | | - - name: Install Nix |
45 | | - shell: bash |
46 | | - run: | |
47 | | - sudo tee /tmp/nix-extra.conf > /dev/null <<'NIXCONF' |
48 | | - always-allow-substitutes = true |
49 | | - extra-experimental-features = nix-command flakes |
50 | | - extra-system-features = kvm |
51 | | - max-jobs = 4 |
52 | | - substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com |
53 | | - trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= |
54 | | - NIXCONF |
55 | | -
|
56 | | - if [ "${{ inputs.push-to-cache }}" = "true" ]; then |
57 | | - echo "post-build-hook = /etc/nix/upload-to-cache.sh" | sudo tee -a /tmp/nix-extra.conf > /dev/null |
58 | | - fi |
59 | | -
|
60 | | - curl -L https://releases.nixos.org/nix/nix-2.34.6/install | sh -s -- --daemon --yes --nix-extra-conf-file /tmp/nix-extra.conf |
61 | | -
|
62 | | - cat /etc/nix/nix.conf |
63 | | -
|
64 | | - # Add nix to PATH for subsequent steps |
65 | | - echo "/nix/var/nix/profiles/default/bin" >> "$GITHUB_PATH" |
66 | | - # Source the daemon profile so nix works in this step too |
67 | | - . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh |
68 | | - - name: Configure Nix with GitHub token |
| 41 | + - name: setup nix |
69 | 42 | shell: bash |
| 43 | + run: cd ${{ github.action_path }} && ./setup-nix.sh |
70 | 44 | env: |
71 | | - GH_TOKEN: ${{ github.token }} |
72 | | - run: | |
73 | | - echo "access-tokens = github.com=$GH_TOKEN" | sudo tee -a /etc/nix/nix.conf > /dev/null |
74 | | - sudo systemctl restart nix-daemon || true |
75 | | - - name: Print Nix version |
| 45 | + AWS_REGION: ${{ inputs.aws-region }} |
| 46 | + GITHUB_TOKEN: ${{ github.token }} |
| 47 | + NIX_SIGN_SECRET_KEY: ${{ inputs.nix-signing-key }} |
| 48 | + PUSH_TO_CACHE: ${{ inputs.push-to-cache == 'true' }} |
| 49 | + |
| 50 | + - name: verify nix in path |
76 | 51 | shell: bash |
77 | 52 | run: nix --version |
78 | | - - name: Setup KVM permissions |
79 | | - shell: bash |
80 | | - run: | |
81 | | - if [ -e /dev/kvm ]; then |
82 | | - sudo chown runner /dev/kvm |
83 | | - sudo chmod 666 /dev/kvm |
84 | | - echo "KVM configured: $(ls -l /dev/kvm)" |
85 | | - else |
86 | | - echo "KVM device not available" |
87 | | - fi |
0 commit comments