nixpkgs/nixos/modules/virtualisation/lxd.nix

140 lines
3.6 KiB
Nix
Raw Normal View History

2015-09-14 08:27:31 +02:00
# Systemd services for lxd.
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.virtualisation.lxd;
zfsCfg = config.boot.zfs;
2015-09-14 08:27:31 +02:00
in
{
###### interface
options = {
2018-03-16 10:58:54 +01:00
virtualisation.lxd = {
enable = mkOption {
2015-09-14 08:27:31 +02:00
type = types.bool;
default = false;
2018-03-16 10:58:54 +01:00
description = ''
This option enables lxd, a daemon that manages
containers. Users in the "lxd" group can interact with
the daemon (e.g. to start or stop containers) using the
<command>lxc</command> command line tool, among others.
'';
2015-09-14 08:27:31 +02:00
};
package = mkOption {
type = types.package;
default = pkgs.lxd;
defaultText = "pkgs.lxd";
description = ''
The LXD package to use.
'';
};
lxcPackage = mkOption {
type = types.package;
default = pkgs.lxc;
defaultText = "pkgs.lxc";
description = ''
The LXC package to use with LXD (required for AppArmor profiles).
'';
};
zfsPackage = mkOption {
type = types.package;
default = with pkgs; if zfsCfg.enableUnstable then zfsUnstable else zfs;
defaultText = "pkgs.zfs";
description = ''
The ZFS package to use with LXD.
'';
};
2018-03-16 10:58:54 +01:00
zfsSupport = mkOption {
type = types.bool;
default = false;
description = ''
Enables lxd to use zfs as a storage for containers.
2018-03-16 10:58:54 +01:00
This option is enabled by default if a zfs pool is configured
with nixos.
'';
};
recommendedSysctlSettings = mkOption {
type = types.bool;
default = false;
description = ''
enables various settings to avoid common pitfalls when
running containers requiring many file operations.
Fixes errors like "Too many open files" or
"neighbour: ndisc_cache: neighbor table overflow!".
See https://lxd.readthedocs.io/en/latest/production-setup/
for details.
'';
};
2018-03-16 10:58:54 +01:00
};
2015-09-14 08:27:31 +02:00
};
###### implementation
config = mkIf cfg.enable {
environment.systemPackages = [ cfg.package ];
2015-09-14 08:27:31 +02:00
2018-02-10 09:18:53 +01:00
security.apparmor = {
enable = true;
profiles = [
"${cfg.lxcPackage}/etc/apparmor.d/usr.bin.lxc-start"
"${cfg.lxcPackage}/etc/apparmor.d/lxc-containers"
2018-02-10 09:18:53 +01:00
];
packages = [ cfg.lxcPackage ];
2018-02-10 09:18:53 +01:00
};
2018-03-16 10:58:54 +01:00
systemd.services.lxd = {
description = "LXD Container Management Daemon";
2015-09-14 08:27:31 +02:00
2018-03-16 10:58:54 +01:00
wantedBy = [ "multi-user.target" ];
after = [ "systemd-udev-settle.service" ];
2015-09-14 08:27:31 +02:00
path = lib.optional cfg.zfsSupport cfg.zfsPackage;
2015-09-14 08:27:31 +02:00
2018-03-16 10:58:54 +01:00
preStart = ''
mkdir -m 0755 -p /var/lib/lxc/rootfs
'';
2018-02-10 09:18:53 +01:00
2018-03-16 10:58:54 +01:00
serviceConfig = {
ExecStart = "@${cfg.package.bin}/bin/lxd lxd --group lxd";
2018-03-16 10:58:54 +01:00
Type = "simple";
KillMode = "process"; # when stopping, leave the containers alone
LimitMEMLOCK = "infinity";
LimitNOFILE = "1048576";
LimitNPROC = "infinity";
TasksMax = "infinity";
2015-09-14 08:27:31 +02:00
};
2018-03-16 10:58:54 +01:00
};
users.groups.lxd.gid = config.ids.gids.lxd;
2015-09-14 08:27:31 +02:00
users.users.root = {
2015-09-14 08:27:31 +02:00
subUidRanges = [ { startUid = 1000000; count = 65536; } ];
subGidRanges = [ { startGid = 1000000; count = 65536; } ];
};
boot.kernel.sysctl = mkIf cfg.recommendedSysctlSettings {
"fs.inotify.max_queued_events" = 1048576;
"fs.inotify.max_user_instances" = 1048576;
"fs.inotify.max_user_watches" = 1048576;
"vm.max_map_count" = 262144;
"kernel.dmesg_restrict" = 1;
"net.ipv4.neigh.default.gc_thresh3" = 8192;
"net.ipv6.neigh.default.gc_thresh3" = 8192;
"kernel.keys.maxkeys" = 2000;
};
2015-09-14 08:27:31 +02:00
};
}