├── .gitignore ├── LICENSE ├── Makefile ├── NOTICE ├── README.md └── archlinux-nix /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | DESTDIR=/ 2 | prefix=usr 3 | 4 | install: 5 | mkdir -p "${DESTDIR}${prefix}/bin" 6 | install -m 755 archlinux-nix "${DESTDIR}${prefix}/bin/" 7 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | archlinux-nix 2 | Copyright (C) 2019 Alastair Pharo 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | archlinux-nix 2 | ============= 3 | 4 | This is a script that helps set up [Nix][nix] on [Arch Linux][arch]. It supports 5 | two distinct ways of installing Nix: 6 | 7 | 1. Via [AUR][aur], using the [nix package][aur-nix]; or 8 | 2. Directly via Nix (i.e. Nix is "self-hosted"). 9 | 10 | In the case of the latter, this script helps to install Nix. You can also use 11 | the [official installer][nix-official-install]. 12 | 13 | Other scripts 14 | 15 | 1. creating a build group and set of build users; and 16 | 2. setting up a sandbox for builds. 17 | 3. launching the nix-daemon. 18 | 19 | 20 | Installation 21 | ------------ 22 | 23 | This script is available via the Arch Linux [AUR][aur]. You can install either 24 | the [archlinux-nix package][archlinux-nix] or the 25 | [archlinux-nix-git package][archlinux-nix-git] if you want the bleeding edge. 26 | 27 | Alternatively, if you attempting to use this script elsewhere, you can clone 28 | this repo and do the following: 29 | 30 | ``` 31 | install archlinux-nix /usr/local/bin 32 | ``` 33 | 34 | 35 | Usage 36 | ----- 37 | 38 | ## Basic usage 39 | 40 | If you are installing Nix from AUR, this script is called automatically, so you 41 | shouldn't need to execute this script at all. If you want to do a 42 | "self-hosted" install, you can execute the following (as root): 43 | 44 | ``` 45 | archlinux-nix bootstrap 46 | ``` 47 | 48 | This will "intelligently" execute various commands (described below) with 49 | sensible defaults to get you up and running. 50 | 51 | ## Status 52 | 53 | ``` 54 | archlinux-nix status 55 | ``` 56 | 57 | Displays some info about whether Nix is installed, etc. 58 | 59 | 60 | ## Install "self-hosted" Nix 61 | 62 | ``` 63 | archlinux-nix install 64 | ``` 65 | 66 | This code (mostly pilfered from the [official install script][nix-official-install]) will 67 | download nix in binary format, install it into `/nix` and add environment 68 | setup to `/etc/profile.d`. 69 | 70 | 71 | ## Set-up build users 72 | 73 | ``` 74 | archlinux-nix setup-build-group 75 | ``` 76 | 77 | This will: 78 | 79 | 1. create a group called `nixbld`, and a set of ten system users, 80 | `nixbld{1..10}`; 81 | 2. add a `build-users-group` line to `nix.conf`; 82 | 3. kill the `nix-daemon` if it's running (so that it can pick up the 83 | new settings); and 84 | 3. fix the ownership on the nix store to be writable by the build 85 | users. 86 | 87 | If you don't like `nixbld`, you can specify a different name: 88 | 89 | ``` 90 | archlinux-nix setup-build-group mynixbuild 91 | ``` 92 | 93 | This would create a group called `mynixbuild`, and users 94 | `mynixbuild{1..10}`. 95 | 96 | ## Get rid of build users 97 | 98 | ``` 99 | archlinux-nix delete-build-group 100 | ``` 101 | 102 | This will: 103 | 104 | 1. determine the name of the group used in nix.conf; 105 | 2. remove the `build-users-group` line from `nix.conf`; 106 | 3. kill the nix-daemon, if it's running; 107 | 4. remove the ten users associated with the group; and 108 | 5. remove the group itself. 109 | 110 | You can also specify a group name to delete a group of users that are not 111 | specified in nix.conf: 112 | 113 | ``` 114 | archlinux-nix delete-build-group mygroup 115 | ``` 116 | 117 | This will skip step 1 in the above series. 118 | 119 | 120 | ## Setup sandboxing 121 | 122 | Note that by default sandboxing is enabled in Nix. For a self-hosted Nix 123 | install, no additional configuration is needed. If you installed Nix via 124 | AUR, sandboxind is setup automatically using the following command: 125 | 126 | ``` 127 | archlinux-nix install-sandbox 128 | ``` 129 | 130 | This creates a Nix profile at `/nix/var/nix/profiles/arch-system/build-sandbox` 131 | that includes the essential scripts required to build nix expressions 132 | (bash, tar, etc.), and the references these in `/etc/nix/nix.conf`. 133 | 134 | ## Stop sandboxing 135 | 136 | ``` 137 | archlinux-nix delete-sandbox 138 | archlinux-nix disable-sandbox 139 | ``` 140 | 141 | The former command will do the opposite of `install-sandbox` (i.e. remove the 142 | `build-sandbox` profile and any mention of it from `nix.conf`); the latter 143 | command will disable sanboxing entirely by adding a line to that effect to 144 | `nix.conf`. 145 | 146 | ## Stopping/starting nix-daemon 147 | 148 | ``` 149 | archlinux-nix enable-nix-daemon 150 | ``` 151 | 152 | This will link (if required) and launch the `nix-daemon` systemd service/socket. 153 | 154 | ``` 155 | archlinux-nix disable-nix-daemon 156 | ``` 157 | 158 | This will do the opposite. 159 | 160 | License 161 | ------- 162 | 163 | Licenced under the Apache License, Version 2.0. 164 | 165 | [nix]: https://nixos.org/nix/ 166 | [arch]: https://www.archlinux.org/ 167 | [nix-official-install]: https://nixos.org/nix/download.html 168 | [archlinux-nix]: https://aur.archlinux.org/packages/archlinux-nix 169 | [archlinux-nix-git]: https://aur.archlinux.org/packages/archlinux-nix-git 170 | [aur]: https://aur.archlinux.org/ 171 | [aur-nix]: https://aur.archlinux.org/packages/nix/ 172 | -------------------------------------------------------------------------------- /archlinux-nix: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | sandbox_binaries="" 4 | sandbox_packages="bash" 5 | default_nixos_version="20.03" 6 | default_group="nixbld" 7 | conf_file=/etc/nix/nix.conf 8 | 9 | dest=/nix 10 | 11 | nix_version=2.3.5 12 | 13 | case $(uname -m) in 14 | x86_64) system=x86_64-linux ;; 15 | i?86) system=i686-linux ;; 16 | aarch64) system=aarch64-linux ;; 17 | *) system=""; hash="" ;; 18 | esac 19 | 20 | oops() { 21 | echo -e "$0:" "$@" >&2 22 | exit 1 23 | } 24 | 25 | pm_path() { 26 | which -a nix 2>/dev/null | grep '^/usr' | head -n 1 27 | } 28 | 29 | pm_installed() { 30 | [ -n "$(pm_path)" ] 31 | } 32 | 33 | nix_installed() { 34 | [ -f "$dest"/var/nix/profiles/default/bin/nix ] 35 | } 36 | 37 | is_installed() { 38 | pm_installed || nix_installed 39 | } 40 | 41 | do_install() { 42 | if pm_installed; then 43 | oops "Nix was installed via some other package manager (e.g. AUR). You should use that if you need to update it." 44 | fi 45 | 46 | if [ -z "$system" ]; then 47 | oops "No binary is available for this system" 48 | fi 49 | 50 | local tmpDir="$(mktemp -d -t nix-binary-tarball-unpack.XXXXXXXXXX || \ 51 | oops "Can\'t create temporary directory for downloading the Nix binary tarball")" 52 | 53 | cleanup() { 54 | rm -rf "$tmpDir" 55 | } 56 | trap cleanup EXIT INT QUIT TERM 57 | 58 | local tarball="$tmpDir/nix-$nix_version-$system.tar.xz" 59 | local url="https://releases.nixos.org/nix/nix-$nix_version/nix-$nix_version-$system.tar.xz" 60 | 61 | echo "downloading Nix $nix_version binary tarball for $system from '$url' to '$tmpDir'..." 62 | curl -L "$url" -o "$tarball" || oops "failed to download '$url'" 63 | 64 | local hash1=$(curl -L $url.sha256) 65 | local hash2="$(sha256sum -b "$tarball" | cut -c1-64)" 66 | 67 | if [ "$hash1" != "$hash2" ]; then 68 | oops "SHA-256 hash mismatch in '$url'; expected $hash1, got $hash2" 69 | fi 70 | 71 | local unpack=$tmpDir/unpack 72 | mkdir -p "$unpack" 73 | < "$tarball" xzcat | tar x -C "$unpack" || oops "failed to unpack '$url'" 74 | local self="$unpack/nix-$nix_version-$system" 75 | 76 | mkdir -pm 0755 "$dest/store" && chown root.root "$dest" "$dest/store" 77 | cp -a -t "$dest/store/" "$self/store/"* || oops "failed to copy store" 78 | 79 | local nix="$dest/store/$(cd "$self/store" && ls -d *-nix-$nix_version)" 80 | 81 | echo "Initialising Nix database..." >&2 82 | if ! "$nix/bin/nix-store" --option build-users-group "" --init; then 83 | oops "failed to initialize the Nix database" 84 | fi 85 | 86 | if ! "$nix/bin/nix-store" --option build-users-group "" --load-db < "$self/.reginfo"; then 87 | oops "unable to register valid paths" 88 | fi 89 | 90 | . "$nix/etc/profile.d/nix.sh" 91 | 92 | if ! "$nix/bin/nix-env" --option build-users-group "" -i "$nix"; then 93 | oops "unable to install Nix into your default profile" 94 | fi 95 | 96 | ln -s $dest/var/nix/profiles/default/etc/profile.d/nix*.sh /etc/profile.d/ 97 | 98 | echo -e "It is recommended that you now bootstrap the Nix install:\n\n\ 99 | \t$0 bootstrap" 100 | } 101 | 102 | install_sandbox() { 103 | local version=$1 104 | mkdir -p $dest/var/nix/profiles/arch-system || exit 1 105 | NIX_REMOTE="" nix-build \ 106 | --option build-use-sandbox false \ 107 | -o $dest/var/nix/profiles/arch-system/build-sandbox \ 108 | -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/$version.tar.gz \ 109 | -E "with import {}; buildEnv { name = \"arch-system-build-sandbox\"; paths = [ $sandbox_packages ]; }" || exit 1 110 | } 111 | 112 | sandbox_exists() { 113 | [ -d /nix/var/nix/profiles/arch-system/build-sandbox ] 114 | } 115 | 116 | delete_sandbox() { 117 | rm -r /nix/var/nix/profiles/arch-system/build-sandbox 118 | } 119 | 120 | sandbox_path() { 121 | readlink -f "/nix/var/nix/profiles/arch-system/build-sandbox/bin/$1" 122 | } 123 | 124 | sandbox_paths() { 125 | [ -f "$conf_file" ] && grep '^\s*\(build-\)\?sandbox-paths\s*=' "$conf_file" | sed 's/^[^=]*=\s*\(.*\)$/\1/' 126 | } 127 | 128 | gen_sandbox_paths() { 129 | local paths="/bin/sh=$(sandbox_path bash)" 130 | for bin in $sandbox_binaries; do 131 | paths+=" /usr/bin/$bin=$(sandbox_path $bin)" 132 | done 133 | echo "sandbox-paths = $paths" 134 | } 135 | 136 | delete_sandbox_paths() { 137 | if [ -f "$conf_file" ]; then 138 | sed -i '/^\s*\(build-\)\?sandbox-paths\s*=/ d' "$conf_file" 139 | fi 140 | } 141 | 142 | create_config() { 143 | mkdir -p $(dirname "$conf_file") 144 | touch "$conf_file" 145 | } 146 | 147 | # Append the sandbox paths into the given file 148 | append_sandbox_paths() { 149 | delete_sandbox_paths 150 | create_config 151 | gen_sandbox_paths >> "$conf_file" 152 | } 153 | 154 | group_exists() { 155 | local group=$1 156 | getent group $group > /dev/null 157 | } 158 | 159 | user_exists() { 160 | local user=$1 161 | getent passwd $user > /dev/null 162 | } 163 | 164 | create_users() { 165 | local group=$1 166 | 167 | mkdir /etc/sysusers.d/ || true 168 | :> /etc/sysusers.d/nix.conf 169 | 170 | # Create a nixbld group. 171 | echo "g $group -" >> /etc/sysusers.d/nix.conf 172 | 173 | # Create 10 users 174 | for i in $(seq 10); do 175 | local user=$group$i 176 | echo "u $user -" >> /etc/sysusers.d/nix.conf 177 | echo "m $user $group" >> /etc/sysusers.d/nix.conf 178 | done 179 | 180 | systemctl restart systemd-sysusers.service 181 | } 182 | 183 | delete_users() { 184 | local group=$1 185 | 186 | rm /etc/sysusers.d/nix.conf 187 | systemctl restart systemd-sysusers.service 188 | } 189 | 190 | delete_legacy_users() { 191 | local group=$1 192 | 193 | # Remove the users 194 | local user 195 | for i in {1..10}; do 196 | user=$group$i 197 | userdel $user 198 | done 199 | 200 | # Remove the group 201 | groupdel $group 202 | } 203 | 204 | delete_build_group() { 205 | if [ -f "$conf_file" ]; then 206 | sed -i '/^\s*build-users-group\s*=/ d' "$conf_file" 207 | fi 208 | } 209 | 210 | append_build_group() { 211 | delete_build_group 212 | 213 | local group=$1 214 | create_config 215 | echo "build-users-group = $group" >> "$conf_file" 216 | } 217 | 218 | create_store() { 219 | local group=$1 220 | 221 | # Create nix folders and set permissions 222 | mkdir -p /nix/store 223 | chown root.$group /nix/store 224 | chmod 1775 /nix/store 225 | mkdir -p /nix/var/nix/gcroots/per-user 226 | mkdir -p /nix/var/nix/profiles/per-user 227 | chmod 1777 /nix/var/nix/{gcroots,profiles}/per-user 228 | } 229 | 230 | restore_store() { 231 | # Restore folder permissions 232 | chmod 755 /nix/store 233 | chown root.root /nix/store 234 | chmod 755 /nix/var/nix/{gcroots,profiles}/per-user 235 | } 236 | 237 | build_users_group() { 238 | [ -f "$conf_file" ] && grep '^\s*build-users-group\s*=' "$conf_file" | sed 's/^.*=\s*\(\w*\)\W/\1/' 239 | } 240 | 241 | sandbox_disabled() { 242 | [ -f "$conf_file" ] && grep -q '^\s*build-use-sandbox\s*=\s*false' "$conf_file" 243 | } 244 | 245 | delete_use_sandbox() { 246 | if [ -f "$conf_file" ]; then 247 | sed -i '/^\s*build-use-sandbox\s*=/ d' "$conf_file" 248 | fi 249 | } 250 | 251 | append_disable_sandbox() { 252 | delete_use_sandbox 253 | create_config 254 | echo 'build-use-sandbox = false' >> "$conf_file" 255 | } 256 | 257 | check_loaded() { 258 | systemctl show $1 --property=LoadState | grep -q "=loaded" 259 | } 260 | 261 | daemon_service_loaded() { 262 | check_loaded nix-daemon.service 263 | } 264 | 265 | daemon_socket_loaded() { 266 | check_loaded nix-daemon.socket 267 | } 268 | 269 | daemon_loaded() { 270 | daemon_service_loaded && daemon_socket_loaded 271 | } 272 | 273 | daemon_socket_active() { 274 | systemctl --quiet is-active nix-daemon.socket 275 | } 276 | 277 | stop_daemon_service() { 278 | if daemon_service_loaded; then 279 | systemctl stop nix-daemon.service 280 | fi 281 | } 282 | 283 | stop_daemon_socket() { 284 | if daemon_service_loaded && daemon_socket_active; then 285 | systemctl disable --now nix-daemon.socket 286 | fi 287 | } 288 | 289 | start_daemon_socket() { 290 | if daemon_socket_loaded; then 291 | systemctl enable nix-daemon.socket 292 | systemctl restart nix-daemon.socket 293 | fi 294 | } 295 | 296 | link_system_unit_from_profile() { 297 | systemctl link $dest/var/nix/profiles/$1/lib/systemd/system/$2 298 | } 299 | 300 | load_daemon() { 301 | if nix_installed; then 302 | link_system_unit_from_profile default nix-daemon.service 303 | link_system_unit_from_profile default nix-daemon.socket 304 | else 305 | systemctl revert nix-daemon.socket 306 | systemctl revert nix-daemon.service 307 | fi 308 | 309 | systemctl daemon-reload 310 | } 311 | 312 | do_status() { 313 | if pm_installed; then 314 | echo "Nix installed via package manager ($(pm_path))" 315 | elif nix_installed; then 316 | echo "Nix self-installed" 317 | else 318 | oops "Nix not installed" 319 | fi 320 | 321 | local sandbox=yes 322 | sandbox_disabled && sandbox=no 323 | local group=$(build_users_group) 324 | 325 | local exists 326 | if [ -n "$group" ]; then 327 | echo "Configured build group: $group" 328 | 329 | exists=no 330 | group_exists $group && exists=yes 331 | echo "Group exists: $exists" 332 | 333 | exists=yes 334 | for user in $group{1..10}; do 335 | if ! user_exists $user; then 336 | exists=no 337 | break 338 | fi 339 | done 340 | 341 | echo "Users exist: $exists" 342 | else 343 | echo "No configured build group." 344 | fi 345 | 346 | echo "Using sandbox (recommended): $sandbox" 347 | 348 | if [ $sandbox = yes ]; then 349 | if pm_installed; then 350 | exists=no 351 | sandbox_exists && exists=yes 352 | echo "Sandbox installed: $exists" 353 | fi 354 | 355 | local paths=$(sandbox_paths) 356 | 357 | if [ -n "$paths" ]; then 358 | echo "Sandbox paths:" 359 | for path in $paths; do 360 | echo " - $path" 361 | done 362 | elif pm_installed; then 363 | echo "Sandbox paths missing from config file" 364 | else 365 | echo "Sandbox paths not required" 366 | fi 367 | fi 368 | 369 | echo -n "Nix-daemon service: " 370 | if daemon_service_loaded; then 371 | echo loaded 372 | else 373 | echo not loaded 374 | fi 375 | 376 | echo -n "Nix-daemon socket: " 377 | if ! daemon_socket_loaded; then 378 | echo not loaded 379 | elif daemon_socket_active; then 380 | echo active 381 | else 382 | echo not active 383 | fi 384 | } 385 | 386 | do_install_sandbox() { 387 | if nix_installed; then 388 | oops "Sandbox paths not required" 389 | fi 390 | 391 | local version=$1 392 | 393 | if [ -z "$version" ]; then 394 | version=$default_nixos_version 395 | fi 396 | 397 | echo "Installing sandbox binaries from NixOS-$version ..." 398 | install_sandbox $version || exit 1 399 | 400 | echo "Adding build-sandbox-paths to "$conf_file" ..." 401 | append_sandbox_paths || exit 1 402 | 403 | echo "Enabling sandboxing ..." 404 | delete_use_sandbox || exit 1 405 | 406 | echo "Killing daemon ..." 407 | stop_daemon_service 408 | } 409 | 410 | do_delete_sandbox() { 411 | echo "Removing sandbox config ..." 412 | delete_sandbox_paths || exit 1 413 | delete_use_sandbox || exit 1 414 | 415 | echo "Killing daemon ..." 416 | stop_daemon_service 417 | 418 | echo "Unlinking sandbox ..." 419 | delete_sandbox || exit 1 420 | 421 | echo "You may wish to run 'nix-store --gc' to delete files" 422 | } 423 | 424 | do_disable_sandbox() { 425 | echo "Disabling build sandboxing ..." 426 | append_disable_sandbox || exit 1 427 | 428 | echo "Killing daemon ..." 429 | stop_daemon_service 430 | } 431 | 432 | do_setup_build_group() { 433 | local group=$1 434 | 435 | if [ -z "$group" ]; then 436 | group=$default_group 437 | fi 438 | 439 | echo "Creating group '$group' and users ..." 440 | create_users $group 441 | 442 | echo "Setting group in "$conf_file" ..." 443 | append_build_group $group 444 | 445 | echo "Killing daemon ..." 446 | stop_daemon_service 447 | 448 | echo "Setting permissions on nix store ..." 449 | create_store $group 450 | } 451 | 452 | do_delete_build_group() { 453 | local group=$1 454 | 455 | if [ -z $group ]; then 456 | group=$(build_users_group) 457 | if [ -z $group ]; then 458 | oops "No existing group setup" 459 | fi 460 | fi 461 | 462 | echo "Deleting build group $group ..." 463 | 464 | if [ "$(build_users_group)" = "$group" ]; then 465 | echo "Removing build group from "$conf_file" ..." 466 | delete_build_group 467 | 468 | echo "Killing daemon ..." 469 | stop_daemon_service 470 | 471 | echo "Resetting permissions on nix store ..." 472 | restore_store 473 | fi 474 | 475 | echo "Deleting group and users ..." 476 | delete_users $group 477 | delete_legacy_users $group 478 | } 479 | 480 | do_enable_nix_daemon() { 481 | if ! daemon_loaded; then 482 | echo "Loading nix-daemon into systemd ..." 483 | load_daemon || exit 1 484 | 485 | if ! daemon_loaded; then 486 | echo "nix-daemon has not loaded. Exiting ..." 487 | exit 1 488 | fi 489 | fi 490 | 491 | if ! daemon_socket_active; then 492 | echo "Activating nix-daemon.socket ..." 493 | start_daemon_socket || exit 1 494 | fi 495 | } 496 | 497 | do_disable_nix_daemon() { 498 | if daemon_loaded; then 499 | echo "Stopping and deactivating nix-daemon socket ..." 500 | stop_daemon_socket || exit 1 501 | echo "Stopping nix-daemon service (if running) ..." 502 | stop_daemon_service || exit 1 503 | else 504 | echo "Nix-daemon is not loaded" 505 | fi 506 | } 507 | 508 | do_bootstrap() { 509 | # Step 1: install Nix 510 | if ! is_installed; then 511 | oops "You need to install Nix before bootstrapping.\n\ 512 | You can install Nix from AUR:\n\n\ 513 | \thttps://aur.archlinux.org/packages/nix/\n\n\ 514 | Alternatively, you can use Nix itself to install Nix (\"self-hosted\" Nix) with\n\ 515 | the help of this tool:\n\n\ 516 | \t$0 install" 517 | fi 518 | 519 | # Step 2: setup build users 520 | local group=$(build_users_group) 521 | if [ -z "$group" ] || ( ! group_exists $group ); then 522 | do_setup_build_group $group || exit 1 523 | else 524 | # Make sure the /nix folder has the correct permissions 525 | create_store $group || exit 1 526 | fi 527 | 528 | # Step 3: setup sandboxing if required 529 | if pm_installed && ( ! sandbox_disabled ) && [ -z "$(sandbox_paths)" ]; then 530 | do_install_sandbox || exit 1 531 | fi 532 | 533 | 534 | # Step 4: launch the daemon 535 | do_enable_nix_daemon || exit 1 536 | 537 | echo "Nix is bootstrapped." 538 | 539 | # Additional instructions for users that have installed self-hosted nix 540 | if nix_installed; then 541 | local nix_env=$dest/var/nix/profiles/default/bin/nix-env 542 | local nix=$(dirname $(dirname $(realpath "$nix_env"))) 543 | echo 544 | echo "To get nix-* commands in your path, you need to ensure your local profile has the" 545 | echo "nix package installed. It can be installed via the following command:" 546 | echo 547 | echo -e "\t\"$nix_env\" -i \"$nix\"" 548 | echo 549 | echo "You also need to source the following files (or otherwise log out and in):" 550 | echo 551 | echo -e "\tsource /etc/profile.d/nix{,-daemon}.sh" 552 | echo 553 | fi 554 | } 555 | 556 | usage() { 557 | echo "$(basename $0) " 558 | echo -e "\nCOMMAND:" 559 | 560 | echo -e "\tBasic Commands" 561 | echo -e "\t--------------\n" 562 | 563 | if ! pm_installed; then 564 | echo -e "\tinstall" 565 | echo -e "\t\tInstall \"self-hosted\" Nix. Note that it is also possible to install Nix using" 566 | echo -e "\t\ta package manager (e.g. AUR)" 567 | fi 568 | 569 | echo -e "\tbootstrap" 570 | echo -e "\t\tSetup Nix with sensible defaults" 571 | 572 | echo -e "\tstatus\n\t\tList current status" 573 | 574 | echo -e "\n" 575 | echo -e "\tAdvanced Commands" 576 | echo -e "\t-----------------\n" 577 | 578 | echo -e "\tinstall-sandbox [VERSION]" 579 | echo -e "\t\tSetup sandbox using binaries from specified NixOS version (defaults to $default_nixos_version)" 580 | 581 | echo -e "\tdelete-sandbox" 582 | echo -e "\t\tDelete sandbox and remove sandbox config" 583 | 584 | echo -e "\tdisable-sandbox" 585 | echo -e "\t\tDisable sandboxing" 586 | 587 | echo -e "\tsetup-build-group [GROUP]" 588 | echo -e "\t\tCreate a group of build users and add to config (defaults to $default_group)" 589 | 590 | echo -e "\tdelete-build-group [GROUP]" 591 | echo -e "\t\tDelete group and users (defaults to existing group given in nix.conf)" 592 | 593 | echo -e "\tenable-nix-daemon" 594 | echo -e "\t\tEnable and start the nix-daemon" 595 | 596 | echo -e "\tdisable-nix-daemon" 597 | echo -e "\t\tDisable and stop the nix-daemon" 598 | } 599 | 600 | command=$1 601 | [ -n "$command" ] && shift 602 | 603 | case $command in 604 | 'install') do_install ;; 605 | 'bootstrap') do_bootstrap ;; 606 | 'status') do_status ;; 607 | 'install-sandbox') do_install_sandbox "$@" ;; 608 | 'delete-sandbox') do_delete_sandbox "$@" ;; 609 | 'disable-sandbox') do_disable_sandbox ;; 610 | 'setup-build-group') do_setup_build_group "$@" ;; 611 | 'delete-build-group') do_delete_build_group "$@" ;; 612 | 'enable-nix-daemon') do_enable_nix_daemon ;; 613 | 'disable-nix-daemon') do_disable_nix_daemon ;; 614 | 'help' | '--help' | '-h' | '') usage ;; 615 | *) oops "$(basename $0): unknown option '$command $@'" ;; 616 | esac 617 | --------------------------------------------------------------------------------