1 # Systemd services for lxd.
3 { config, lib, pkgs, ... }:
9 cfg = config.virtualisation.lxd;
10 zfsCfg = config.boot.zfs;
18 virtualisation.lxd = {
23 This option enables lxd, a daemon that manages
24 containers. Users in the "lxd" group can interact with
25 the daemon (e.g. to start or stop containers) using the
26 <command>lxc</command> command line tool, among others.
28 Most of the time, you'll also want to start lxcfs, so
29 that containers can "see" the limits:
31 virtualisation.lxc.lxcfs.enable = true;
38 default = pkgs.lxd.override { nftablesSupport = config.networking.nftables.enable; };
39 defaultText = "pkgs.lxd";
41 The LXD package to use.
45 lxcPackage = mkOption {
48 defaultText = "pkgs.lxc";
50 The LXC package to use with LXD (required for AppArmor profiles).
54 zfsPackage = mkOption {
56 default = with pkgs; if zfsCfg.enableUnstable then zfsUnstable else zfs;
57 defaultText = "pkgs.zfs";
59 The ZFS package to use with LXD.
63 zfsSupport = mkOption {
67 Enables lxd to use zfs as a storage for containers.
69 This option is enabled by default if a zfs pool is configured
74 recommendedSysctlSettings = mkOption {
78 enables various settings to avoid common pitfalls when
79 running containers requiring many file operations.
80 Fixes errors like "Too many open files" or
81 "neighbour: ndisc_cache: neighbor table overflow!".
82 See https://lxd.readthedocs.io/en/latest/production-setup/
91 config = mkIf cfg.enable {
92 environment.systemPackages = [ cfg.package ];
96 packages = [ cfg.lxcPackage ];
98 "bin/lxc-start".profile = ''
99 #include ${cfg.lxcPackage}/etc/apparmor.d/usr.bin.lxc-start
101 "lxc-containers".profile = ''
102 #include ${cfg.lxcPackage}/etc/apparmor.d/lxc-containers
107 systemd.services.lxd = {
108 description = "LXD Container Management Daemon";
110 wantedBy = [ "multi-user.target" ];
111 after = [ "systemd-udev-settle.service" ];
113 path = lib.optional cfg.zfsSupport cfg.zfsPackage;
116 mkdir -m 0755 -p /var/lib/lxc/rootfs
120 ExecStart = "@${cfg.package}/bin/lxd lxd --group lxd";
122 KillMode = "process"; # when stopping, leave the containers alone
123 LimitMEMLOCK = "infinity";
124 LimitNOFILE = "1048576";
125 LimitNPROC = "infinity";
126 TasksMax = "infinity";
128 # By default, `lxd` loads configuration files from hard-coded
129 # `/usr/share/lxc/config` - since this is a no-go for us, we have to
130 # explicitly tell it where the actual configuration files are
131 Environment = mkIf (config.virtualisation.lxc.lxcfs.enable)
132 "LXD_LXC_TEMPLATE_CONFIG=${pkgs.lxcfs}/share/lxc/config";
136 users.groups.lxd.gid = config.ids.gids.lxd;
139 subUidRanges = [ { startUid = 1000000; count = 65536; } ];
140 subGidRanges = [ { startGid = 1000000; count = 65536; } ];
143 boot.kernel.sysctl = mkIf cfg.recommendedSysctlSettings {
144 "fs.inotify.max_queued_events" = 1048576;
145 "fs.inotify.max_user_instances" = 1048576;
146 "fs.inotify.max_user_watches" = 1048576;
147 "vm.max_map_count" = 262144;
148 "kernel.dmesg_restrict" = 1;
149 "net.ipv4.neigh.default.gc_thresh3" = 8192;
150 "net.ipv6.neigh.default.gc_thresh3" = 8192;
151 "kernel.keys.maxkeys" = 2000;