2020-04-24 23:36:52 +00:00
|
|
|
# LXC Configuration
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.virtualisation.lxc;
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2024-01-02 11:29:13 +00:00
|
|
|
meta = {
|
|
|
|
maintainers = lib.teams.lxc.members;
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
options.virtualisation.lxc = {
|
|
|
|
enable =
|
2024-01-02 11:29:13 +00:00
|
|
|
lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
This enables Linux Containers (LXC), which provides tools
|
|
|
|
for creating and managing system or application containers
|
|
|
|
on Linux.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
unprivilegedContainers = lib.mkEnableOption "support for unprivileged users to launch containers";
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
systemConfig =
|
2024-01-02 11:29:13 +00:00
|
|
|
lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
This is the system-wide LXC config. See
|
2022-08-21 13:32:41 +00:00
|
|
|
{manpage}`lxc.system.conf(5)`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
2024-05-15 15:35:15 +00:00
|
|
|
package = lib.mkPackageOption pkgs "lxc" { };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
defaultConfig =
|
2024-01-02 11:29:13 +00:00
|
|
|
lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Default config (default.conf) for new containers, i.e. for
|
2022-08-21 13:32:41 +00:00
|
|
|
network config. See {manpage}`lxc.container.conf(5)`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
usernetConfig =
|
2024-01-02 11:29:13 +00:00
|
|
|
lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
This is the config file for managing unprivileged user network
|
2022-08-21 13:32:41 +00:00
|
|
|
administration access in LXC. See {manpage}`lxc-usernet(5)`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
2024-09-19 14:19:46 +00:00
|
|
|
|
|
|
|
bridgeConfig =
|
|
|
|
lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
This is the config file for override lxc-net bridge default settings.
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2024-05-15 15:35:15 +00:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2020-04-24 23:36:52 +00:00
|
|
|
environment.etc."lxc/lxc.conf".text = cfg.systemConfig;
|
|
|
|
environment.etc."lxc/lxc-usernet".text = cfg.usernetConfig;
|
|
|
|
environment.etc."lxc/default.conf".text = cfg.defaultConfig;
|
2024-09-19 14:19:46 +00:00
|
|
|
environment.etc."lxc/lxc-net".text = cfg.bridgeConfig;
|
|
|
|
environment.pathsToLink = [ "/share/lxc" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
systemd.tmpfiles.rules = [ "d /var/lib/lxc/rootfs 0755 root root -" ];
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
security.apparmor.packages = [ cfg.package ];
|
2021-05-20 23:08:51 +00:00
|
|
|
security.apparmor.policies = {
|
|
|
|
"bin.lxc-start".profile = ''
|
2024-05-15 15:35:15 +00:00
|
|
|
include ${cfg.package}/etc/apparmor.d/usr.bin.lxc-start
|
2021-05-20 23:08:51 +00:00
|
|
|
'';
|
|
|
|
"lxc-containers".profile = ''
|
2024-05-15 15:35:15 +00:00
|
|
|
include ${cfg.package}/etc/apparmor.d/lxc-containers
|
2021-05-20 23:08:51 +00:00
|
|
|
'';
|
|
|
|
};
|
2024-09-19 14:19:46 +00:00
|
|
|
|
|
|
|
# We don't need the `lxc-user` group, unless the unprivileged containers are enabled.
|
|
|
|
users.groups = lib.mkIf cfg.unprivilegedContainers { lxc-user = {}; };
|
|
|
|
|
|
|
|
# `lxc-user-nic` needs suid to attach to bridge for unpriv containers.
|
|
|
|
security.wrappers = lib.mkIf cfg.unprivilegedContainers {
|
|
|
|
lxcUserNet = {
|
|
|
|
source = "${pkgs.lxc}/libexec/lxc/lxc-user-nic";
|
|
|
|
setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "lxc-user";
|
|
|
|
program = "lxc-user-nic";
|
|
|
|
permissions = "u+rx,g+x,o-rx";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# Add lxc-net service if unpriv mode is enabled.
|
|
|
|
systemd.packages = lib.mkIf cfg.unprivilegedContainers [ pkgs.lxc ];
|
|
|
|
systemd.services = lib.mkIf cfg.unprivilegedContainers {
|
|
|
|
lxc-net = {
|
|
|
|
enable = true;
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
path = [ pkgs.iproute2 pkgs.iptables pkgs.getent pkgs.dnsmasq ];
|
|
|
|
};
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
}
|