2022-06-16 22:42:03 +02:00
|
|
|
{ self, config, lib, pkgs, ... }:
|
2022-05-28 00:12:18 +02:00
|
|
|
{
|
2022-06-16 21:18:04 +02:00
|
|
|
options = with lib; {
|
|
|
|
c3d2.deployment.microvmBaseZfsDataset = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Base ZFS dataset whereunder to create shares for MicroVMs.";
|
|
|
|
};
|
2022-05-28 00:12:18 +02:00
|
|
|
};
|
2022-05-31 20:36:53 +02:00
|
|
|
|
2022-06-16 21:18:04 +02:00
|
|
|
config = {
|
2022-06-16 22:42:03 +02:00
|
|
|
microvm.autostart =
|
|
|
|
# static list of microvms from other sources
|
|
|
|
[ "staging-data-hoarder" ]
|
|
|
|
++
|
|
|
|
# just all the microvms from this flake
|
|
|
|
# that are supposed to run on the server
|
|
|
|
builtins.filter (name:
|
|
|
|
self.nixosConfigurations.${name}
|
|
|
|
.config ? c3d2.deployment.server
|
|
|
|
&& self.nixosConfigurations.${name}
|
|
|
|
.config.c3d2.deployment.server
|
|
|
|
== config.networking.hostName
|
|
|
|
) (builtins.attrNames self.nixosConfigurations);
|
2022-06-16 21:18:04 +02:00
|
|
|
|
|
|
|
systemd.services."microvm-virtiofsd@" = {
|
|
|
|
requires = [ "microvm-zfs-datasets@%i.service" ];
|
|
|
|
};
|
|
|
|
systemd.services."microvm-zfs-datasets@" = {
|
|
|
|
description = "Create ZFS datasets for MicroVM '%i'";
|
|
|
|
before = [ "microvm-virtiofsd@%i.service" ];
|
|
|
|
after = [ "local-fs.target" ];
|
|
|
|
partOf = [ "microvm@%i.service" ];
|
|
|
|
unitConfig.ConditionPathExists = "/var/lib/microvms/%i/current/share/microvm/virtiofs";
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
WorkingDirectory = "/var/lib/microvms/%i";
|
|
|
|
SyslogIdentifier = "microvm-zfs-datasets@%i";
|
|
|
|
};
|
|
|
|
path = with pkgs; [ zfs ];
|
|
|
|
scriptArgs = "%i";
|
|
|
|
script = ''
|
|
|
|
zfsExists() {
|
|
|
|
zfs list $1 >/dev/null 2>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
NAME="$1"
|
|
|
|
BASE="${config.c3d2.deployment.microvmBaseZfsDataset}"
|
|
|
|
zfsExists $BASE || \
|
|
|
|
zfs create $BASE
|
|
|
|
zfsExists $BASE/$NAME || \
|
|
|
|
zfs create $BASE/$NAME
|
|
|
|
for d in current/share/microvm/virtiofs/*; do
|
|
|
|
SOURCE=$(cat $d/source)
|
|
|
|
TAG=$(basename $d)
|
2022-06-16 22:18:04 +02:00
|
|
|
MNT=$SOURCE
|
|
|
|
if [[ "$MNT" == /var/lib/microvms/$NAME/* ]]; then
|
|
|
|
zfsExists $BASE/$NAME/$TAG || \
|
|
|
|
zfs create -o mountpoint=$MNT $BASE/$NAME/$TAG
|
|
|
|
fi
|
2022-06-16 21:18:04 +02:00
|
|
|
done
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
nix = {
|
|
|
|
# fetch github-prebuilt microvm-kernels
|
|
|
|
binaryCaches = [
|
|
|
|
"https://microvm.cachix.org"
|
|
|
|
];
|
|
|
|
binaryCachePublicKeys = [
|
|
|
|
"microvm.cachix.org-1:oXnBc6hRE3eX5rSYdRyMYXnfzcCxC7yKPTbZXALsqys="
|
|
|
|
];
|
|
|
|
};
|
2022-05-31 20:36:53 +02:00
|
|
|
};
|
2022-05-28 00:12:18 +02:00
|
|
|
}
|