2024-01-22 17:34:52 +00:00
|
|
|
test@{ config, lib, hostPkgs, ... }:
|
2023-10-31 16:32:09 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
pkgs = config.nodes.client.nixpkgs.pkgs;
|
|
|
|
|
|
|
|
# Trivial Nix expression to build remotely.
|
|
|
|
expr = config: nr: pkgs.writeText "expr.nix"
|
|
|
|
''
|
|
|
|
let utils = builtins.storePath ${config.system.build.extraUtils}; in
|
|
|
|
derivation {
|
|
|
|
name = "hello-${toString nr}";
|
|
|
|
system = "i686-linux";
|
|
|
|
PATH = "''${utils}/bin";
|
|
|
|
builder = "''${utils}/bin/sh";
|
|
|
|
args = [ "-c" "${
|
|
|
|
lib.concatStringsSep "; " [
|
|
|
|
''if [[ -n $NIX_LOG_FD ]]''
|
|
|
|
''then echo '@nix {\"action\":\"setPhase\",\"phase\":\"buildPhase\"}' >&''$NIX_LOG_FD''
|
|
|
|
"fi"
|
|
|
|
"echo Hello"
|
|
|
|
"mkdir $out"
|
|
|
|
"cat /proc/sys/kernel/hostname > $out/host"
|
|
|
|
]
|
|
|
|
}" ];
|
|
|
|
outputs = [ "out" ];
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2024-01-22 17:44:16 +00:00
|
|
|
options = {
|
|
|
|
builders.config = lib.mkOption {
|
|
|
|
type = lib.types.deferredModule;
|
|
|
|
description = ''
|
|
|
|
Configuration to add to the builder nodes.
|
|
|
|
'';
|
|
|
|
default = { };
|
2024-01-22 17:34:52 +00:00
|
|
|
};
|
2024-01-22 17:44:16 +00:00
|
|
|
};
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
config = {
|
|
|
|
name = lib.mkDefault "remote-builds-ssh-ng";
|
|
|
|
|
|
|
|
nodes =
|
2024-01-22 17:47:59 +00:00
|
|
|
{
|
|
|
|
builder =
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
{
|
|
|
|
imports = [ test.config.builders.config ];
|
|
|
|
services.openssh.enable = true;
|
|
|
|
virtualisation.writableStore = true;
|
|
|
|
nix.settings.sandbox = true;
|
|
|
|
nix.settings.substituters = lib.mkForce [ ];
|
|
|
|
};
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
client =
|
|
|
|
{ config, lib, pkgs, ... }:
|
2024-01-22 17:47:59 +00:00
|
|
|
{
|
|
|
|
nix.settings.max-jobs = 0; # force remote building
|
2024-01-22 17:44:16 +00:00
|
|
|
nix.distributedBuilds = true;
|
|
|
|
nix.buildMachines =
|
2024-01-22 17:47:59 +00:00
|
|
|
[{
|
|
|
|
hostName = "builder";
|
|
|
|
sshUser = "root";
|
|
|
|
sshKey = "/root/.ssh/id_ed25519";
|
|
|
|
system = "i686-linux";
|
|
|
|
maxJobs = 1;
|
|
|
|
protocol = "ssh-ng";
|
|
|
|
}];
|
2024-01-22 17:44:16 +00:00
|
|
|
virtualisation.writableStore = true;
|
|
|
|
virtualisation.additionalPaths = [ config.system.build.extraUtils ];
|
|
|
|
nix.settings.substituters = lib.mkForce [ ];
|
|
|
|
programs.ssh.extraConfig = "ConnectTimeout 30";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = { nodes }: ''
|
|
|
|
# fmt: off
|
|
|
|
import subprocess
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
start_all()
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
# Create an SSH key on the client.
|
|
|
|
subprocess.run([
|
|
|
|
"${hostPkgs.openssh}/bin/ssh-keygen", "-t", "ed25519", "-f", "key", "-N", ""
|
|
|
|
], capture_output=True, check=True)
|
|
|
|
client.succeed("mkdir -p -m 700 /root/.ssh")
|
|
|
|
client.copy_from_host("key", "/root/.ssh/id_ed25519")
|
|
|
|
client.succeed("chmod 600 /root/.ssh/id_ed25519")
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
# Install the SSH key on the builder.
|
|
|
|
client.wait_for_unit("network.target")
|
|
|
|
builder.succeed("mkdir -p -m 700 /root/.ssh")
|
|
|
|
builder.copy_from_host("key.pub", "/root/.ssh/authorized_keys")
|
|
|
|
builder.wait_for_unit("sshd")
|
|
|
|
client.succeed(f"ssh -o StrictHostKeyChecking=no {builder.name} 'echo hello world'")
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
# Perform a build
|
|
|
|
out = client.succeed("nix-build ${expr nodes.client 1} 2> build-output")
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
# Verify that the build was done on the builder
|
|
|
|
builder.succeed(f"test -e {out.strip()}")
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
# Print the build log, prefix the log lines to avoid nix intercepting lines starting with @nix
|
|
|
|
buildOutput = client.succeed("sed -e 's/^/build-output:/' build-output")
|
|
|
|
print(buildOutput)
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
# Make sure that we get the expected build output
|
|
|
|
client.succeed("grep -qF Hello build-output")
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
# We don't want phase reporting in the build output
|
|
|
|
client.fail("grep -qF '@nix' build-output")
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
# Get the log file
|
|
|
|
client.succeed(f"nix-store --read-log {out.strip()} > log-output")
|
|
|
|
# Prefix the log lines to avoid nix intercepting lines starting with @nix
|
|
|
|
logOutput = client.succeed("sed -e 's/^/log-file:/' log-output")
|
|
|
|
print(logOutput)
|
2023-10-31 16:32:09 +00:00
|
|
|
|
2024-01-22 17:44:16 +00:00
|
|
|
# Check that we get phase reporting in the log file
|
|
|
|
client.succeed("grep -q '@nix {\"action\":\"setPhase\",\"phase\":\"buildPhase\"}' log-output")
|
|
|
|
'';
|
|
|
|
};
|
2023-10-31 16:32:09 +00:00
|
|
|
}
|