Distributed build: Difference between revisions
imported>Srid →NixOS: Use ssh-ng (more reliable) |
imported>Doronbehar Rewrite most of the guide, mostly with better specification of remote vs local machine, with additional usecases. |
||
| Line 4: | Line 4: | ||
There is a dedicated chapter in the [https://nixos.org/manual/nix/stable/advanced-topics/distributed-builds.html Nix Manual]. | There is a dedicated chapter in the [https://nixos.org/manual/nix/stable/advanced-topics/distributed-builds.html Nix Manual]. | ||
This is a step by step guide to setting up distributed builds. | This is a step by step guide to setting up distributed builds, that includes some SSH tips that are out of scope for Nix Manual's chapter. | ||
== Prerequisites == | == Prerequisites == | ||
SSH access to the remote builder must be setup for being able to distribute builds to it. The table below lists SSH access requirements for (hopefully) all the scenarios you might find yourself: | |||
{{ | {| class="wikitable" style="margin:auto" | ||
|- | |||
! Local Machine !! Remote Builder !! Requirements | |||
|- | |||
| NixOS / System-wide Nix installation || NixOS / System-wide Nix installation || '''Local''' machine's {{ic|root}} user needs SSH access to ''a'' user on the '''remote''' machine. | |||
|- | |||
| Single-user Nix installation || NixOS / System-wide Nix installation || ''Your'' user on your '''Local''' machine needs SSH access to ''a'' user on the '''remote''' machine. | |||
|- | |||
| NixOS / System-wide Nix installation || Single-user Nix installation || '''Local''' machine's {{ic|root}} user needs SSH access to the user on the '''remote''' machine with Nix installed with their UID (see [https://nixos.org/manual/nix/stable/installation/single-user.html Nix manual page]). | |||
|- | |||
| Single-user Nix installation || NixOS / System-wide Nix installation || ''Your'' user on your '''Local''' machine needs SSH access to the user on the '''remote''' machine with Nix installed with their UID (see [https://nixos.org/manual/nix/stable/installation/single-user.html Nix manual page]). | |||
|} | |||
In any case, the definitive test for SSH access for Nix is: | |||
{{Commands|nix store ping --store ssh://<REMOTE-BUILDER>}} | |||
Where {{ic|<REMOTE-BUILDER>}} is the remote builder's IP address, host address or whatever name you configure in {{ic|~/.ssh/config}} or {{ic|/root/.ssh/config}}, including the {{ic|user@}} prefix. | |||
An alternative check, is the following: | |||
{{Commands|ssh <REMOTE-BUILDER> 'type nix-store'}} | |||
The following sections guide you how to setup such authentication, with security in mind, and maximal comfort, assuming basic knowledge about SSH authentication keys. | |||
===General Best practices=== | |||
It is recommended to not allow {{ic|root}} access to the remote machine, even if only via an SSH public/private key pair! Especially because it's not required in any of the 4 scenarios described in the table above. | |||
In all of the cases above it is recommended to create an SSH public / private key pair without a passphrase, so that you won't have to run {{ic|ssh-add}} along with {{ic|ssh-agent}} prior to using the remote builder. In cases the local machine has NixOS / System-wide installation of Nix you'd probably need to spawn {{ic|eval $(ssh-agent)}} while you are logged in as {{ic|root}}. Not using a passphrase for the SSH key allows other users to enjoy the remote builder. | |||
Since the access to the remote machine doesn't have to be privileged, you can choose to login to a weakly privileged, and password-locked user on the remote machine, which may help you feel comfortable with the fact {{ic|root}} can access it without a passphrase. | |||
{{Tip|The above assumes you have root access on the '''remote''' machine, that allows you to create such a weakly priviledged user, or that you have good relationship with your system administrator - which is the expected scenario if they have installed Nix system wide.}} | |||
In case the remote machine doesn't have NixOS / System-wide Nix installation, you probably don't have a choice, but to allow access without passphrase and with an SSH key to the user with Nix installed for them. | |||
===Recommended System-wide Nix -> System-wide Nix setup=== | |||
For the common case where your local Nix is installed system-wide. It is recommended to create a user on the '''remote''' machine, that will have an unwriteable home directory, with a {{ic|~/.ssh/authorized_keys}} in it, that will allow SSH access to that user without passphrase. | |||
Here | Here are steps that will help you to achieve that: | ||
{{ | - {{ic|ssh}} to the remote builder. | ||
- Run (requires priveledges) {{ic|useradd -m -L nixremote}} where {{ic|-L}} locks the user such that no body will be able to {{ic|su}} to it, and {{ic|-m}} makes sure a home directory is create for the {{ic|nixremote}} user. | |||
- Run (requires priveledges) {{ic|mkdir ~nixremote/.ssh}}. | |||
In case your '''remote builder''' has Nix installed system-wide, but without NixOS, you may need to add something like the following to your {{ic|/etc/ssh/sshd_config}}: | |||
{{file|/etc/ssh/sshd_config|text| | |||
<nowiki> | |||
SetEnv PATH=/nix/var/nix/profiles/default/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |||
</nowiki> | </nowiki> | ||
}} | }} | ||
{{ | Such that straight away when logging in, your {{ic|$PATH}} will include nix' executables' directory. | ||
Then, '''on your local machine''', create the private / public key pair without a passphrase, as root, run: | |||
{{Commands| | {{Commands| | ||
# ssh-keygen -f /root/.ssh/nixremote | |||
}} | |||
}} | |||
Copy the contents of {{ic|/root/.ssh/nixremote.pub}} '''from your local machine''' to '''the remote builder''' {{ic|~nixremote/.ssh/authorized_keys}}. | |||
Then to further harden the setup, remove write permissions from everyone '''on the remote host''''s {{ic|nixremote}} home directory with: | |||
{{ | |||
{{Commands| | |||
# chmod -R a-w ~nixremote | |||
}}. | |||
Now you'd want to make it easy for {{ic|root}} on '''your local machine''' to connect to {{ic|nixremote@builder}}. You can do that by creating the following {{ic|/root/.ssh/config}} on the '''Local''' machine: | |||
{{file|/root/.ssh/config|text| | |||
<nowiki> | |||
< | Host builder # Replace by IP address, or add a ProxyCommand, see `man ssh_config` for full docs. | ||
# | # Prevent using ssh-agent or another keyfile, useful for testing | ||
IdentitiesOnly yes | |||
IdentityFile /root/.ssh/nixremote | |||
# The weakly privileged user on the remote builder - if not set, `root` is used - which will hopefully fail | |||
User nixremote | |||
</ | </nowiki> | ||
}} | |||
{{Tip|When connecting to a new remote builder, ssh will ask you whether you trust the identity of the builder. Nix needs fully unattended connection, so you may want to accept all fingerprints by default. To achieve this you can use <code><nowiki>StrictHostKeyChecking=accept-new</nowiki></code> in that config section. Note that this has security implications if you usually really check the fingerprints when prompted to do so. If you anticipate the host key won't change in the future, you can add it manually to '''the local machines'''' {{ic|/root/.ssh/known_hosts}}.}} | |||
You may also want to make nix on '''the remote machine''' trust that new user by adding it to {{ic|nix.trustedUsers}} if it's using NixOS, or by manually adding <code><nowiki>trusted-users = nixremote</nowiki></code> to {{ic|/etc/nix/nix.conf}}. | |||
You may also want to tell Nix '''on your local machine''' that the remote builder is available, and tell it what are it's ''supported features'' (see [https://github.com/NixOS/nix/issues/7380 missing nix documentation issue]). If your '''local machine''', uses NixOS, you can use the documented [https://search.nixos.org/options?channel=unstable&from=0&size=15&sort=relevance&type=packages&query=nix.buildmachine {{ic|nix.buildMachines}}] NixOS option. | |||
Here's an example NixOS configuration that may be a useful inspiration: | |||
{{file|/etc/nixos/configuration.nix|nix|<nowiki> | {{file|/etc/nixos/configuration.nix|nix|<nowiki> | ||
{ config, pkgs, ... }: | { config, pkgs, ... }: | ||
| Line 101: | Line 127: | ||
{{Evaluate}} | {{Evaluate}} | ||
==== Remote Builders' Features ==== | |||
Each builder is declared with a set of <code>supportedFeatures</code>. | Each builder is declared with a set of <code>supportedFeatures</code>. | ||
When a builder lacks one of the <code>requiredSystemFeatures</code> of a derivation, it will be ignored. Here are some features used in nixpkgs: | When a builder lacks one of the <code>requiredSystemFeatures</code> of a derivation, it will be ignored. Here are some features used in nixpkgs: | ||
| Line 134: | Line 150: | ||
|} | |} | ||
=== Non standard Nix installations === | |||
If you are not root on the remote builder and have used '''nix-user-chroot''' or '''PRoot''' to install nix there (see [[Nix Installation Guide]]) then nix is not available on the PATH of the remote builder. We describe a solution for nix-user-chroot which is easily adapted to PRoot. | |||
* Create a script <code>~/bin/nix_wrapper.sh</code> as follows: | |||
<syntaxHighlight lang="sh"> | |||
#!/bin/sh | |||
exec ~/bin/nix-user-chroot ~/.nix bash -c ' | |||
. ~/.nix-profile/etc/profile.d/nix.sh | |||
exec $SSH_ORIGINAL_COMMAND | |||
' | |||
</syntaxHighlight> | |||
Of course, adapt this script to the location of the store and nix-user-chroot. Make the script executable. | |||
* In <code>~/.ssh/authorized_keys</code>, locate the line corresponding to <code>~/.ssh/nixremote.pub</code> and prepend this: <code>command="/home/something/bin/nix_wrapper.sh"</code>. | |||
Now ssh will transparently run nix-user-chroot when you connect to the remote builder with the specified ssh key. | |||
== Using remote builders == | |||
==== Local builder ==== | |||
Your local machine is still a builder, notably when connecting to remote builders fails, nix will fallback to building locally. | |||
To never use the local machine set the <code>max-jobs</code> nix option to 0 | |||
{{Commands|$ nix-build -j0 blah}} | |||
====Using remote builders as substituters==== | ====Using remote builders as substituters==== | ||
| Line 170: | Line 210: | ||
* How do I know why my builds aren't being distributed? | * How do I know why my builds aren't being distributed? | ||
** Run <code>nix build -vvvvvvvvv 2>&1 | less</code> and search for <code>decline</code>. | ** Run <code>nix build -vvvvvvvvv 2>&1 | less</code> and search for <code>decline</code>. | ||
* I can <code>nix ping | * I can <code>nix store ping</code> but the build doesn't distribute. | ||
** If on NixOS, Check that <code>nix ping | ** If on NixOS, Check that <code>nix store ping</code> command works when run as root. | ||
** If you configured builders on the command line (with <code>--builders</code>), make sure your account is in <code>nix.trustedUsers</code> in <code>/etc/nixos/configuration.nix</code>. Only <code>/etc/nix/nix.conf</code> is taken into account otherwise. | ** If you configured builders on the command line (with <code>--builders</code>), make sure your account is in <code>nix.trustedUsers</code> in <code>/etc/nixos/configuration.nix</code>. Only <code>/etc/nix/nix.conf</code> is taken into account otherwise. | ||
* I can ping the store as root, but I'm getting "broken pipe" errors when trying to distribute. | * I can ping the store as root, but I'm getting "broken pipe" errors when trying to distribute. | ||