diff --git a/containers/server-n0/ssh.nix b/containers/server-n0/ssh.nix index 6b293a5..dde4a13 100644 --- a/containers/server-n0/ssh.nix +++ b/containers/server-n0/ssh.nix @@ -61,16 +61,27 @@ let net ? { }, resources ? { }, traits ? [ ], + isolation ? { }, }: mkContainer { inherit name; inherit net resources; svc.autoStart = true; - isolation.nixDaemon = nixDaemon; + isolation = { nixDaemon = nixDaemon; } // isolation; traits = [ sshTrait ] ++ traits; packages = with pkgs; [ tmux ] ++ packages; miscProperties = self._sshpiperRegister routingInfo; }; + + kvmTrait = { + containerConfig = + { pkgs, ... }: + { + users.users.runner.extraGroups = [ "wheel" ]; + security.sudo.wheelNeedsPassword = false; + environment.systemPackages = [ pkgs.qemu_kvm ]; + }; + }; in { ssh-admin = mkSsh { @@ -121,6 +132,25 @@ in mem = "16G"; cpu = 65; }; + traits = [ kvmTrait ]; + isolation.allowedDevices = [ + { + node = "/dev/kvm"; + modifier = "rw"; + } + { + node = "/dev/vhost-net"; + modifier = "rw"; + } + { + node = "/dev/net/tun"; + modifier = "rwm"; + } + ]; + isolation.additionalCapabilities = [ + "CAP_NET_ADMIN" + "CAP_MKNOD" + ]; routingInfo = [ { username = "autumn"; diff --git a/dsl.nix b/dsl.nix index ab4be0e..1c6dcf0 100644 --- a/dsl.nix +++ b/dsl.nix @@ -228,6 +228,17 @@ rec { : systemd-nspawn user namespacing; see `--private-users=` in systemd-nspawn(1). Bind mounts always go through idmapped `--bind`. + `isolation.allowedDevices` + : List of `{ node; modifier; }` host devices to grant the container's + cgroup access to; see `containers..allowedDevices` in the NixOS + manual. nspawn bind-mounts a handful of devices (`/dev/kvm`, + `/dev/net/tun`, ...) into the container itself once the cgroup allows it - + this doesn't need a matching `resources.fs` bind mount. + + `isolation.additionalCapabilities` + : Extra Linux capabilities granted to the container; see + `containers..additionalCapabilities` in the NixOS manual. + `miscProperties` : Arbitrary data attached to the result. Used for cross-container discovery via `collectContainers`. @@ -272,6 +283,8 @@ rec { bindMounts = resources.fs or { }; allowsNixDaemon = isolation.nixDaemon or false; privateUsers = isolation.privateUsers or "pick"; + allowedDevices = isolation.allowedDevices or [ ]; + additionalCapabilities = isolation.additionalCapabilities or [ ]; allowInternet = hasInternetAccess access; allBindMounts = builtins.foldl' (acc: t: acc // (t.resources.fs or { })) { } traits // bindMounts; @@ -368,7 +381,12 @@ rec { kind = "nspawn"; miscProperties = flattenMiscProp self miscProperties; nixosContainer = { - inherit autoStart privateUsers; + inherit + autoStart + privateUsers + allowedDevices + additionalCapabilities + ; bindMounts = { }; extraFlags = bindMountExtraFlags; privateNetwork = true;