2015-09-14 07:27:31 +01:00
|
|
|
# Systemd services for lxd.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.virtualisation.lxd;
|
2019-11-19 04:08:49 +00:00
|
|
|
zfsCfg = config.boot.zfs;
|
2015-09-14 07:27:31 +01:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2018-03-16 09:58:54 +00:00
|
|
|
virtualisation.lxd = {
|
|
|
|
enable = mkOption {
|
2015-09-14 07:27:31 +01:00
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2018-03-16 09:58:54 +00: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.
|
2020-06-05 13:40:02 +01:00
|
|
|
|
|
|
|
Most of the time, you'll also want to start lxcfs, so
|
|
|
|
that containers can "see" the limits:
|
|
|
|
<code>
|
|
|
|
virtualisation.lxc.lxcfs.enable = true;
|
|
|
|
</code>
|
2018-03-16 09:58:54 +00:00
|
|
|
'';
|
2015-09-14 07:27:31 +01:00
|
|
|
};
|
2019-11-19 04:08:49 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
2020-06-05 11:57:18 +01:00
|
|
|
default = pkgs.lxd.override { nftablesSupport = config.networking.nftables.enable; };
|
2020-01-30 14:26:54 +00:00
|
|
|
defaultText = "pkgs.lxd";
|
2019-11-19 04:08:49 +00:00
|
|
|
description = ''
|
|
|
|
The LXD package to use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
lxcPackage = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.lxc;
|
2020-01-30 14:26:54 +00:00
|
|
|
defaultText = "pkgs.lxc";
|
2019-11-19 04:08:49 +00:00
|
|
|
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 09:58:54 +00:00
|
|
|
zfsSupport = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
2019-11-19 04:08:49 +00:00
|
|
|
Enables lxd to use zfs as a storage for containers.
|
|
|
|
|
2018-03-16 09:58:54 +00:00
|
|
|
This option is enabled by default if a zfs pool is configured
|
|
|
|
with nixos.
|
|
|
|
'';
|
|
|
|
};
|
2020-06-05 11:57:18 +01:00
|
|
|
|
2019-12-14 14:29:08 +00:00
|
|
|
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 09:58:54 +00:00
|
|
|
};
|
2015-09-14 07:27:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2019-11-19 04:08:49 +00:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2015-09-14 07:27:31 +01:00
|
|
|
|
2018-02-10 08:18:53 +00:00
|
|
|
security.apparmor = {
|
|
|
|
enable = true;
|
2019-11-19 04:08:49 +00:00
|
|
|
packages = [ cfg.lxcPackage ];
|
2020-08-08 01:01:35 +01:00
|
|
|
policies = {
|
|
|
|
"bin.lxc-start".profile = ''
|
|
|
|
include ${cfg.lxcPackage}/etc/apparmor.d/usr.bin.lxc-start
|
|
|
|
'';
|
|
|
|
"lxc-containers".profile = ''
|
|
|
|
include ${cfg.lxcPackage}/etc/apparmor.d/lxc-containers
|
|
|
|
'';
|
|
|
|
};
|
2018-02-10 08:18:53 +00:00
|
|
|
};
|
|
|
|
|
2018-03-16 09:58:54 +00:00
|
|
|
systemd.services.lxd = {
|
|
|
|
description = "LXD Container Management Daemon";
|
2015-09-14 07:27:31 +01:00
|
|
|
|
2018-03-16 09:58:54 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "systemd-udev-settle.service" ];
|
2015-09-14 07:27:31 +01:00
|
|
|
|
2019-11-19 04:08:49 +00:00
|
|
|
path = lib.optional cfg.zfsSupport cfg.zfsPackage;
|
2015-09-14 07:27:31 +01:00
|
|
|
|
2018-03-16 09:58:54 +00:00
|
|
|
preStart = ''
|
|
|
|
mkdir -m 0755 -p /var/lib/lxc/rootfs
|
|
|
|
'';
|
2018-02-10 08:18:53 +00:00
|
|
|
|
2018-03-16 09:58:54 +00:00
|
|
|
serviceConfig = {
|
2020-04-28 02:50:34 +01:00
|
|
|
ExecStart = "@${cfg.package}/bin/lxd lxd --group lxd";
|
2018-03-16 09:58:54 +00:00
|
|
|
Type = "simple";
|
|
|
|
KillMode = "process"; # when stopping, leave the containers alone
|
2019-12-14 14:29:08 +00:00
|
|
|
LimitMEMLOCK = "infinity";
|
|
|
|
LimitNOFILE = "1048576";
|
|
|
|
LimitNPROC = "infinity";
|
|
|
|
TasksMax = "infinity";
|
2020-06-05 13:40:02 +01:00
|
|
|
|
|
|
|
# By default, `lxd` loads configuration files from hard-coded
|
|
|
|
# `/usr/share/lxc/config` - since this is a no-go for us, we have to
|
|
|
|
# explicitly tell it where the actual configuration files are
|
|
|
|
Environment = mkIf (config.virtualisation.lxc.lxcfs.enable)
|
|
|
|
"LXD_LXC_TEMPLATE_CONFIG=${pkgs.lxcfs}/share/lxc/config";
|
2015-09-14 07:27:31 +01:00
|
|
|
};
|
2018-03-16 09:58:54 +00:00
|
|
|
};
|
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.groups.lxd.gid = config.ids.gids.lxd;
|
2015-09-14 07:27:31 +01:00
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.users.root = {
|
2015-09-14 07:27:31 +01:00
|
|
|
subUidRanges = [ { startUid = 1000000; count = 65536; } ];
|
|
|
|
subGidRanges = [ { startGid = 1000000; count = 65536; } ];
|
|
|
|
};
|
2019-12-14 14:29:08 +00:00
|
|
|
|
|
|
|
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 07:27:31 +01:00
|
|
|
};
|
|
|
|
}
|