11 cfg = config.virtualisation.lxc;
16 maintainers = lib.teams.lxc.members;
19 options.virtualisation.lxc = {
20 enable = lib.mkOption {
21 type = lib.types.bool;
24 This enables Linux Containers (LXC), which provides tools
25 for creating and managing system or application containers
30 unprivilegedContainers = lib.mkEnableOption "support for unprivileged users to launch containers";
32 systemConfig = lib.mkOption {
33 type = lib.types.lines;
36 This is the system-wide LXC config. See
37 {manpage}`lxc.system.conf(5)`.
40 package = lib.mkPackageOption pkgs "lxc" { };
42 defaultConfig = lib.mkOption {
43 type = lib.types.lines;
46 Default config (default.conf) for new containers, i.e. for
47 network config. See {manpage}`lxc.container.conf(5)`.
51 usernetConfig = lib.mkOption {
52 type = lib.types.lines;
55 This is the config file for managing unprivileged user network
56 administration access in LXC. See {manpage}`lxc-usernet(5)`.
60 bridgeConfig = lib.mkOption {
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" ];