diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3c4d041..09b8412 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: @@ -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 diff --git a/README.md b/README.md index 3c15c7a..0f6c25f 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,19 @@ Installs [Nix](https://nixos.org/nix/) on GitHub Actions for the supported platforms: Linux and macOS. +By default it has no channels configured, you have to set `nix_path` +by [picking a channel](https://status.nixos.org/) +or [pin nixpkgs yourself](https://nix.dev/tutorials/towards-reproducibility-pinning-nixpkgs.html). + +# 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: @@ -28,14 +41,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 -- `install_url`: specify URL to install Nix from (mostly useful for testing non-stable releases) +``` +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 +``` -- `nix_path`: set `NIX_PATH` environment variable (if set `skip_adding_nixpkgs_channel` will be implicitly enabled) +## Inputs (specify using `with:`) -- `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. +- `install_url`: specify URL to install Nix from (useful for testing non-stable releases) + +- `nix_path`: set `NIX_PATH` environment variable, for example `nixpkgs=channel:nixos-unstable` + +- `extra_nix_config`: append to `/etc/nix/nix.conf` --- diff --git a/action.yml b/action.yml index c5124a7..9c3b0b0 100644 --- a/action.yml +++ b/action.yml @@ -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: diff --git a/lib/install-nix.sh b/lib/install-nix.sh index 01b5fe4..b6083ea 100755 --- a/lib/install-nix.sh +++ b/lib/install-nix.sh @@ -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" @@ -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 @@ -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