1 { config, lib, pkgs, ... }:
4 cfg = config.virtualisation.incus;
5 preseedFormat = pkgs.formats.yaml { };
8 meta.maintainers = [ lib.maintainers.adamcstephens ];
11 virtualisation.incus = {
12 enable = lib.mkEnableOption (lib.mdDoc ''
13 incusd, a daemon that manages containers and virtual machines.
15 Users in the "incus-admin" group can interact with
16 the daemon (e.g. to start or stop containers) using the
17 {command}`incus` command line tool, among others.
20 package = lib.mkPackageOptionMD pkgs "incus" { };
22 lxcPackage = lib.mkPackageOptionMD pkgs "lxc" { };
24 preseed = lib.mkOption {
25 type = lib.types.nullOr (
26 lib.types.submodule { freeformType = preseedFormat.type; }
31 description = lib.mdDoc ''
32 Configuration for Incus preseed, see
33 <https://linuxcontainers.org/incus/docs/main/howto/initialize/#non-interactive-configuration>
36 Changes to this will be re-applied to Incus which will overwrite existing entities or create missing ones,
37 but entities will *not* be removed by preseed.
46 "ipv4.address" = "10.0.100.1/24";
74 source = "/var/lib/incus/storage-pools/default";
81 socketActivation = lib.mkEnableOption (
83 socket-activation for starting incus.service. Enabling this option
84 will stop incus.service from starting automatically on boot.
88 startTimeout = lib.mkOption {
89 type = lib.types.ints.unsigned;
92 description = lib.mdDoc ''
93 Time to wait (in seconds) for incusd to become ready to process requests.
94 If incusd does not reply within the configured time, `incus.service` will be
95 considered failed and systemd will attempt to restart it.
101 config = lib.mkIf cfg.enable {
102 # https://github.com/lxc/incus/blob/f145309929f849b9951658ad2ba3b8f10cbe69d1/doc/reference/server_settings.md
103 boot.kernel.sysctl = {
104 "fs.aio-max-nr" = lib.mkDefault 524288;
105 "fs.inotify.max_queued_events" = lib.mkDefault 1048576;
106 "fs.inotify.max_user_instances" = lib.mkOverride 1050 1048576; # override in case conflict nixos/modules/services/x11/xserver.nix
107 "fs.inotify.max_user_watches" = lib.mkOverride 1050 1048576; # override in case conflict nixos/modules/services/x11/xserver.nix
108 "kernel.dmesg_restrict" = lib.mkDefault 1;
109 "kernel.keys.maxbytes" = lib.mkDefault 2000000;
110 "kernel.keys.maxkeys" = lib.mkDefault 2000;
111 "net.core.bpf_jit_limit" = lib.mkDefault 1000000000;
112 "net.ipv4.neigh.default.gc_thresh3" = lib.mkDefault 8192;
113 "net.ipv6.neigh.default.gc_thresh3" = lib.mkDefault 8192;
114 # vm.max_map_count is set higher in nixos/modules/config/sysctl.nix
117 boot.kernelModules = [
123 ] ++ lib.optionals (!config.networking.nftables.enable) [ "iptable_mangle" ];
125 environment.systemPackages = [ cfg.package ];
127 # Note: the following options are also declared in virtualisation.lxc, but
128 # the latter can't be simply enabled to reuse the formers, because it
129 # does a bunch of unrelated things.
130 systemd.tmpfiles.rules = [ "d /var/lib/lxc/rootfs 0755 root root -" ];
132 security.apparmor = {
133 packages = [ cfg.lxcPackage ];
135 "bin.lxc-start".profile = ''
136 include ${cfg.lxcPackage}/etc/apparmor.d/usr.bin.lxc-start
138 "lxc-containers".profile = ''
139 include ${cfg.lxcPackage}/etc/apparmor.d/lxc-containers
144 systemd.services.incus = {
145 description = "Incus Container and Virtual Machine Management Daemon";
147 wantedBy = lib.mkIf (!cfg.socketActivation) [ "multi-user.target" ];
149 "network-online.target"
151 ] ++ (lib.optional cfg.socketActivation "incus.socket");
154 ] ++ (lib.optional cfg.socketActivation "incus.socket");
156 "network-online.target"
159 path = lib.mkIf config.boot.zfs.enabled [ config.boot.zfs.package ];
162 # Override Path to the LXC template configuration directory
163 INCUS_LXC_TEMPLATE_CONFIG = "${pkgs.lxcfs}/share/lxc/config";
167 ExecStart = "${cfg.package}/bin/incusd --group incus-admin";
168 ExecStartPost = "${cfg.package}/bin/incusd waitready --timeout=${cfg.startTimeout}";
169 ExecStop = "${cfg.package}/bin/incus admin shutdown";
171 KillMode = "process"; # when stopping, leave the containers alone
173 LimitMEMLOCK = "infinity";
174 LimitNOFILE = "1048576";
175 LimitNPROC = "infinity";
176 TasksMax = "infinity";
178 Restart = "on-failure";
179 TimeoutStartSec = "${cfg.startTimeout}s";
180 TimeoutStopSec = "30s";
184 systemd.sockets.incus = lib.mkIf cfg.socketActivation {
185 description = "Incus UNIX socket";
186 wantedBy = [ "sockets.target" ];
189 ListenStream = "/var/lib/incus/unix.socket";
191 SocketGroup = "incus-admin";
192 Service = "incus.service";
196 systemd.services.incus-preseed = lib.mkIf (cfg.preseed != null) {
197 description = "Incus initialization with preseed file";
199 wantedBy = ["incus.service"];
200 after = ["incus.service"];
201 bindsTo = ["incus.service"];
202 partOf = ["incus.service"];
205 ${cfg.package}/bin/incus admin init --preseed <${
206 preseedFormat.generate "incus-preseed.yaml" cfg.preseed
212 RemainAfterExit = true;
216 users.groups.incus-admin = { };
219 # match documented default ranges https://linuxcontainers.org/incus/docs/main/userns-idmap/#allowed-ranges
234 virtualisation.lxc.lxcfs.enable = true;