9 cfg = config.virtualisation.lxd.agent;
11 # the lxd agent is provided by the lxd daemon through a virtiofs or 9p mount
12 # this is a port of the distrobuilder lxd-agent generator
13 # https://github.com/lxc/distrobuilder/blob/f77300bf7d7d5707b08eaf8a434d647d1ba81b5d/generators/lxd-agent.go#L18-L55
15 PREFIX="/run/lxd_agent"
18 mount -t virtiofs config "$PREFIX/.mnt" >/dev/null 2>&1
22 modprobe 9pnet_virtio >/dev/null 2>&1 || true
23 mount -t 9p config "$PREFIX/.mnt" -o access=0,trans=virtio,size=1048576 >/dev/null 2>&1
27 umount -l "$PREFIX" >/dev/null 2>&1 || true
28 rmdir "$PREFIX" >/dev/null 2>&1 || true
33 # Setup the mount target.
34 umount -l "$PREFIX" >/dev/null 2>&1 || true
36 mount -t tmpfs tmpfs "$PREFIX" -o mode=0700,size=50M
37 mkdir -p "$PREFIX/.mnt"
40 mount_virtiofs || mount_9p || fail "Couldn't mount virtiofs or 9p, failing."
43 cp -Ra "$PREFIX/.mnt/"* "$PREFIX"
45 # Unmount the temporary mount.
50 chown -R root:root "$PREFIX"
55 virtualisation.lxd.agent.enable = lib.mkEnableOption "LXD agent";
58 config = lib.mkIf cfg.enable {
59 # https://github.com/lxc/distrobuilder/blob/f77300bf7d7d5707b08eaf8a434d647d1ba81b5d/generators/lxd-agent.go#L108-L125
60 systemd.services.lxd-agent = {
62 wantedBy = [ "multi-user.target" ];
65 ++ lib.optionals config.services.cloud-init.enable [
68 "cloud-init-local.service"
70 conflicts = [ "shutdown.target" ];
75 # allow `incus exec` to find system binaries
76 "/run/current-system/sw"
79 preStart = preStartScript;
81 # avoid killing nixos-rebuild switch when executed through lxc exec
82 restartIfChanged = false;
83 stopIfChanged = false;
86 Description = "LXD - agent";
87 Documentation = "https://documentation.ubuntu.com/lxd/en/latest";
88 ConditionPathExists = "/dev/virtio-ports/org.linuxcontainers.lxd";
89 DefaultDependencies = "no";
90 StartLimitInterval = "60";
91 StartLimitBurst = "10";
96 WorkingDirectory = "-/run/lxd_agent";
97 ExecStart = "/run/lxd_agent/lxd-agent";
98 Restart = "on-failure";
103 systemd.paths.lxd-agent = {
105 wantedBy = [ "multi-user.target" ];
106 pathConfig.PathExists = "/dev/virtio-ports/org.linuxcontainers.lxd";