├── .editorconfig ├── .github ├── dependabot.yml └── workflows │ ├── test.yml │ └── update-nix.yml ├── .gitignore ├── LICENSE ├── README.md ├── RELEASE.md ├── action.yml ├── install-nix.sh └── test.nix /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | charset = utf-8 9 | end_of_line = lf 10 | indent_size = 2 11 | indent_style = space 12 | insert_final_newline = true 13 | 14 | [LICENSE] 15 | indent_size = unset 16 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | 4 | - package-ecosystem: github-actions 5 | directory: "/" 6 | schedule: 7 | interval: daily 8 | time: '00:00' 9 | timezone: UTC 10 | open-pull-requests-limit: 10 11 | commit-message: 12 | prefix: "chore" 13 | include: "scope" -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: "install-nix-action test" 2 | on: 3 | pull_request: 4 | push: 5 | branches: 6 | - master 7 | 8 | env: 9 | nixpkgs_channel: nixpkgs=channel:nixos-24.11 10 | oldest_supported_installer: nix-2.8.0 11 | 12 | jobs: 13 | simple-build: 14 | strategy: 15 | fail-fast: false 16 | matrix: 17 | os: 18 | - ubuntu-latest 19 | - ubuntu-24.04-arm 20 | - macos-latest 21 | - macos-13 22 | runs-on: ${{ matrix.os }} 23 | steps: 24 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 25 | - name: Install Nix 26 | uses: ./ 27 | with: 28 | nix_path: ${{ env.nixpkgs_channel }} 29 | - run: nix-env -iA cachix -f https://cachix.org/api/v1/install 30 | - run: cat /etc/nix/nix.conf 31 | # cachix should be available and be able to configure a cache 32 | - run: cachix use cachix 33 | - run: nix-build test.nix 34 | 35 | custom-nix-path: 36 | strategy: 37 | fail-fast: false 38 | matrix: 39 | os: 40 | - ubuntu-latest 41 | - ubuntu-24.04-arm 42 | - macos-latest 43 | - macos-13 44 | runs-on: ${{ matrix.os }} 45 | steps: 46 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 47 | - name: Install Nix 48 | uses: ./ 49 | with: 50 | nix_path: ${{ env.nixpkgs_channel }} 51 | - run: test $NIX_PATH == '${{ env.nixpkgs_channel }}' 52 | - run: nix-build test.nix 53 | 54 | extra-nix-config: 55 | strategy: 56 | fail-fast: false 57 | matrix: 58 | os: 59 | - ubuntu-latest 60 | - ubuntu-24.04-arm 61 | - macos-latest 62 | - macos-13 63 | runs-on: ${{ matrix.os }} 64 | steps: 65 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 66 | - name: Install Nix 67 | uses: ./ 68 | with: 69 | nix_path: ${{ env.nixpkgs_channel }} 70 | extra_nix_config: | 71 | sandbox = relaxed 72 | - run: cat /etc/nix/nix.conf 73 | - run: nix-build test.nix --arg noChroot true 74 | 75 | flakes: 76 | strategy: 77 | fail-fast: false 78 | matrix: 79 | os: 80 | - ubuntu-latest 81 | - ubuntu-24.04-arm 82 | - macos-latest 83 | - macos-13 84 | runs-on: ${{ matrix.os }} 85 | steps: 86 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 87 | - name: Install Nix 88 | uses: ./ 89 | - run: nix flake show github:NixOS/nixpkgs 90 | 91 | latest-installer: 92 | strategy: 93 | fail-fast: false 94 | matrix: 95 | include: 96 | - os: ubuntu-latest 97 | system: x86_64-linux 98 | - os: ubuntu-24.04-arm 99 | system: aarch64-linux 100 | - os: macos-latest 101 | system: aarch64-darwin 102 | - os: macos-13 103 | system: x86_64-darwin 104 | runs-on: ${{ matrix.os }} 105 | steps: 106 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 107 | - name: Run NAR server 108 | run: | 109 | curl --location https://github.com/cachix/nar-toolbox/releases/download/v0.1.0/nar-toolbox-${{ matrix.system }} -O 110 | chmod +x ./nar-toolbox-${{ matrix.system }} 111 | ./nar-toolbox-${{ matrix.system }} serve https://cache.nixos.org & 112 | - name: Install Nix 113 | uses: ./ 114 | with: 115 | nix_path: ${{ env.nixpkgs_channel }} 116 | install_url: https://hydra.nixos.org/job/nix/master/installerScript/latest-finished/download/1/install 117 | install_options: "--tarball-url-prefix http://localhost:8080" 118 | - run: nix-build test.nix 119 | 120 | oldest-supported-installer: 121 | strategy: 122 | fail-fast: false 123 | matrix: 124 | os: 125 | - ubuntu-latest 126 | - ubuntu-24.04-arm 127 | - macos-latest 128 | - macos-13 129 | runs-on: ${{ matrix.os }} 130 | steps: 131 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 132 | - name: Install Nix 133 | uses: ./ 134 | with: 135 | nix_path: ${{ env.nixpkgs_channel }} 136 | install_url: https://releases.nixos.org/nix/${{ env.oldest_supported_installer }}/install 137 | - run: nix-build test.nix 138 | 139 | act-support: 140 | strategy: 141 | matrix: 142 | os: [ubuntu-latest] 143 | runs-on: ${{ matrix.os }} 144 | steps: 145 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 146 | - run: curl https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash 147 | - run: docker pull ghcr.io/catthehacker/ubuntu:js-24.04 148 | - run: | 149 | ./bin/act push \ 150 | -P ubuntu-latest=ghcr.io/catthehacker/ubuntu:js-24.04 \ 151 | -j simple-build \ 152 | --matrix os:ubuntu-latest 153 | -------------------------------------------------------------------------------- /.github/workflows/update-nix.yml: -------------------------------------------------------------------------------- 1 | name: "Update nix" 2 | on: 3 | repository_dispatch: 4 | workflow_dispatch: 5 | schedule: 6 | - cron: "31 2 * * *" 7 | jobs: 8 | update-nix-releases: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v4 12 | - name: Update nix releases 13 | env: 14 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} 15 | run: | 16 | latest_nix=$( 17 | gh api repos/NixOS/nix/tags --paginate --jq '.[].name' | 18 | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | 19 | sort -V | 20 | tail -n 1 21 | ) 22 | if [ -z "$latest_nix" ]; then 23 | echo "Failed to determine latest Nix version." >&2 24 | exit 1 25 | fi 26 | sed -i -E "s/nix_version=[0-9.]+/nix_version=${latest_nix}/" ./install-nix.sh 27 | - name: Create Pull Request 28 | uses: peter-evans/create-pull-request@v7 29 | with: 30 | title: Update nix versions 31 | labels: dependencies 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # dotenv environment variables file 2 | .env* 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --- 204 | 205 | The MIT License (MIT) 206 | 207 | Copyright (c) 2018 GitHub, Inc. and contributors 208 | 209 | Permission is hereby granted, free of charge, to any person obtaining a copy 210 | of this software and associated documentation files (the "Software"), to deal 211 | in the Software without restriction, including without limitation the rights 212 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 213 | copies of the Software, and to permit persons to whom the Software is 214 | furnished to do so, subject to the following conditions: 215 | 216 | The above copyright notice and this permission notice shall be included in 217 | all copies or substantial portions of the Software. 218 | 219 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 220 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 221 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 222 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 223 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 224 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 225 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # install-nix-action 2 | 3 | ![GitHub Actions badge](https://github.com/cachix/install-nix-action/workflows/install-nix-action%20test/badge.svg) 4 | 5 | Installs [Nix](https://nixos.org/nix/) on GitHub Actions for the supported platforms: Linux and macOS. 6 | 7 | By default it has no nixpkgs configured, you have to set `nix_path` 8 | by [picking a channel](https://status.nixos.org/) 9 | or [pin nixpkgs yourself](https://nix.dev/reference/pinning-nixpkgs) 10 | (see also [pinning tutorial](https://nix.dev/tutorials/towards-reproducibility-pinning-nixpkgs)). 11 | 12 | # Features 13 | 14 | - Quick installation (~4s on Linux, ~20s on macOS) 15 | - Multi-User installation (with sandboxing enabled only on Linux) 16 | - [Self-hosted GitHub runner](https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners) support 17 | - Allows specifying Nix installation URL via `install_url` (the oldest supported Nix version is 2.3.5) 18 | - Allows specifying extra Nix configuration options via `extra_nix_config` 19 | - Allows specifying `$NIX_PATH` and channels via `nix_path` 20 | - Share `/nix/store` between builds using [cachix-action](https://github.com/cachix/cachix-action) for simple binary cache setup to speed up your builds and share binaries with your team 21 | - Enables KVM on supported machines: run VMs and NixOS tests with full hardware-acceleration 22 | 23 | ## Usage 24 | 25 | Create `.github/workflows/test.yml` in your repo with the following contents: 26 | 27 | ```yaml 28 | name: "Test" 29 | on: 30 | pull_request: 31 | push: 32 | jobs: 33 | tests: 34 | runs-on: ubuntu-latest 35 | steps: 36 | - uses: actions/checkout@v4 37 | - uses: cachix/install-nix-action@v31 38 | with: 39 | nix_path: nixpkgs=channel:nixos-unstable 40 | - run: nix-build 41 | ``` 42 | 43 | ## Usage with Flakes 44 | 45 | ```yaml 46 | name: "Test" 47 | on: 48 | pull_request: 49 | push: 50 | jobs: 51 | tests: 52 | runs-on: ubuntu-latest 53 | steps: 54 | - uses: actions/checkout@v4 55 | - uses: cachix/install-nix-action@v31 56 | with: 57 | github_access_token: ${{ secrets.GITHUB_TOKEN }} 58 | - run: nix build 59 | - run: nix flake check 60 | ``` 61 | 62 | To install Nix from any commit, go to [the corresponding installer_test action](https://github.com/NixOS/nix/runs/2219534360) and click on "Run cachix/install-nix-action@XX" step and expand the first line. 63 | 64 | ## Inputs (specify using `with:`) 65 | 66 | - `extra_nix_config`: append to `/etc/nix/nix.conf` 67 | 68 | - `github_access_token`: configure Nix to pull from GitHub using the given GitHub token. This helps work around rate limit issues. Has no effect when `access-tokens` is also specified in `extra_nix_config`. 69 | 70 | - `install_url`: specify URL to install Nix from (useful for testing non-stable releases or pinning Nix, for example https://releases.nixos.org/nix/nix-2.3.7/install) 71 | 72 | - `install_options`: additional installer flags passed to the installer script. 73 | 74 | - `nix_path`: set `NIX_PATH` environment variable, for example `nixpkgs=channel:nixos-unstable` 75 | 76 | - `enable_kvm`: whether to enable KVM for hardware-accelerated virtualization on Linux. Enabled by default if available. 77 | 78 | - `set_as_trusted_user`: whether to add the current user to `trusted-users`. Enabled by default. 79 | 80 | 81 | ## Differences from the default Nix installer 82 | 83 | Some settings have been optimised for use in CI environments: 84 | 85 | - `nix.conf` settings. Override these defaults with `extra_nix_config`: 86 | 87 | - The experimental `flakes` and `nix-command` features are enabled. Disable by overriding `experimental-features` in `extra_nix_config`. 88 | 89 | - `max-jobs` is set to `auto`. 90 | 91 | - `show-trace` is set to `true`. 92 | 93 | - `$USER` is added to `trusted-users`. 94 | 95 | - `$GITHUB_TOKEN` is added to `access_tokens` if no other `github_access_token` is provided. 96 | 97 | - `always-allow-substitutes` is set to `true`. 98 | 99 | - `ssl-cert-file` is set to `/etc/ssl/cert.pem` on macOS. 100 | 101 | - KVM is enabled on Linux if available. Disable by setting `enable_kvm: false`. 102 | 103 | - `$TMPDIR` is set to `$RUNNER_TEMP` if empty. 104 | 105 | --- 106 | 107 | ## FAQ 108 | 109 | ### How do I print nixpkgs version I have configured? 110 | 111 | ```yaml 112 | - name: Print nixpkgs version 113 | run: nix-instantiate --eval -E '(import {}).lib.version' 114 | ``` 115 | 116 | ### How do I run NixOS tests? 117 | 118 | With the following inputs: 119 | 120 | ```yaml 121 | - uses: cachix/install-nix-action@vXX 122 | with: 123 | enable_kvm: true 124 | extra_nix_config: "system-features = nixos-test benchmark big-parallel kvm" 125 | ``` 126 | 127 | ### How do I install packages via nix-env from the specified `nix_path`? 128 | 129 | ``` 130 | nix-env -i mypackage -f '' 131 | ``` 132 | 133 | ### How do I add a binary cache? 134 | 135 | If the binary cache you want to add is hosted on [Cachix](https://cachix.org/) and you are 136 | using [cachix-action](https://github.com/cachix/cachix-action), you 137 | should use their `extraPullNames` input like this: 138 | 139 | ```yaml 140 | - uses: cachix/cachix-action@vXX 141 | with: 142 | name: mycache 143 | authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' 144 | extraPullNames: nix-community 145 | ``` 146 | 147 | Otherwise, you can add any binary cache to nix.conf using 148 | install-nix-action's own `extra_nix_config` input: 149 | 150 | ```yaml 151 | - uses: cachix/install-nix-action@v31 152 | with: 153 | extra_nix_config: | 154 | trusted-public-keys = hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= 155 | substituters = https://hydra.iohk.io https://cache.nixos.org/ 156 | ``` 157 | 158 | ### How do I pass environment variables to commands run with `nix develop` or `nix shell`? 159 | 160 | Nix runs commands in a restricted environment by default, called `pure mode`. 161 | In pure mode, environment variables are not passed through to improve the reproducibility of the shell. 162 | 163 | You can use the `--keep / -k` flag to keep certain environment variables: 164 | 165 | ```yaml 166 | - name: Run a command with nix develop 167 | run: nix develop --ignore-environment --keep MY_ENV_VAR --command echo $MY_ENV_VAR 168 | env: 169 | MY_ENV_VAR: "hello world" 170 | ``` 171 | 172 | Or you can disable pure mode entirely with the `--impure` flag: 173 | 174 | ``` 175 | nix develop --impure 176 | ``` 177 | 178 | ### How do I pass AWS credentials to the Nix daemon? 179 | 180 | In multi-user mode, Nix commands that operate on the Nix store are forwarded to a privileged daemon. This daemon runs in a separate context from your GitHub Actions workflow and cannot access the workflow's environment variables. Consequently, any secrets or credentials defined in your workflow environment will not be available to Nix operations that require store access. 181 | 182 | There are two ways to pass AWS credentials to the Nix daemon: 183 | - Configure a default profile using the AWS CLI 184 | - Install Nix in single-user mode 185 | 186 | #### Configure a default profile using the AWS CLI 187 | 188 | The Nix daemon supports reading AWS credentials from the `~/.aws/credentials` file. 189 | 190 | We can use the AWS CLI to configure a default profile using short-lived credentials fetched using OIDC: 191 | 192 | ```yaml 193 | job: 194 | build: 195 | runs-on: ubuntu-latest 196 | # Required permissions to request AWS credentials 197 | permissions: 198 | id-token: write 199 | contents: read 200 | steps: 201 | - uses: actions/checkout@v4 202 | - uses: cachix/install-nix-action@v31 203 | - name: Assume AWS Role 204 | uses: aws-actions/configure-aws-credentials@v4.1.0 205 | with: 206 | aws-region: us-east-1 207 | role-to-assume: arn:aws-cn:iam::123456789100:role/my-github-actions-role 208 | - name: Make AWS Credentials accessible to nix-daemon 209 | run: | 210 | sudo -i aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}" 211 | sudo -i aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}" 212 | sudo -i aws configure set aws_session_token "${AWS_SESSION_TOKEN}" 213 | sudo -i aws configure set region "${AWS_REGION}" 214 | ``` 215 | 216 | #### Install Nix in single-user mode 217 | 218 | In some environments it may be possible to install Nix in single-user mode by passing the `--no-daemon` flag to the installer. 219 | This mode is normally used on platforms without an init system, like systemd, and in containerized environments with a single user that can own the entire Nix store. 220 | 221 | This approach is more generic as it allows passing environment variables directly to Nix, including secrets, proxy settings, and other configuration options. 222 | 223 | However, it may not be suitable for all environments. [Consult the Nix manual](https://nix.dev/manual/nix/latest/installation/nix-security) for the latest restrictions and differences between the two modes. 224 | 225 | For example, single-user mode is currently supported on hosted Linux GitHub runners, like `ubuntu-latest`. 226 | It is not supported on macOS runners, like `macos-latest`. 227 | 228 | ```yaml 229 | - uses: cachix/install-nix-action@v31 230 | with: 231 | install_options: --no-daemon 232 | ``` 233 | -------------------------------------------------------------------------------- /RELEASE.md: -------------------------------------------------------------------------------- 1 | # Release 2 | 3 | As of v31, releases of this action follow Semantic Versioning. 4 | 5 | ### Publishing a new release 6 | 7 | #### Publish the release 8 | 9 | Draft [a new release on GitHub](https://github.com/cachix/install-nix-action/releases): 10 | 11 | - In `Choose a tag`, create a new tag, like `v31.2.1`, following semver. 12 | - Click `Generate release notes`. 13 | - `Set as the latest release` should be selected automatically. 14 | - Publish release 15 | 16 | #### Update the major tag 17 | 18 | The major tag, like `v31`, allows downstream users to opt-in to automatic non-breaking updates. 19 | 20 | This process follows GitHub's own guidelines: 21 | https://github.com/actions/toolkit/blob/main/docs/action-versioning.md 22 | 23 | ##### Fetch the latest tags 24 | 25 | ``` 26 | git pull --tags --force 27 | ``` 28 | 29 | ##### Move the tag 30 | 31 | ``` 32 | git tag -fa v31 33 | ``` 34 | ``` 35 | git push origin v31 --force 36 | ``` 37 | 38 | #### Update the release notes for the major tag 39 | 40 | Find the release on GitHub: https://github.com/cachix/install-nix-action/releases 41 | 42 | Edit the release and click `Generate release notes`. 43 | Edit the formatting and publish. 44 | 45 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'Install Nix' 2 | description: 'Installs Nix on GitHub Actions for the supported platforms: Linux and macOS.' 3 | author: 'Domen Kožar' 4 | inputs: 5 | extra_nix_config: 6 | description: 'Gets appended to `/etc/nix/nix.conf` if passed.' 7 | github_access_token: 8 | description: 'Configure Nix to pull from GitHub using the given GitHub token.' 9 | install_url: 10 | description: 'Installation URL that will contain a script to install Nix.' 11 | install_options: 12 | description: 'Additional installer flags passed to the installer script.' 13 | nix_path: 14 | description: 'Set NIX_PATH environment variable.' 15 | enable_kvm: 16 | description: 'Enable KVM for hardware-accelerated virtualization on Linux, if available.' 17 | required: false 18 | default: true 19 | set_as_trusted_user: 20 | description: 'Add current user to `trusted-users`.' 21 | required: false 22 | default: true 23 | branding: 24 | color: 'blue' 25 | icon: 'sun' 26 | runs: 27 | using: 'composite' 28 | steps: 29 | - run : ${GITHUB_ACTION_PATH}/install-nix.sh 30 | shell: bash 31 | env: 32 | INPUT_EXTRA_NIX_CONFIG: ${{ inputs.extra_nix_config }} 33 | INPUT_GITHUB_ACCESS_TOKEN: ${{ inputs.github_access_token }} 34 | INPUT_INSTALL_OPTIONS: ${{ inputs.install_options }} 35 | INPUT_INSTALL_URL: ${{ inputs.install_url }} 36 | INPUT_NIX_PATH: ${{ inputs.nix_path }} 37 | INPUT_ENABLE_KVM: ${{ inputs.enable_kvm }} 38 | INPUT_SET_AS_TRUSTED_USER: ${{ inputs.set_as_trusted_user }} 39 | GITHUB_TOKEN: ${{ github.token }} 40 | -------------------------------------------------------------------------------- /install-nix.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | 4 | if nix_path="$(type -p nix)" ; then 5 | echo "Aborting: Nix is already installed at ${nix_path}" 6 | exit 7 | fi 8 | 9 | if [[ ($OSTYPE =~ linux) && ($INPUT_ENABLE_KVM == 'true') ]]; then 10 | enable_kvm() { 11 | echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-install-nix-action-kvm.rules 12 | sudo udevadm control --reload-rules && sudo udevadm trigger --name-match=kvm 13 | } 14 | 15 | echo '::group::Enabling KVM support' 16 | enable_kvm && echo 'Enabled KVM' || echo 'KVM is not available' 17 | echo '::endgroup::' 18 | fi 19 | 20 | # GitHub command to put the following log messages into a group which is collapsed by default 21 | echo "::group::Installing Nix" 22 | 23 | # Create a temporary workdir 24 | workdir=$(mktemp -d) 25 | trap 'rm -rf "$workdir"' EXIT 26 | 27 | # Configure Nix 28 | add_config() { 29 | echo "$1" >> "$workdir/nix.conf" 30 | } 31 | add_config "show-trace = true" 32 | # Set jobs to number of cores 33 | add_config "max-jobs = auto" 34 | if [[ $OSTYPE =~ darwin ]]; then 35 | add_config "ssl-cert-file = /etc/ssl/cert.pem" 36 | fi 37 | # Allow binary caches specified at user level 38 | if [[ $INPUT_SET_AS_TRUSTED_USER == 'true' ]]; then 39 | add_config "trusted-users = root ${USER:-}" 40 | fi 41 | # Add a GitHub access token. 42 | # Token-less access is subject to lower rate limits. 43 | if [[ -n "${INPUT_GITHUB_ACCESS_TOKEN:-}" ]]; then 44 | echo "::debug::Using the provided github_access_token for github.com" 45 | add_config "access-tokens = github.com=$INPUT_GITHUB_ACCESS_TOKEN" 46 | # Use the default GitHub token if available. 47 | # Skip this step if running an Enterprise instance. The default token there does not work for github.com. 48 | elif [[ -n "${GITHUB_TOKEN:-}" && $GITHUB_SERVER_URL == "https://github.com" ]]; then 49 | echo "::debug::Using the default GITHUB_TOKEN for github.com" 50 | add_config "access-tokens = github.com=$GITHUB_TOKEN" 51 | else 52 | echo "::debug::Continuing without a GitHub access token" 53 | fi 54 | # Append extra nix configuration if provided 55 | if [[ -n "${INPUT_EXTRA_NIX_CONFIG:-}" ]]; then 56 | add_config "$INPUT_EXTRA_NIX_CONFIG" 57 | fi 58 | if [[ ! $INPUT_EXTRA_NIX_CONFIG =~ "experimental-features" ]]; then 59 | add_config "experimental-features = nix-command flakes" 60 | fi 61 | # Always allow substituting from the cache, even if the derivation has `allowSubstitutes = false`. 62 | # This is a CI optimisation to avoid having to download the inputs for already-cached derivations to rebuild trivial text files. 63 | if [[ ! $INPUT_EXTRA_NIX_CONFIG =~ "always-allow-substitutes" ]]; then 64 | add_config "always-allow-substitutes = true" 65 | fi 66 | 67 | # Nix installer flags 68 | installer_options=( 69 | --no-channel-add 70 | --nix-extra-conf-file "$workdir/nix.conf" 71 | ) 72 | 73 | # only use the nix-daemon settings if on darwin (which get ignored) or systemd is supported 74 | if [[ (! $INPUT_INSTALL_OPTIONS =~ "--no-daemon") && ($OSTYPE =~ darwin || -e /run/systemd/system) ]]; then 75 | installer_options+=( 76 | --daemon 77 | --daemon-user-count "$(python3 -c 'import multiprocessing as mp; print(mp.cpu_count() * 2)')" 78 | ) 79 | else 80 | # "fix" the following error when running nix* 81 | # error: the group 'nixbld' specified in 'build-users-group' does not exist 82 | add_config "build-users-group =" 83 | sudo mkdir -p /etc/nix 84 | sudo chmod 0755 /etc/nix 85 | sudo cp "$workdir/nix.conf" /etc/nix/nix.conf 86 | fi 87 | 88 | if [[ -n "${INPUT_INSTALL_OPTIONS:-}" ]]; then 89 | IFS=' ' read -r -a extra_installer_options <<< "$INPUT_INSTALL_OPTIONS" 90 | installer_options=("${extra_installer_options[@]}" "${installer_options[@]}") 91 | fi 92 | 93 | echo "installer options: ${installer_options[*]}" 94 | 95 | # There is --retry-on-errors, but only newer curl versions support that 96 | curl_retries=5 97 | nix_version=2.29.0 98 | while ! curl -sS -o "$workdir/install" -v --fail -L "${INPUT_INSTALL_URL:-https://releases.nixos.org/nix/nix-${nix_version}/install}" 99 | do 100 | sleep 1 101 | ((curl_retries--)) 102 | if [[ $curl_retries -le 0 ]]; then 103 | echo "curl retries failed" >&2 104 | exit 1 105 | fi 106 | done 107 | 108 | sh "$workdir/install" "${installer_options[@]}" 109 | 110 | # Set paths 111 | echo "/nix/var/nix/profiles/default/bin" >> "$GITHUB_PATH" 112 | # new path for nix 2.14 113 | echo "$HOME/.nix-profile/bin" >> "$GITHUB_PATH" 114 | 115 | if [[ -n "${INPUT_NIX_PATH:-}" ]]; then 116 | echo "NIX_PATH=${INPUT_NIX_PATH}" >> "$GITHUB_ENV" 117 | fi 118 | 119 | # Set temporary directory (if not already set) to fix https://github.com/cachix/install-nix-action/issues/197 120 | if [[ -z "${TMPDIR:-}" ]]; then 121 | echo "TMPDIR=${RUNNER_TEMP}" >> "$GITHUB_ENV" 122 | fi 123 | 124 | # Close the log message group which was opened above 125 | echo "::endgroup::" 126 | -------------------------------------------------------------------------------- /test.nix: -------------------------------------------------------------------------------- 1 | # Realizes > of derivations with size of MB 2 | { size ? 1 # MB 3 | , num ? 10 # count 4 | , currentTime ? builtins.currentTime 5 | , noChroot ? false 6 | }: 7 | 8 | with import {}; 9 | 10 | let 11 | drv = i: runCommand "${toString currentTime}-${toString i}" { 12 | __noChroot = noChroot; 13 | } '' 14 | dd if=/dev/zero of=$out bs=${toString size}MB count=1 15 | ''; 16 | in writeText "empty-${toString num}-${toString size}MB" '' 17 | ${lib.concatMapStringsSep "" drv (lib.range 1 num)} 18 | '' 19 | --------------------------------------------------------------------------------