Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
install-nix: validate installer's hash
  • Loading branch information
Fumesover committed Nov 21, 2026
commit f4c81564be6a9591afc5e4de78481282819419b2
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_hash: ${{ 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_hash: ${{ inputs.oldest_installer_sha256 }}
- run: nix-build tests/test-build.nix
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,37 @@ 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 }}

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
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_hash` | Expected SHA-256 of the installer script. **Required when `install_url` is set.** Releases publish it next to the installer (e.g., <https://releases.nixos.org/nix/nix-2.3.7/install.sha256>) | 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
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_hash:
description: 'Expected SHA-256 of the installer script. Required when `install_url` is set.'
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_HASH: ${{ inputs.install_hash }}
INPUT_NIX_PATH: ${{ inputs.nix_path }}
INPUT_ENABLE_KVM: ${{ inputs.enable_kvm }}
INPUT_SET_AS_TRUSTED_USER: ${{ inputs.set_as_trusted_user }}
Expand Down
42 changes: 41 additions & 1 deletion install-nix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,26 @@ 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_hash overrides it
# - custom install_url: install_hash is required
if [[ -n "${INPUT_INSTALL_URL:-}" ]]; then
install_url=$INPUT_INSTALL_URL
expected_sha256=${INPUT_INSTALL_HASH:-}
if [[ -z "$expected_sha256" ]]; then
echo "::error::install_url is set but install_hash is not. Set install_hash to the SHA-256 of '$install_url'." >&2
exit 1
fi
else
install_url="https://releases.nixos.org/nix/nix-${nix_version}/install"
expected_sha256=${INPUT_INSTALL_HASH:-$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 +130,27 @@ while ! curl -sS -o "$workdir/install" -v --fail -L "${INPUT_INSTALL_URL:-https:
fi
done

# Verify the integrity of the installer
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"

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

# Configure the environment
Expand Down