3 { config, lib, pkgs, ... }:
6 cfg = config.virtualisation.lxc;
11 maintainers = lib.teams.lxc.members;
14 options.virtualisation.lxc = {
17 type = lib.types.bool;
20 This enables Linux Containers (LXC), which provides tools
21 for creating and managing system or application containers
26 unprivilegedContainers = lib.mkEnableOption "support for unprivileged users to launch containers";
30 type = lib.types.lines;
33 This is the system-wide LXC config. See
34 {manpage}`lxc.system.conf(5)`.
37 package = lib.mkPackageOption pkgs "lxc" { };
41 type = lib.types.lines;
44 Default config (default.conf) for new containers, i.e. for
45 network config. See {manpage}`lxc.container.conf(5)`.
51 type = lib.types.lines;
54 This is the config file for managing unprivileged user network
55 administration access in LXC. See {manpage}`lxc-usernet(5)`.
61 type = lib.types.lines;
64 This is the config file for override lxc-net bridge default settings.
71 config = lib.mkIf cfg.enable {
72 environment.systemPackages = [ cfg.package ];
73 environment.etc."lxc/lxc.conf".text = cfg.systemConfig;
74 environment.etc."lxc/lxc-usernet".text = cfg.usernetConfig;
75 environment.etc."lxc/default.conf".text = cfg.defaultConfig;
76 environment.etc."lxc/lxc-net".text = cfg.bridgeConfig;
77 environment.pathsToLink = [ "/share/lxc" ];
78 systemd.tmpfiles.rules = [ "d /var/lib/lxc/rootfs 0755 root root -" ];
80 security.apparmor.packages = [ cfg.package ];
81 security.apparmor.policies = {
82 "bin.lxc-start".profile = ''
83 include ${cfg.package}/etc/apparmor.d/usr.bin.lxc-start
85 "lxc-containers".profile = ''
86 include ${cfg.package}/etc/apparmor.d/lxc-containers
90 # We don't need the `lxc-user` group, unless the unprivileged containers are enabled.
91 users.groups = lib.mkIf cfg.unprivilegedContainers { lxc-user = {}; };
93 # `lxc-user-nic` needs suid to attach to bridge for unpriv containers.
94 security.wrappers = lib.mkIf cfg.unprivilegedContainers {
96 source = "${pkgs.lxc}/libexec/lxc/lxc-user-nic";
100 program = "lxc-user-nic";
101 permissions = "u+rx,g+x,o-rx";
105 # Add lxc-net service if unpriv mode is enabled.
106 systemd.packages = lib.mkIf cfg.unprivilegedContainers [ pkgs.lxc ];
107 systemd.services = lib.mkIf cfg.unprivilegedContainers {
110 wantedBy = [ "multi-user.target" ];
111 path = [ pkgs.iproute2 pkgs.iptables pkgs.getent pkgs.dnsmasq ];