2020-04-24 23:36:52 +00:00
|
|
|
# Systemd services for lxd.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.virtualisation.lxd;
|
2021-02-05 17:12:51 +00:00
|
|
|
in {
|
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "virtualisation" "lxd" "zfsPackage" ] "Override zfs in an overlay instead to override it globally")
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
virtualisation.lxd = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
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-18 07:06:33 +00: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>
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
2021-12-06 16:07:01 +00:00
|
|
|
default = pkgs.lxd;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "pkgs.lxd";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The LXD package to use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
lxcPackage = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.lxc;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "pkgs.lxc";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The LXC package to use with LXD (required for AppArmor profiles).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
zfsSupport = mkOption {
|
|
|
|
type = types.bool;
|
2021-02-05 17:12:51 +00:00
|
|
|
default = config.boot.zfs.enabled;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "config.boot.zfs.enabled";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Enables lxd to use zfs as a storage for containers.
|
|
|
|
|
|
|
|
This option is enabled by default if a zfs pool is configured
|
|
|
|
with nixos.
|
|
|
|
'';
|
|
|
|
};
|
2020-06-18 07:06:33 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
recommendedSysctlSettings = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-03-09 03:18:52 +00:00
|
|
|
Enables various settings to avoid common pitfalls when
|
2020-04-24 23:36:52 +00:00
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
2021-03-09 03:18:52 +00:00
|
|
|
|
|
|
|
startTimeout = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 600;
|
|
|
|
apply = toString;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-03-09 03:18:52 +00:00
|
|
|
Time to wait (in seconds) for LXD to become ready to process requests.
|
|
|
|
If LXD does not reply within the configured time, lxd.service will be
|
|
|
|
considered failed and systemd will attempt to restart it.
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
|
2021-03-09 03:18:52 +00:00
|
|
|
# Note: the following options are also declared in virtualisation.lxc, but
|
|
|
|
# the latter can't be simply enabled to reuse the formers, because it
|
|
|
|
# does a bunch of unrelated things.
|
|
|
|
systemd.tmpfiles.rules = [ "d /var/lib/lxc/rootfs 0755 root root -" ];
|
|
|
|
|
2021-05-20 23:08:51 +00:00
|
|
|
security.apparmor = {
|
|
|
|
packages = [ cfg.lxcPackage ];
|
|
|
|
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
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-01-09 10:05:03 +00:00
|
|
|
# TODO: remove once LXD gets proper support for cgroupsv2
|
|
|
|
# (currently most of the e.g. CPU accounting stuff doesn't work)
|
|
|
|
systemd.enableUnifiedCgroupHierarchy = false;
|
|
|
|
|
2021-03-09 03:18:52 +00:00
|
|
|
systemd.sockets.lxd = {
|
|
|
|
description = "LXD UNIX socket";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
|
|
|
|
socketConfig = {
|
|
|
|
ListenStream = "/var/lib/lxd/unix.socket";
|
|
|
|
SocketMode = "0660";
|
|
|
|
SocketGroup = "lxd";
|
|
|
|
Service = "lxd.service";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
systemd.services.lxd = {
|
|
|
|
description = "LXD Container Management Daemon";
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-03-09 03:18:52 +00:00
|
|
|
after = [ "network-online.target" "lxcfs.service" ];
|
|
|
|
requires = [ "network-online.target" "lxd.socket" "lxcfs.service" ];
|
|
|
|
documentation = [ "man:lxd(1)" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-03-09 03:18:52 +00:00
|
|
|
path = optional cfg.zfsSupport config.boot.zfs.package;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
serviceConfig = {
|
2020-05-15 21:57:56 +00:00
|
|
|
ExecStart = "@${cfg.package}/bin/lxd lxd --group lxd";
|
2021-03-09 03:18:52 +00:00
|
|
|
ExecStartPost = "${cfg.package}/bin/lxd waitready --timeout=${cfg.startTimeout}";
|
|
|
|
ExecStop = "${cfg.package}/bin/lxd shutdown";
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
KillMode = "process"; # when stopping, leave the containers alone
|
|
|
|
LimitMEMLOCK = "infinity";
|
|
|
|
LimitNOFILE = "1048576";
|
|
|
|
LimitNPROC = "infinity";
|
|
|
|
TasksMax = "infinity";
|
2020-06-18 07:06:33 +00:00
|
|
|
|
2021-03-09 03:18:52 +00:00
|
|
|
Restart = "on-failure";
|
|
|
|
TimeoutStartSec = "${cfg.startTimeout}s";
|
|
|
|
TimeoutStopSec = "30s";
|
|
|
|
|
2020-06-18 07:06:33 +00: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";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-09-18 10:52:07 +00:00
|
|
|
users.groups.lxd = {};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
users.users.root = {
|
|
|
|
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;
|
|
|
|
};
|
2021-06-06 07:54:09 +00:00
|
|
|
|
|
|
|
boot.kernelModules = [ "veth" "xt_comment" "xt_CHECKSUM" "xt_MASQUERADE" ]
|
|
|
|
++ optionals (!config.networking.nftables.enable) [ "iptable_mangle" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
}
|