2023-10-12 21:59:19 +01:00
|
|
|
{ config, lib, options, ... }:
|
2022-12-20 00:07:02 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
keysDirectory = "/var/keys";
|
|
|
|
|
|
|
|
user = "builder";
|
|
|
|
|
|
|
|
keyType = "ed25519";
|
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
cfg = config.virtualisation.darwin-builder;
|
|
|
|
|
2022-12-20 00:07:02 +00:00
|
|
|
in
|
|
|
|
|
2023-01-02 15:36:41 +00:00
|
|
|
{
|
|
|
|
imports = [
|
2022-12-20 00:07:02 +00:00
|
|
|
../virtualisation/qemu-vm.nix
|
2022-12-27 11:46:26 +00:00
|
|
|
|
|
|
|
# Avoid a dependency on stateVersion
|
|
|
|
{
|
|
|
|
disabledModules = [
|
|
|
|
../virtualisation/nixos-containers.nix
|
|
|
|
../services/x11/desktop-managers/xterm.nix
|
|
|
|
];
|
2023-07-23 09:07:48 +01:00
|
|
|
# swraid's default depends on stateVersion
|
|
|
|
config.boot.swraid.enable = false;
|
2022-12-27 11:46:26 +00:00
|
|
|
options.boot.isContainer = lib.mkOption { default = false; internal = true; };
|
|
|
|
}
|
2022-12-20 00:07:02 +00:00
|
|
|
];
|
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
options.virtualisation.darwin-builder = with lib; {
|
|
|
|
diskSize = mkOption {
|
|
|
|
default = 20 * 1024;
|
|
|
|
type = types.int;
|
|
|
|
example = 30720;
|
|
|
|
description = "The maximum disk space allocated to the runner in MB";
|
2022-12-20 00:07:02 +00:00
|
|
|
};
|
2023-04-07 04:10:49 +01:00
|
|
|
memorySize = mkOption {
|
|
|
|
default = 3 * 1024;
|
|
|
|
type = types.int;
|
|
|
|
example = 8192;
|
|
|
|
description = "The runner's memory in MB";
|
|
|
|
};
|
|
|
|
min-free = mkOption {
|
|
|
|
default = 1024 * 1024 * 1024;
|
|
|
|
type = types.int;
|
|
|
|
example = 1073741824;
|
|
|
|
description = ''
|
|
|
|
The threshold (in bytes) of free disk space left at which to
|
|
|
|
start garbage collection on the runner
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
max-free = mkOption {
|
|
|
|
default = 3 * 1024 * 1024 * 1024;
|
|
|
|
type = types.int;
|
|
|
|
example = 3221225472;
|
|
|
|
description = ''
|
|
|
|
The threshold (in bytes) of free disk space left at which to
|
|
|
|
stop garbage collection on the runner
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
workingDirectory = mkOption {
|
|
|
|
default = ".";
|
|
|
|
type = types.str;
|
|
|
|
example = "/var/lib/darwin-builder";
|
|
|
|
description = ''
|
|
|
|
The working directory to use to run the script. When running
|
|
|
|
as part of a flake will need to be set to a non read-only filesystem.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
hostPort = mkOption {
|
2023-06-07 03:39:27 +01:00
|
|
|
default = 31022;
|
2023-04-07 04:10:49 +01:00
|
|
|
type = types.int;
|
2023-06-07 03:39:27 +01:00
|
|
|
example = 22;
|
2023-04-07 04:10:49 +01:00
|
|
|
description = ''
|
|
|
|
The localhost host port to forward TCP to the guest port.
|
|
|
|
'';
|
2022-12-20 00:07:02 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
config = {
|
|
|
|
# The builder is not intended to be used interactively
|
|
|
|
documentation.enable = false;
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
environment.etc = {
|
|
|
|
"ssh/ssh_host_ed25519_key" = {
|
|
|
|
mode = "0600";
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
source = ./keys/ssh_host_ed25519_key;
|
|
|
|
};
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
"ssh/ssh_host_ed25519_key.pub" = {
|
|
|
|
mode = "0644";
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
source = ./keys/ssh_host_ed25519_key.pub;
|
|
|
|
};
|
|
|
|
};
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
# DNS fails for QEMU user networking (SLiRP) on macOS. See:
|
|
|
|
#
|
|
|
|
# https://github.com/utmapp/UTM/issues/2353
|
|
|
|
#
|
|
|
|
# This works around that by using a public DNS server other than the DNS
|
|
|
|
# server that QEMU provides (normally 10.0.2.3)
|
|
|
|
networking.nameservers = [ "8.8.8.8" ];
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-11-19 19:56:15 +00:00
|
|
|
# The linux builder is a lightweight VM for remote building; not evaluation.
|
|
|
|
nix.channel.enable = false;
|
|
|
|
# remote builder uses `nix-daemon` (ssh-ng:) or `nix-store --serve` (ssh:)
|
|
|
|
# --force: do not complain when missing
|
|
|
|
# TODO: install a store-only nix
|
|
|
|
# https://github.com/NixOS/rfcs/blob/master/rfcs/0134-nix-store-layer.md#detailed-design
|
|
|
|
environment.extraSetup = ''
|
|
|
|
rm --force $out/bin/{nix-instantiate,nix-build,nix-shell,nix-prefetch*,nix}
|
|
|
|
'';
|
2023-11-19 20:24:27 +00:00
|
|
|
# Deployment is by image.
|
|
|
|
# TODO system.switch.enable = false;?
|
|
|
|
system.disableInstallerTools = true;
|
2023-11-19 19:56:15 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
nix.settings = {
|
|
|
|
auto-optimise-store = true;
|
2023-01-04 05:12:56 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
min-free = cfg.min-free;
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
max-free = cfg.max-free;
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2024-06-09 21:31:44 +01:00
|
|
|
trusted-users = [ user ];
|
2023-04-07 04:10:49 +01:00
|
|
|
};
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
services = {
|
|
|
|
getty.autologinUser = user;
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
openssh = {
|
|
|
|
enable = true;
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
authorizedKeysFiles = [ "${keysDirectory}/%u_${keyType}.pub" ];
|
2023-01-02 15:36:41 +00:00
|
|
|
};
|
2023-04-07 04:10:49 +01:00
|
|
|
};
|
2022-12-27 02:25:56 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
system.build.macos-builder-installer =
|
|
|
|
let
|
|
|
|
privateKey = "/etc/nix/${user}_${keyType}";
|
|
|
|
|
|
|
|
publicKey = "${privateKey}.pub";
|
|
|
|
|
|
|
|
# This installCredentials script is written so that it's as easy as
|
|
|
|
# possible for a user to audit before confirming the `sudo`
|
|
|
|
installCredentials = hostPkgs.writeShellScript "install-credentials" ''
|
2024-03-06 21:03:51 +00:00
|
|
|
set -euo pipefail
|
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
KEYS="''${1}"
|
|
|
|
INSTALL=${hostPkgs.coreutils}/bin/install
|
|
|
|
"''${INSTALL}" -g nixbld -m 600 "''${KEYS}/${user}_${keyType}" ${privateKey}
|
|
|
|
"''${INSTALL}" -g nixbld -m 644 "''${KEYS}/${user}_${keyType}.pub" ${publicKey}
|
|
|
|
'';
|
|
|
|
|
|
|
|
hostPkgs = config.virtualisation.host.pkgs;
|
|
|
|
|
2023-06-07 03:39:27 +01:00
|
|
|
script = hostPkgs.writeShellScriptBin "create-builder" (
|
2024-03-06 21:03:51 +00:00
|
|
|
''
|
|
|
|
set -euo pipefail
|
|
|
|
'' +
|
2023-04-07 04:10:49 +01:00
|
|
|
# When running as non-interactively as part of a DarwinConfiguration the working directory
|
|
|
|
# must be set to a writeable directory.
|
|
|
|
(if cfg.workingDirectory != "." then ''
|
|
|
|
${hostPkgs.coreutils}/bin/mkdir --parent "${cfg.workingDirectory}"
|
|
|
|
cd "${cfg.workingDirectory}"
|
2023-06-07 03:39:27 +01:00
|
|
|
'' else "") + ''
|
2023-04-07 04:10:49 +01:00
|
|
|
KEYS="''${KEYS:-./keys}"
|
|
|
|
${hostPkgs.coreutils}/bin/mkdir --parent "''${KEYS}"
|
|
|
|
PRIVATE_KEY="''${KEYS}/${user}_${keyType}"
|
|
|
|
PUBLIC_KEY="''${PRIVATE_KEY}.pub"
|
|
|
|
if [ ! -e "''${PRIVATE_KEY}" ] || [ ! -e "''${PUBLIC_KEY}" ]; then
|
|
|
|
${hostPkgs.coreutils}/bin/rm --force -- "''${PRIVATE_KEY}" "''${PUBLIC_KEY}"
|
|
|
|
${hostPkgs.openssh}/bin/ssh-keygen -q -f "''${PRIVATE_KEY}" -t ${keyType} -N "" -C 'builder@localhost'
|
|
|
|
fi
|
|
|
|
if ! ${hostPkgs.diffutils}/bin/cmp "''${PUBLIC_KEY}" ${publicKey}; then
|
|
|
|
(set -x; sudo --reset-timestamp ${installCredentials} "''${KEYS}")
|
|
|
|
fi
|
2023-06-14 00:16:34 +01:00
|
|
|
KEYS="$(${hostPkgs.nix}/bin/nix-store --add "$KEYS")" ${lib.getExe config.system.build.vm}
|
2023-04-07 04:10:49 +01:00
|
|
|
'');
|
|
|
|
|
|
|
|
in
|
|
|
|
script.overrideAttrs (old: {
|
2023-10-12 21:58:50 +01:00
|
|
|
pos = __curPos; # sets meta.position to point here; see script binding above for package definition
|
2023-04-07 04:10:49 +01:00
|
|
|
meta = (old.meta or { }) // {
|
|
|
|
platforms = lib.platforms.darwin;
|
|
|
|
};
|
2023-10-12 21:59:19 +01:00
|
|
|
passthru = (old.passthru or { }) // {
|
|
|
|
# Let users in the repl inspect the config
|
|
|
|
nixosConfig = config;
|
|
|
|
nixosOptions = options;
|
|
|
|
};
|
2023-04-07 04:10:49 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
system = {
|
|
|
|
# To prevent gratuitous rebuilds on each change to Nixpkgs
|
|
|
|
nixos.revision = null;
|
|
|
|
|
|
|
|
stateVersion = lib.mkDefault (throw ''
|
|
|
|
The macOS linux builder should not need a stateVersion to be set, but a module
|
|
|
|
has accessed stateVersion nonetheless.
|
|
|
|
Please inspect the trace of the following command to figure out which module
|
|
|
|
has a dependency on stateVersion.
|
|
|
|
|
2023-07-04 11:46:42 +01:00
|
|
|
nix-instantiate --attr darwin.linux-builder --show-trace
|
2023-04-07 04:10:49 +01:00
|
|
|
'');
|
|
|
|
};
|
2022-12-27 11:46:26 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
users.users."${user}" = {
|
|
|
|
isNormalUser = true;
|
|
|
|
};
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
security.polkit.enable = true;
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
security.polkit.extraConfig = ''
|
|
|
|
polkit.addRule(function(action, subject) {
|
|
|
|
if (action.id === "org.freedesktop.login1.power-off" && subject.user === "${user}") {
|
|
|
|
return "yes";
|
|
|
|
} else {
|
|
|
|
return "no";
|
|
|
|
}
|
|
|
|
})
|
|
|
|
'';
|
2023-01-02 15:36:41 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
virtualisation = {
|
|
|
|
diskSize = cfg.diskSize;
|
2023-01-02 15:36:41 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
memorySize = cfg.memorySize;
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
forwardPorts = [
|
|
|
|
{ from = "host"; guest.port = 22; host.port = cfg.hostPort; }
|
|
|
|
];
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
# Disable graphics for the builder since users will likely want to run it
|
|
|
|
# non-interactively in the background.
|
|
|
|
graphics = false;
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
sharedDirectories.keys = {
|
|
|
|
source = "\"$KEYS\"";
|
|
|
|
target = keysDirectory;
|
|
|
|
};
|
2022-12-20 00:07:02 +00:00
|
|
|
|
2023-04-07 04:10:49 +01:00
|
|
|
# If we don't enable this option then the host will fail to delegate builds
|
|
|
|
# to the guest, because:
|
|
|
|
#
|
|
|
|
# - The host will lock the path to build
|
|
|
|
# - The host will delegate the build to the guest
|
|
|
|
# - The guest will attempt to lock the same path and fail because
|
|
|
|
# the lockfile on the host is visible on the guest
|
|
|
|
#
|
|
|
|
# Snapshotting the host's /nix/store as an image isolates the guest VM's
|
|
|
|
# /nix/store from the host's /nix/store, preventing this problem.
|
|
|
|
useNixStoreImage = true;
|
|
|
|
|
|
|
|
# Obviously the /nix/store needs to be writable on the guest in order for it
|
|
|
|
# to perform builds.
|
|
|
|
writableStore = true;
|
|
|
|
|
|
|
|
# This ensures that anything built on the guest isn't lost when the guest is
|
|
|
|
# restarted.
|
|
|
|
writableStoreUseTmpfs = false;
|
2023-06-07 03:41:59 +01:00
|
|
|
|
|
|
|
# Pass certificates from host to the guest otherwise when custom CA certificates
|
|
|
|
# are required we can't use the cached builder.
|
|
|
|
useHostCerts = true;
|
2022-12-20 00:07:02 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|