2011-10-11 13:06:59 +00:00
|
|
|
# Test Nix's remote build feature.
|
|
|
|
|
2019-10-04 08:45:33 +00:00
|
|
|
{ nixpkgs, system, overlay }:
|
2011-10-11 13:06:59 +00:00
|
|
|
|
2020-10-17 20:08:18 +00:00
|
|
|
with import (nixpkgs + "/nixos/lib/testing-python.nix") {
|
2019-10-04 08:45:33 +00:00
|
|
|
inherit system;
|
|
|
|
extraConfigurations = [ { nixpkgs.overlays = [ overlay ]; } ];
|
|
|
|
};
|
2011-10-11 13:06:59 +00:00
|
|
|
|
2014-04-15 13:32:27 +00:00
|
|
|
makeTest (
|
2011-10-11 13:06:59 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2018-09-08 02:10:56 +00:00
|
|
|
# The configuration of the remote builders.
|
|
|
|
builder =
|
2011-10-11 13:06:59 +00:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
{ services.openssh.enable = true;
|
|
|
|
virtualisation.writableStore = true;
|
2017-02-21 14:21:56 +00:00
|
|
|
nix.useSandbox = true;
|
2011-10-11 13:06:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# 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";
|
2014-12-11 08:58:29 +00:00
|
|
|
args = [ "-c" "if [ ${toString nr} = 5 ]; then echo FAIL; exit 1; fi; echo Hello; mkdir $out $foo; cat /proc/sys/kernel/hostname > $out/host; ln -s $out $foo/bar; sleep 10" ];
|
2014-07-14 10:19:27 +00:00
|
|
|
outputs = [ "out" "foo" ];
|
2011-10-11 13:06:59 +00:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2020-10-17 21:34:38 +00:00
|
|
|
name = "remote-builds";
|
2011-10-11 13:06:59 +00:00
|
|
|
|
|
|
|
nodes =
|
2018-09-08 02:10:56 +00:00
|
|
|
{ builder1 = builder;
|
|
|
|
builder2 = builder;
|
2011-10-11 13:06:59 +00:00
|
|
|
|
|
|
|
client =
|
2019-11-05 10:12:25 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2011-10-11 13:06:59 +00:00
|
|
|
{ nix.maxJobs = 0; # force remote building
|
|
|
|
nix.distributedBuilds = true;
|
|
|
|
nix.buildMachines =
|
2018-09-08 02:10:56 +00:00
|
|
|
[ { hostName = "builder1";
|
2011-10-11 13:06:59 +00:00
|
|
|
sshUser = "root";
|
2018-02-08 12:46:23 +00:00
|
|
|
sshKey = "/root/.ssh/id_ed25519";
|
2011-10-11 13:06:59 +00:00
|
|
|
system = "i686-linux";
|
|
|
|
maxJobs = 1;
|
|
|
|
}
|
2018-09-08 02:10:56 +00:00
|
|
|
{ hostName = "builder2";
|
2011-10-11 13:06:59 +00:00
|
|
|
sshUser = "root";
|
2018-02-08 12:46:23 +00:00
|
|
|
sshKey = "/root/.ssh/id_ed25519";
|
2011-10-11 13:06:59 +00:00
|
|
|
system = "i686-linux";
|
|
|
|
maxJobs = 1;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
virtualisation.writableStore = true;
|
|
|
|
virtualisation.pathsInNixDB = [ config.system.build.extraUtils ];
|
2019-11-05 10:12:25 +00:00
|
|
|
nix.binaryCaches = lib.mkForce [ ];
|
2014-09-05 09:49:07 +00:00
|
|
|
programs.ssh.extraConfig = "ConnectTimeout 30";
|
2011-10-11 13:06:59 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-10-17 20:08:18 +00:00
|
|
|
testScript = { nodes }: ''
|
|
|
|
# fmt: off
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
start_all()
|
|
|
|
|
|
|
|
# Create an SSH key on the client.
|
|
|
|
subprocess.run([
|
|
|
|
"${pkgs.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")
|
|
|
|
|
|
|
|
# Install the SSH key on the builders.
|
|
|
|
client.wait_for_unit("network.target")
|
|
|
|
for builder in [builder1, builder2]:
|
|
|
|
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'")
|
|
|
|
|
|
|
|
# Perform a build and check that it was performed on the builder.
|
|
|
|
out = client.succeed(
|
|
|
|
"nix-build ${expr nodes.client.config 1} 2> build-output",
|
|
|
|
"grep -q Hello build-output"
|
|
|
|
)
|
|
|
|
builder1.succeed(f"test -e {out}")
|
|
|
|
|
|
|
|
# And a parallel build.
|
|
|
|
paths = client.succeed(r'nix-store -r $(nix-instantiate ${expr nodes.client.config 2})\!out $(nix-instantiate ${expr nodes.client.config 3})\!out')
|
|
|
|
out1, out2 = paths.split()
|
|
|
|
builder1.succeed(f"test -e {out1} -o -e {out2}")
|
|
|
|
builder2.succeed(f"test -e {out1} -o -e {out2}")
|
|
|
|
|
|
|
|
# And a failing build.
|
|
|
|
client.fail("nix-build ${expr nodes.client.config 5}")
|
|
|
|
|
|
|
|
# Test whether the build hook automatically skips unavailable builders.
|
|
|
|
builder1.block()
|
|
|
|
client.succeed("nix-build ${expr nodes.client.config 4}")
|
|
|
|
'';
|
2011-10-11 13:06:59 +00:00
|
|
|
})
|