Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .github/workflows/test-per-system.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ on:
required: false
default: 'nix-2.8.0'
type: string
oldest_installer_sha256:
description: 'SHA-256 of the oldest_installer_version installer script'
required: false
default: 'f43bfedfca9151479462d8bc11f238615a2d52f19b14894d8e2b59980e75ef72'
type: string

env:
nixpkgs_channel: nixpkgs=channel:nixos-25.05
Expand Down Expand Up @@ -78,11 +83,19 @@ jobs:
curl --location https://github.com/cachix/nar-toolbox/releases/download/v0.1.0/nar-toolbox-${{ inputs.system }} -O
chmod +x ./nar-toolbox-${{ inputs.system }}
./nar-toolbox-${{ inputs.system }} serve https://cache.nixos.org &
# Resolve latest-finished to a fixed build, so the URL and the hash Hydra reports for it can't drift apart
- name: Find latest installer
id: latest-installer
run: |
build=$(curl -sSfL --retry 5 -H 'Accept: application/json' https://hydra.nixos.org/job/nix/master/installerScript/latest-finished)
echo "url=https://hydra.nixos.org/build/$(jq -r '.id' <<<"$build")/download/1/install" >>"$GITHUB_OUTPUT"
echo "sha256=$(jq -r '.buildproducts."1".sha256hash' <<<"$build")" >>"$GITHUB_OUTPUT"
- name: Install Nix
uses: ./
with:
nix_path: ${{ env.nixpkgs_channel }}
install_url: https://hydra.nixos.org/job/nix/master/installerScript/latest-finished/download/1/install
install_url: ${{ steps.latest-installer.outputs.url }}
install_sha256: ${{ steps.latest-installer.outputs.sha256 }}
install_options: "--tarball-url-prefix http://localhost:8080"
- run: nix-build tests/test-build.nix

Expand All @@ -95,4 +108,5 @@ jobs:
with:
nix_path: ${{ env.nixpkgs_channel }}
install_url: https://releases.nixos.org/nix/${{ inputs.oldest_installer_version }}/install
install_sha256: ${{ inputs.oldest_installer_sha256 }}
- run: nix-build tests/test-build.nix
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,69 @@ jobs:
- runs-on: ubuntu-latest
system: x86_64-linux
oldest_installer_version: nix-2.8.0
oldest_installer_sha256: f43bfedfca9151479462d8bc11f238615a2d52f19b14894d8e2b59980e75ef72
- runs-on: ubuntu-22.04
system: x86_64-linux
oldest_installer_version: nix-2.8.0
oldest_installer_sha256: f43bfedfca9151479462d8bc11f238615a2d52f19b14894d8e2b59980e75ef72
- runs-on: ubuntu-24.04-arm
system: aarch64-linux
oldest_installer_version: nix-2.8.0
oldest_installer_sha256: f43bfedfca9151479462d8bc11f238615a2d52f19b14894d8e2b59980e75ef72
- runs-on: ubuntu-22.04-arm
system: aarch64-linux
oldest_installer_version: nix-2.8.0
oldest_installer_sha256: f43bfedfca9151479462d8bc11f238615a2d52f19b14894d8e2b59980e75ef72
- runs-on: macos-latest
system: aarch64-darwin
oldest_installer_version: nix-2.18.6
oldest_installer_sha256: b4eedc0c884003d3f2b9ca2a4bb08ea14f7eab15f25800470161d6b51d3179b9
- runs-on: macos-26
system: aarch64-darwin
oldest_installer_version: nix-2.18.6
oldest_installer_sha256: b4eedc0c884003d3f2b9ca2a4bb08ea14f7eab15f25800470161d6b51d3179b9
- runs-on: macos-15
system: aarch64-darwin
oldest_installer_version: nix-2.18.6
oldest_installer_sha256: b4eedc0c884003d3f2b9ca2a4bb08ea14f7eab15f25800470161d6b51d3179b9
uses: ./.github/workflows/test-per-system.yml
with:
runs-on: ${{ matrix.runs-on }}
system: ${{ matrix.system }}
oldest_installer_version: ${{ matrix.oldest_installer_version }}
oldest_installer_sha256: ${{ matrix.oldest_installer_sha256 }}

# The action must refuse to run an installer that doesn't match install_sha256
install-sha256-mismatch:
strategy:
fail-fast: false
matrix:
runs-on: [ubuntu-latest, macos-latest]
install_url:
# Default installer
- ""
# Custom installer
- https://releases.nixos.org/nix/nix-2.18.6/install
runs-on: ${{ matrix.runs-on }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Install Nix
id: install-nix
continue-on-error: true
uses: ./
with:
install_url: ${{ matrix.install_url }}
install_sha256: "0000000000000000000000000000000000000000000000000000000000000000"
- name: Check that the install failed
run: |
if [[ "${{ steps.install-nix.outcome }}" != "failure" ]]; then
echo "Expected the install to fail on a SHA-256 mismatch, got: ${{ steps.install-nix.outcome }}" >&2
exit 1
fi
if command -v nix >/dev/null 2>&1 || [[ -e /nix/var/nix ]]; then
echo "Nix was installed despite the SHA-256 mismatch" >&2
exit 1
fi

act-support:
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/update-nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ jobs:
echo "Latest Nix version: ${latest_nix}"
echo "CURRENT_NIX=${current_nix}" >> $GITHUB_ENV
echo "LATEST_NIX=${latest_nix}" >> $GITHUB_ENV
latest_sha256=$(curl -sSfL --retry 5 "https://releases.nixos.org/nix/nix-${latest_nix}/install.sha256")
if ! [[ "$latest_sha256" =~ ^[0-9a-f]{64}$ ]]; then
echo "Failed to fetch the installer hash for Nix ${latest_nix}." >&2
exit 1
fi
sed -i -E "s/nix_version=[0-9.]+/nix_version=${latest_nix}/" ./install-nix.sh
sed -i -E "s/nix_install_sha256=[0-9a-f]+/nix_install_sha256=${latest_sha256}/" ./install-nix.sh
- name: Create Pull Request
uses: peter-evans/create-pull-request@v8
with:
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Installs [Nix](https://nixos.org/nix/) on GitHub Actions runners for Linux and m
- Quick installation (~4s on Linux / ~20s on macOS)
- Multi-user installation with sandboxing enabled by default on Linux
- Support for [self-hosted GitHub runners](https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners)
- Allows specifying Nix installation URL via `install_url` (the oldest supported Nix version is 2.3.5)
- Allows specifying Nix installation URL via `install_url` (the oldest supported Nix version is 2.3.5), verified against `install_sha256` if set
- Allows specifying extra Nix configuration options via `extra_nix_config`
- Allows specifying `$NIX_PATH` and channels via `nix_path`
- Enables KVM on supported machines: run VMs and NixOS tests with full hardware-acceleration
Expand Down Expand Up @@ -59,6 +59,7 @@ jobs:
| Name | Description | Default |
|------|-------------|---------|
| `install_url` | URL to install Nix from. Useful for testing non-stable releases or pinning a specific Nix version (e.g., <https://releases.nixos.org/nix/nix-2.3.7/install>) | `""` |
| `install_sha256` | Expected SHA-256 of the installer script. Optional, but recommended when `install_url` is set | Pinned hash of the default installer |
| `install_options` | Additional flags to pass to the Nix installer script | `""` |
| `extra_nix_config` | Additional configuration to append to `/etc/nix/nix.conf` | `""` |
| `nix_path` | Value to set for the `NIX_PATH` environment variable (e.g., `nixpkgs=channel:nixos-unstable`) | `""` |
Expand Down Expand Up @@ -114,6 +115,25 @@ Use `nix_path` to configure optional channels by [picking a channel](https://sta

See also the [tutorial on pinning on nix.dev](https://nix.dev/tutorials/towards-reproducibility-pinning-nixpkgs).

### How do I install a specific version of Nix?

Point `install_url` at the installer for that version, and set `install_sha256`
to its SHA-256 (releases publish it next to the installer, at `<install_url>.sha256`):

```yaml
- uses: cachix/install-nix-action@v32
with:
install_url: https://releases.nixos.org/nix/nix-2.18.6/install
install_sha256: b4eedc0c884003d3f2b9ca2a4bb08ea14f7eab15f25800470161d6b51d3179b9
```

`install_sha256` is optional, but recommended: the action hashes the downloaded installer
and refuses to run it if the hash doesn't match, so a compromised or
unexpectedly changed installer fails the step instead of running on your runner.
Without it, the action prints a warning and runs the installer unverified.

The default installer is always verified against a hash pinned in the action.

### How do I run NixOS tests on Linux?

```yaml
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ inputs:
description: 'Configure Nix to pull from GitHub using the given GitHub token.'
install_url:
description: 'Installation URL that will contain a script to install Nix.'
install_sha256:
description: 'Expected SHA-256 of the installer script. Defaults to the pinned hash of the default installer; with a custom `install_url` and no hash, the installer is not verified.'
install_options:
description: 'Additional installer flags passed to the installer script.'
nix_path:
Expand All @@ -33,6 +35,7 @@ runs:
INPUT_GITHUB_ACCESS_TOKEN: ${{ inputs.github_access_token }}
INPUT_INSTALL_OPTIONS: ${{ inputs.install_options }}
INPUT_INSTALL_URL: ${{ inputs.install_url }}
INPUT_INSTALL_SHA256: ${{ inputs.install_sha256 }}
INPUT_NIX_PATH: ${{ inputs.nix_path }}
INPUT_ENABLE_KVM: ${{ inputs.enable_kvm }}
INPUT_SET_AS_TRUSTED_USER: ${{ inputs.set_as_trusted_user }}
Expand Down
43 changes: 42 additions & 1 deletion install-nix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,25 @@ echo "installer options: ${installer_options[*]}"
# There is --retry-on-errors, but only newer curl versions support that
curl_retries=5
nix_version=2.36.0
while ! curl -sS -o "$workdir/install" -v --fail -L "${INPUT_INSTALL_URL:-https://releases.nixos.org/nix/nix-${nix_version}/install}"; do
# sha256 of https://releases.nixos.org/nix/nix-${nix_version}/install
# Kept in sync with nix_version by .github/workflows/update-nix.yml
nix_install_sha256=8388dfd4d3c3a0c2dc79f764d453eafcc244255f47c93c1958f4775af22f0353

# Pick the installer and the hash to check it against:
# - default installer: the pinned hash above, unless install_sha256 overrides it
# - custom install_url: install_sha256 if set, otherwise the installer is not verified
if [[ -n "${INPUT_INSTALL_URL:-}" ]]; then
install_url=$INPUT_INSTALL_URL
expected_sha256=${INPUT_INSTALL_SHA256:-}
if [[ -z "$expected_sha256" ]]; then
echo "::warning::install_url is set but install_sha256 is not, so the installer will not be verified. Set install_sha256 to the SHA-256 of '$install_url'."
fi
else
install_url="https://releases.nixos.org/nix/nix-${nix_version}/install"
expected_sha256=${INPUT_INSTALL_SHA256:-$nix_install_sha256}
fi

while ! curl -sS -o "$workdir/install" -v --fail -L "$install_url"; do
sleep 1
((curl_retries--))
if [[ $curl_retries -le 0 ]]; then
Expand All @@ -111,6 +129,29 @@ while ! curl -sS -o "$workdir/install" -v --fail -L "${INPUT_INSTALL_URL:-https:
fi
done

# Verify the integrity of the installer
if [[ -n "$expected_sha256" ]]; then
if command -v sha256sum >/dev/null 2>&1; then
actual_sha256="$(sha256sum -b "$workdir/install" | cut -c1-64)"
elif command -v shasum >/dev/null 2>&1; then
actual_sha256="$(shasum -a 256 -b "$workdir/install" | cut -c1-64)"
elif command -v openssl >/dev/null 2>&1; then
actual_sha256="$(openssl dgst -r -sha256 "$workdir/install" | cut -c1-64)"
else
echo "Cannot verify the SHA-256 hash of '$install_url'; you need one of 'sha256sum', 'shasum', or 'openssl'" >&2
exit 1
fi

# Compare case-insensitively, hashes are often published in either case
# (tr instead of ${var,,}: macOS ships bash 3.2)
expected_sha256=$(tr '[:upper:]' '[:lower:]' <<<"$expected_sha256")
if [[ "$actual_sha256" != "$expected_sha256" ]]; then
echo "SHA-256 hash mismatch for '$install_url': expected $expected_sha256, got $actual_sha256" >&2
exit 1
fi
echo "Verified SHA-256 of '$install_url': $actual_sha256"
fi

sh "$workdir/install" "${installer_options[@]}"

# Configure the environment
Expand Down
Loading