nixpkgs/nixos/modules/virtualisation/lxd-agent.nix
Silvan Mosberger 4f0dadbf38 treewide: format all inactive Nix files
After final improvements to the official formatter implementation,
this commit now performs the first treewide reformat of Nix files using it.
This is part of the implementation of RFC 166.

Only "inactive" files are reformatted, meaning only files that
aren't being touched by any PR with activity in the past 2 months.
This is to avoid conflicts for PRs that might soon be merged.
Later we can do a full treewide reformat to get the rest,
which should not cause as many conflicts.

A CI check has already been running for some time to ensure that new and
already-formatted files are formatted, so the files being reformatted here
should also stay formatted.

This commit was automatically created and can be verified using

    nix-build a08b3a4d19.tar.gz \
      --argstr baseRev b32a094368
    result/bin/apply-formatting $NIXPKGS_PATH
2024-12-10 20:26:33 +01:00

109 lines
2.9 KiB
Nix

{
config,
lib,
pkgs,
...
}:
let
cfg = config.virtualisation.lxd.agent;
# the lxd agent is provided by the lxd daemon through a virtiofs or 9p mount
# this is a port of the distrobuilder lxd-agent generator
# https://github.com/lxc/distrobuilder/blob/f77300bf7d7d5707b08eaf8a434d647d1ba81b5d/generators/lxd-agent.go#L18-L55
preStartScript = ''
PREFIX="/run/lxd_agent"
mount_virtiofs() {
mount -t virtiofs config "$PREFIX/.mnt" >/dev/null 2>&1
}
mount_9p() {
modprobe 9pnet_virtio >/dev/null 2>&1 || true
mount -t 9p config "$PREFIX/.mnt" -o access=0,trans=virtio,size=1048576 >/dev/null 2>&1
}
fail() {
umount -l "$PREFIX" >/dev/null 2>&1 || true
rmdir "$PREFIX" >/dev/null 2>&1 || true
echo "$1"
exit 1
}
# Setup the mount target.
umount -l "$PREFIX" >/dev/null 2>&1 || true
mkdir -p "$PREFIX"
mount -t tmpfs tmpfs "$PREFIX" -o mode=0700,size=50M
mkdir -p "$PREFIX/.mnt"
# Try virtiofs first.
mount_virtiofs || mount_9p || fail "Couldn't mount virtiofs or 9p, failing."
# Copy the data.
cp -Ra "$PREFIX/.mnt/"* "$PREFIX"
# Unmount the temporary mount.
umount "$PREFIX/.mnt"
rmdir "$PREFIX/.mnt"
# Fix up permissions.
chown -R root:root "$PREFIX"
'';
in
{
options = {
virtualisation.lxd.agent.enable = lib.mkEnableOption "LXD agent";
};
config = lib.mkIf cfg.enable {
# https://github.com/lxc/distrobuilder/blob/f77300bf7d7d5707b08eaf8a434d647d1ba81b5d/generators/lxd-agent.go#L108-L125
systemd.services.lxd-agent = {
enable = true;
wantedBy = [ "multi-user.target" ];
before =
[ "shutdown.target" ]
++ lib.optionals config.services.cloud-init.enable [
"cloud-init.target"
"cloud-init.service"
"cloud-init-local.service"
];
conflicts = [ "shutdown.target" ];
path = [
pkgs.kmod
pkgs.util-linux
# allow `incus exec` to find system binaries
"/run/current-system/sw"
];
preStart = preStartScript;
# avoid killing nixos-rebuild switch when executed through lxc exec
restartIfChanged = false;
stopIfChanged = false;
unitConfig = {
Description = "LXD - agent";
Documentation = "https://documentation.ubuntu.com/lxd/en/latest";
ConditionPathExists = "/dev/virtio-ports/org.linuxcontainers.lxd";
DefaultDependencies = "no";
StartLimitInterval = "60";
StartLimitBurst = "10";
};
serviceConfig = {
Type = "notify";
WorkingDirectory = "-/run/lxd_agent";
ExecStart = "/run/lxd_agent/lxd-agent";
Restart = "on-failure";
RestartSec = "5s";
};
};
systemd.paths.lxd-agent = {
enable = true;
wantedBy = [ "multi-user.target" ];
pathConfig.PathExists = "/dev/virtio-ports/org.linuxcontainers.lxd";
};
};
}