Skip to content

Commit

Permalink
Support self-hosted runners and always skip adding a channel
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Sep 21, 2020
1 parent 5a2cbac commit 775aed5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
21 changes: 4 additions & 17 deletions .github/workflows/test.yml
Expand Up @@ -15,28 +15,14 @@ jobs:
- run: yarn build
- name: Install Nix
uses: ./
with:
nix_path: nixpkgs=channel:nixos-20.03
- run: nix-env -iA cachix -f https://cachix.org/api/v1/install
- run: cat /etc/nix/nix.conf
# cachix should be available and be able to configure a cache
- run: cachix use cachix
- run: nix-build test.nix

no-channel:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- run: yarn install --frozen-lockfile
- run: yarn build
- name: Install Nix
uses: ./
with:
skip_adding_nixpkgs_channel: true
- run: nix-build test.nix && exit 1 || echo "OK"
- run: NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/tarball/ab5863afada3c1b50fc43bf774b75ea71b287cde nix-build test.nix

custom-nix-path:
strategy:
matrix:
Expand Down Expand Up @@ -64,7 +50,8 @@ jobs:
- run: yarn build
- name: Install Nix
uses: ./
with:
with:
nix_path: nixpkgs=channel:nixos-20.03
extra_nix_config: |
sandbox = relaxed
- run: cat /etc/nix/nix.conf
Expand Down
41 changes: 36 additions & 5 deletions README.md
Expand Up @@ -4,6 +4,15 @@

Installs [Nix](https://nixos.org/nix/) on GitHub Actions for the supported platforms: Linux and macOS.

# Features

- Quick installation (~4s on Linux, ~20s on macOS)
- Multi-User mode with sandboxing enabled on Linux
- [Self-hosted github runner](https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners) support
- Allows specifying Nix installation URL
- Allows specifying extra Nix configration options
- Allows specifying `$NIX_PATH` and channels

## Usage

Create `.github/workflows/test.yml` in your repo with the following contents:
Expand All @@ -28,14 +37,36 @@ See also [cachix-action](https://github.com/cachix/cachix-action) for
simple binary cache setup to speed up your builds and share binaries
with developers.

## Options `with: ...`
# Usage with Flakes

```
name: "Test"
on:
pull_request:
push:
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
# Nix Flakes doesn't work on shallow clones
fetch-depth: 0
- uses: cachix/install-nix-action@v11
with:
install_url: https://github.com/numtide/nix-flakes-installer/releases/download/nix-3.0pre20200820_4d77513/install
extra_nix_config: |
experimental-features = nix-command flakes
- run: nix-build
```

## Inputs (specify using `with:`)

- `install_url`: specify URL to install Nix from (mostly useful for testing non-stable releases)
- `install_url`: specify URL to install Nix from (useful for testing non-stable releases)

- `nix_path`: set `NIX_PATH` environment variable (if set `skip_adding_nixpkgs_channel` will be implicitly enabled)
- `nix_path`: set `NIX_PATH` environment variable, for example `nixpkgs=channel:nixos-unstable`

- `skip_adding_nixpkgs_channel`: set to `true` to skip adding nixpkgs-unstable channel (and save ~5s for each job build)
- `extra_nix_config`: gets appended to `/etc/nix/nix.conf` if passed.
- `extra_nix_config`: append to `/etc/nix/nix.conf`

---

Expand Down
4 changes: 1 addition & 3 deletions action.yml
Expand Up @@ -5,9 +5,7 @@ inputs:
install_url:
description: 'Installation URL that will contain a script to install Nix'
nix_path:
description: 'Set NIX_PATH environment variable. If set "skip_adding_nixpkgs_channel" will be implicitly enabled'
skip_adding_nixpkgs_channel:
description: 'Skip adding nixpkgs-unstable channel'
description: 'Set NIX_PATH environment variable.'
extra_nix_config:
description: 'gets appended to `/etc/nix/nix.conf` if passed.'
branding:
Expand Down
18 changes: 8 additions & 10 deletions lib/install-nix.sh
Expand Up @@ -7,8 +7,8 @@ add_config() {
}
# Set jobs to number of cores
add_config "max-jobs = auto"
# Allow binary caches for runner user
add_config "trusted-users = root runner"
# Allow binary caches for user
add_config "trusted-users = root $USER"
# Append extra nix configuration if provided
if [[ $INPUT_EXTRA_NIX_CONFIG != "" ]]; then
add_config "$INPUT_EXTRA_NIX_CONFIG"
Expand All @@ -18,19 +18,17 @@ fi
installer_options=(
--daemon
--daemon-user-count 4
--no-channel-add
--darwin-use-unencrypted-nix-store-volume
--nix-extra-conf-file /tmp/nix.conf
)

if [[ $INPUT_SKIP_ADDING_NIXPKGS_CHANNEL = "true" || $INPUT_NIX_PATH != "" ]]; then
installer_options+=(--no-channel-add)
else
INPUT_NIX_PATH="/nix/var/nix/profiles/per-user/root/channels"
# On self-hosted runners we don't need to install more than once
if [ ! -d "/nix/store" ]
then
sh <(curl --retry 5 --retry-connrefused -L "${INPUT_INSTALL_URL:-https://nixos.org/nix/install}") "${installer_options[@]}"
fi

sh <(curl --retry 5 --retry-connrefused -L "${INPUT_INSTALL_URL:-https://nixos.org/nix/install}") \
"${installer_options[@]}"

if [[ $OSTYPE =~ darwin ]]; then
# Disable spotlight indexing of /nix to speed up performance
sudo mdutil -i off /nix
Expand All @@ -43,7 +41,7 @@ if [[ $OSTYPE =~ darwin ]]; then
fi

# Set paths
echo "::add-path::/nix/var/nix/profiles/per-user/runner/profile/bin"
echo "::add-path::/nix/var/nix/profiles/per-user/$USER/profile/bin"
echo "::add-path::/nix/var/nix/profiles/default/bin"

if [[ $INPUT_NIX_PATH != "" ]]; then
Expand Down

0 comments on commit 775aed5

Please sign in to comment.