mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-07-13 13:40:28 +03:00
nixos/lxd: add virtual-machine support, image and module
This commit is contained in:
parent
73982ef92a
commit
efd1605be6
15 changed files with 365 additions and 232 deletions
|
@ -1,96 +1,16 @@
|
|||
{ lib, config, pkgs, ... }:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
templateSubmodule = { ... }: {
|
||||
options = {
|
||||
enable = mkEnableOption (lib.mdDoc "this template");
|
||||
|
||||
target = mkOption {
|
||||
description = lib.mdDoc "Path in the container";
|
||||
type = types.path;
|
||||
};
|
||||
template = mkOption {
|
||||
description = lib.mdDoc ".tpl file for rendering the target";
|
||||
type = types.path;
|
||||
};
|
||||
when = mkOption {
|
||||
description = lib.mdDoc "Events which trigger a rewrite (create, copy)";
|
||||
type = types.listOf (types.str);
|
||||
};
|
||||
properties = mkOption {
|
||||
description = lib.mdDoc "Additional properties";
|
||||
type = types.attrs;
|
||||
default = {};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
toYAML = name: data: pkgs.writeText name (generators.toYAML {} data);
|
||||
|
||||
cfg = config.virtualisation.lxc;
|
||||
templates = if cfg.templates != {} then let
|
||||
list = mapAttrsToList (name: value: { inherit name; } // value)
|
||||
(filterAttrs (name: value: value.enable) cfg.templates);
|
||||
in
|
||||
{
|
||||
files = map (tpl: {
|
||||
source = tpl.template;
|
||||
target = "/templates/${tpl.name}.tpl";
|
||||
}) list;
|
||||
properties = listToAttrs (map (tpl: nameValuePair tpl.target {
|
||||
when = tpl.when;
|
||||
template = "${tpl.name}.tpl";
|
||||
properties = tpl.properties;
|
||||
}) list);
|
||||
}
|
||||
else { files = []; properties = {}; };
|
||||
|
||||
in
|
||||
{
|
||||
in {
|
||||
imports = [
|
||||
../installer/cd-dvd/channel.nix
|
||||
../profiles/clone-config.nix
|
||||
../profiles/minimal.nix
|
||||
./lxc-instance-common.nix
|
||||
];
|
||||
|
||||
options = {
|
||||
virtualisation.lxc = {
|
||||
templates = mkOption {
|
||||
description = lib.mdDoc "Templates for LXD";
|
||||
type = types.attrsOf (types.submodule (templateSubmodule));
|
||||
default = {};
|
||||
example = literalExpression ''
|
||||
{
|
||||
# create /etc/hostname on container creation. also requires networking.hostName = "" to be set
|
||||
"hostname" = {
|
||||
enable = true;
|
||||
target = "/etc/hostname";
|
||||
template = builtins.toFile "hostname.tpl" "{{ container.name }}";
|
||||
when = [ "create" ];
|
||||
};
|
||||
# create /etc/nixos/hostname.nix with a configuration for keeping the hostname applied
|
||||
"hostname-nix" = {
|
||||
enable = true;
|
||||
target = "/etc/nixos/hostname.nix";
|
||||
template = builtins.toFile "hostname-nix.tpl" "{ ... }: { networking.hostName = \"{{ container.name }}\"; }";
|
||||
# copy keeps the file updated when the container is changed
|
||||
when = [ "create" "copy" ];
|
||||
};
|
||||
# copy allow the user to specify a custom configuration.nix
|
||||
"configuration-nix" = {
|
||||
enable = true;
|
||||
target = "/etc/nixos/configuration.nix";
|
||||
template = builtins.toFile "configuration-nix" "{{ config_get(\"user.user-data\", properties.default) }}";
|
||||
when = [ "create" ];
|
||||
};
|
||||
};
|
||||
'';
|
||||
};
|
||||
|
||||
privilegedContainer = mkOption {
|
||||
type = types.bool;
|
||||
privilegedContainer = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = lib.mdDoc ''
|
||||
Whether this LXC container will be running as a privileged container or not. If set to `true` then
|
||||
|
@ -116,24 +36,6 @@ in
|
|||
${config.nix.package.out}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
|
||||
'';
|
||||
|
||||
system.build.metadata = pkgs.callPackage ../../lib/make-system-tarball.nix {
|
||||
contents = [
|
||||
{
|
||||
source = toYAML "metadata.yaml" {
|
||||
architecture = builtins.elemAt (builtins.match "^([a-z0-9_]+).+" (toString pkgs.system)) 0;
|
||||
creation_date = 1;
|
||||
properties = {
|
||||
description = "${config.system.nixos.distroName} ${config.system.nixos.codeName} ${config.system.nixos.label} ${pkgs.system}";
|
||||
os = "${config.system.nixos.distroId}";
|
||||
release = "${config.system.nixos.codeName}";
|
||||
};
|
||||
templates = templates.properties;
|
||||
};
|
||||
target = "/metadata.yaml";
|
||||
}
|
||||
] ++ templates.files;
|
||||
};
|
||||
|
||||
# TODO: build rootfs as squashfs for faster unpack
|
||||
system.build.tarball = pkgs.callPackage ../../lib/make-system-tarball.nix {
|
||||
extraArgs = "--owner=0";
|
||||
|
@ -180,7 +82,7 @@ in
|
|||
ProtectKernelTunables=no
|
||||
NoNewPrivileges=no
|
||||
LoadCredential=
|
||||
'' + optionalString cfg.privilegedContainer ''
|
||||
'' + lib.optionalString cfg.privilegedContainer ''
|
||||
# Additional settings for privileged containers
|
||||
ProtectHome=no
|
||||
ProtectSystem=no
|
||||
|
@ -193,28 +95,8 @@ in
|
|||
})
|
||||
];
|
||||
|
||||
# Allow the user to login as root without password.
|
||||
users.users.root.initialHashedPassword = mkOverride 150 "";
|
||||
|
||||
system.activationScripts.installInitScript = mkForce ''
|
||||
system.activationScripts.installInitScript = lib.mkForce ''
|
||||
ln -fs $systemConfig/init /sbin/init
|
||||
'';
|
||||
|
||||
# Some more help text.
|
||||
services.getty.helpLine =
|
||||
''
|
||||
|
||||
Log in as "root" with an empty password.
|
||||
'';
|
||||
|
||||
# Containers should be light-weight, so start sshd on demand.
|
||||
services.openssh.enable = mkDefault true;
|
||||
services.openssh.startWhenNeeded = mkDefault true;
|
||||
|
||||
# As this is intended as a standalone image, undo some of the minimal profile stuff
|
||||
environment.noXlibs = false;
|
||||
documentation.enable = true;
|
||||
documentation.nixos.enable = true;
|
||||
services.logrotate.enable = true;
|
||||
};
|
||||
}
|
||||
|
|
104
nixos/modules/virtualisation/lxc-image-metadata.nix
Normal file
104
nixos/modules/virtualisation/lxc-image-metadata.nix
Normal file
|
@ -0,0 +1,104 @@
|
|||
{ lib, config, pkgs, ... }:
|
||||
|
||||
let
|
||||
templateSubmodule = {...}: {
|
||||
options = {
|
||||
enable = lib.mkEnableOption "this template";
|
||||
|
||||
target = lib.mkOption {
|
||||
description = "Path in the container";
|
||||
type = lib.types.path;
|
||||
};
|
||||
template = lib.mkOption {
|
||||
description = ".tpl file for rendering the target";
|
||||
type = lib.types.path;
|
||||
};
|
||||
when = lib.mkOption {
|
||||
description = "Events which trigger a rewrite (create, copy)";
|
||||
type = lib.types.listOf (lib.types.str);
|
||||
};
|
||||
properties = lib.mkOption {
|
||||
description = "Additional properties";
|
||||
type = lib.types.attrs;
|
||||
default = {};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
toYAML = name: data: pkgs.writeText name (lib.generators.toYAML {} data);
|
||||
|
||||
cfg = config.virtualisation.lxc;
|
||||
templates = if cfg.templates != {} then let
|
||||
list = lib.mapAttrsToList (name: value: { inherit name; } // value)
|
||||
(lib.filterAttrs (name: value: value.enable) cfg.templates);
|
||||
in
|
||||
{
|
||||
files = map (tpl: {
|
||||
source = tpl.template;
|
||||
target = "/templates/${tpl.name}.tpl";
|
||||
}) list;
|
||||
properties = lib.listToAttrs (map (tpl: lib.nameValuePair tpl.target {
|
||||
when = tpl.when;
|
||||
template = "${tpl.name}.tpl";
|
||||
properties = tpl.properties;
|
||||
}) list);
|
||||
}
|
||||
else { files = []; properties = {}; };
|
||||
|
||||
in {
|
||||
options = {
|
||||
virtualisation.lxc = {
|
||||
templates = lib.mkOption {
|
||||
description = "Templates for LXD";
|
||||
type = lib.types.attrsOf (lib.types.submodule templateSubmodule);
|
||||
default = {};
|
||||
example = lib.literalExpression ''
|
||||
{
|
||||
# create /etc/hostname on container creation
|
||||
"hostname" = {
|
||||
enable = true;
|
||||
target = "/etc/hostname";
|
||||
template = builtins.writeFile "hostname.tpl" "{{ container.name }}";
|
||||
when = [ "create" ];
|
||||
};
|
||||
# create /etc/nixos/hostname.nix with a configuration for keeping the hostname applied
|
||||
"hostname-nix" = {
|
||||
enable = true;
|
||||
target = "/etc/nixos/hostname.nix";
|
||||
template = builtins.writeFile "hostname-nix.tpl" "{ ... }: { networking.hostName = "{{ container.name }}"; }";
|
||||
# copy keeps the file updated when the container is changed
|
||||
when = [ "create" "copy" ];
|
||||
};
|
||||
# copy allow the user to specify a custom configuration.nix
|
||||
"configuration-nix" = {
|
||||
enable = true;
|
||||
target = "/etc/nixos/configuration.nix";
|
||||
template = builtins.writeFile "configuration-nix" "{{ config_get(\"user.user-data\", properties.default) }}";
|
||||
when = [ "create" ];
|
||||
};
|
||||
};
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
config = {
|
||||
system.build.metadata = pkgs.callPackage ../../lib/make-system-tarball.nix {
|
||||
contents = [
|
||||
{
|
||||
source = toYAML "metadata.yaml" {
|
||||
architecture = builtins.elemAt (builtins.match "^([a-z0-9_]+).+" (toString pkgs.system)) 0;
|
||||
creation_date = 1;
|
||||
properties = {
|
||||
description = "${config.system.nixos.distroName} ${config.system.nixos.codeName} ${config.system.nixos.label} ${pkgs.system}";
|
||||
os = "${config.system.nixos.distroId}";
|
||||
release = "${config.system.nixos.codeName}";
|
||||
};
|
||||
templates = templates.properties;
|
||||
};
|
||||
target = "/metadata.yaml";
|
||||
}
|
||||
] ++ templates.files;
|
||||
};
|
||||
};
|
||||
}
|
30
nixos/modules/virtualisation/lxc-instance-common.nix
Normal file
30
nixos/modules/virtualisation/lxc-instance-common.nix
Normal file
|
@ -0,0 +1,30 @@
|
|||
{lib, ...}:
|
||||
|
||||
{
|
||||
imports = [
|
||||
./lxc-image-metadata.nix
|
||||
|
||||
../installer/cd-dvd/channel.nix
|
||||
../profiles/clone-config.nix
|
||||
../profiles/minimal.nix
|
||||
];
|
||||
|
||||
# Allow the user to login as root without password.
|
||||
users.users.root.initialHashedPassword = lib.mkOverride 150 "";
|
||||
|
||||
# Some more help text.
|
||||
services.getty.helpLine = ''
|
||||
|
||||
Log in as "root" with an empty password.
|
||||
'';
|
||||
|
||||
# Containers should be light-weight, so start sshd on demand.
|
||||
services.openssh.enable = lib.mkDefault true;
|
||||
services.openssh.startWhenNeeded = lib.mkDefault true;
|
||||
|
||||
# As this is intended as a standalone image, undo some of the minimal profile stuff
|
||||
environment.noXlibs = false;
|
||||
documentation.enable = true;
|
||||
documentation.nixos.enable = true;
|
||||
services.logrotate.enable = true;
|
||||
}
|
46
nixos/modules/virtualisation/lxd-virtual-machine.nix
Normal file
46
nixos/modules/virtualisation/lxd-virtual-machine.nix
Normal file
|
@ -0,0 +1,46 @@
|
|||
{ config, lib, pkgs, ... }:
|
||||
|
||||
let
|
||||
serialDevice =
|
||||
if pkgs.stdenv.hostPlatform.isx86
|
||||
then "ttyS0"
|
||||
else "ttyAMA0"; # aarch64
|
||||
in {
|
||||
imports = [
|
||||
./lxc-instance-common.nix
|
||||
|
||||
../profiles/qemu-guest.nix
|
||||
];
|
||||
|
||||
config = {
|
||||
system.build.qemuImage = import ../../lib/make-disk-image.nix {
|
||||
inherit pkgs lib config;
|
||||
|
||||
partitionTableType = "efi";
|
||||
format = "qcow2-compressed";
|
||||
copyChannel = true;
|
||||
};
|
||||
|
||||
fileSystems = {
|
||||
"/" = {
|
||||
device = "/dev/disk/by-label/nixos";
|
||||
autoResize = true;
|
||||
fsType = "ext4";
|
||||
};
|
||||
"/boot" = {
|
||||
device = "/dev/disk/by-label/ESP";
|
||||
fsType = "vfat";
|
||||
};
|
||||
};
|
||||
|
||||
boot.growPartition = true;
|
||||
boot.loader.systemd-boot.enable = true;
|
||||
|
||||
# image building needs to know what device to install bootloader on
|
||||
boot.loader.grub.device = "/dev/vda";
|
||||
|
||||
boot.kernelParams = ["console=tty1" "console=${serialDevice}"];
|
||||
|
||||
virtualisation.lxd.agent.enable = lib.mkDefault true;
|
||||
};
|
||||
}
|
|
@ -196,7 +196,7 @@ in {
|
|||
"kernel.keys.maxkeys" = 2000;
|
||||
};
|
||||
|
||||
boot.kernelModules = [ "veth" "xt_comment" "xt_CHECKSUM" "xt_MASQUERADE" ]
|
||||
boot.kernelModules = [ "veth" "xt_comment" "xt_CHECKSUM" "xt_MASQUERADE" "vhost_vsock" ]
|
||||
++ optionals (!config.networking.nftables.enable) [ "iptable_mangle" ];
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue