mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-10 03:23:29 +03:00
virtualisation/lxc-container: use system.build.image
This commit is contained in:
parent
f3563c996e
commit
06ad3811a8
2 changed files with 109 additions and 80 deletions
|
@ -1,4 +1,9 @@
|
||||||
{ lib, config, pkgs, ... }:
|
{
|
||||||
|
lib,
|
||||||
|
config,
|
||||||
|
pkgs,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
|
||||||
{
|
{
|
||||||
meta = {
|
meta = {
|
||||||
|
@ -8,18 +13,27 @@
|
||||||
imports = [
|
imports = [
|
||||||
./lxc-instance-common.nix
|
./lxc-instance-common.nix
|
||||||
|
|
||||||
(lib.mkRemovedOptionModule [ "virtualisation" "lxc" "nestedContainer" ] "")
|
(lib.mkRemovedOptionModule [
|
||||||
(lib.mkRemovedOptionModule [ "virtualisation" "lxc" "privilegedContainer" ] "")
|
"virtualisation"
|
||||||
|
"lxc"
|
||||||
|
"nestedContainer"
|
||||||
|
] "")
|
||||||
|
(lib.mkRemovedOptionModule [
|
||||||
|
"virtualisation"
|
||||||
|
"lxc"
|
||||||
|
"privilegedContainer"
|
||||||
|
] "")
|
||||||
];
|
];
|
||||||
|
|
||||||
options = { };
|
options = { };
|
||||||
|
|
||||||
config = let
|
config =
|
||||||
initScript = if config.boot.initrd.systemd.enable then "prepare-root" else "init";
|
let
|
||||||
in {
|
initScript = if config.boot.initrd.systemd.enable then "prepare-root" else "init";
|
||||||
boot.isContainer = true;
|
in
|
||||||
boot.postBootCommands =
|
{
|
||||||
''
|
boot.isContainer = true;
|
||||||
|
boot.postBootCommands = ''
|
||||||
# After booting, register the contents of the Nix store in the Nix
|
# After booting, register the contents of the Nix store in the Nix
|
||||||
# database.
|
# database.
|
||||||
if [ -f /nix-path-registration ]; then
|
if [ -f /nix-path-registration ]; then
|
||||||
|
@ -31,78 +45,84 @@
|
||||||
${config.nix.package.out}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
|
${config.nix.package.out}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
|
||||||
'';
|
'';
|
||||||
|
|
||||||
# supplement 99-ethernet-default-dhcp which excludes veth
|
# supplement 99-ethernet-default-dhcp which excludes veth
|
||||||
systemd.network = lib.mkIf config.networking.useDHCP {
|
systemd.network = lib.mkIf config.networking.useDHCP {
|
||||||
networks."99-lxc-veth-default-dhcp" = {
|
networks."99-lxc-veth-default-dhcp" = {
|
||||||
matchConfig = {
|
matchConfig = {
|
||||||
Type = "ether";
|
Type = "ether";
|
||||||
Kind = "veth";
|
Kind = "veth";
|
||||||
Name = [
|
Name = [
|
||||||
"en*"
|
"en*"
|
||||||
"eth*"
|
"eth*"
|
||||||
];
|
];
|
||||||
|
};
|
||||||
|
DHCP = "yes";
|
||||||
|
networkConfig.IPv6PrivacyExtensions = "kernel";
|
||||||
};
|
};
|
||||||
DHCP = "yes";
|
|
||||||
networkConfig.IPv6PrivacyExtensions = "kernel";
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
system.nixos.tags = lib.mkOverride 99 [ "lxc" ];
|
||||||
|
image.extension = "tar.xz";
|
||||||
|
image.filePath = "tarball/${config.image.fileName}";
|
||||||
|
system.build.image = lib.mkOverride 99 config.system.build.tarball;
|
||||||
|
|
||||||
|
system.build.tarball = pkgs.callPackage ../../lib/make-system-tarball.nix {
|
||||||
|
fileName = config.image.baseName;
|
||||||
|
extraArgs = "--owner=0";
|
||||||
|
|
||||||
|
storeContents = [
|
||||||
|
{
|
||||||
|
object = config.system.build.toplevel;
|
||||||
|
symlink = "none";
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
contents = [
|
||||||
|
{
|
||||||
|
source = config.system.build.toplevel + "/${initScript}";
|
||||||
|
target = "/sbin/init";
|
||||||
|
}
|
||||||
|
# Technically this is not required for lxc, but having also make this configuration work with systemd-nspawn.
|
||||||
|
# Nixos will setup the same symlink after start.
|
||||||
|
{
|
||||||
|
source = config.system.build.toplevel + "/etc/os-release";
|
||||||
|
target = "/etc/os-release";
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
extraCommands = "mkdir -p proc sys dev";
|
||||||
|
};
|
||||||
|
|
||||||
|
system.build.squashfs = pkgs.callPackage ../../lib/make-squashfs.nix {
|
||||||
|
fileName = "nixos-lxc-image-${pkgs.stdenv.hostPlatform.system}";
|
||||||
|
|
||||||
|
hydraBuildProduct = true;
|
||||||
|
noStrip = true; # keep directory structure
|
||||||
|
comp = "zstd -Xcompression-level 6";
|
||||||
|
|
||||||
|
storeContents = [ config.system.build.toplevel ];
|
||||||
|
|
||||||
|
pseudoFiles = [
|
||||||
|
"/sbin d 0755 0 0"
|
||||||
|
"/sbin/init s 0555 0 0 ${config.system.build.toplevel}/${initScript}"
|
||||||
|
"/dev d 0755 0 0"
|
||||||
|
"/proc d 0555 0 0"
|
||||||
|
"/sys d 0555 0 0"
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
|
system.build.installBootLoader = pkgs.writeScript "install-lxc-sbin-init.sh" ''
|
||||||
|
#!${pkgs.runtimeShell}
|
||||||
|
${pkgs.coreutils}/bin/ln -fs "$1/${initScript}" /sbin/init
|
||||||
|
'';
|
||||||
|
|
||||||
|
# networkd depends on this, but systemd module disables this for containers
|
||||||
|
systemd.additionalUpstreamSystemUnits = [ "systemd-udev-trigger.service" ];
|
||||||
|
|
||||||
|
systemd.packages = [ pkgs.distrobuilder.generator ];
|
||||||
|
|
||||||
|
system.activationScripts.installInitScript = lib.mkForce ''
|
||||||
|
ln -fs $systemConfig/${initScript} /sbin/init
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
system.build.tarball = pkgs.callPackage ../../lib/make-system-tarball.nix {
|
|
||||||
extraArgs = "--owner=0";
|
|
||||||
|
|
||||||
storeContents = [
|
|
||||||
{
|
|
||||||
object = config.system.build.toplevel;
|
|
||||||
symlink = "none";
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
contents = [
|
|
||||||
{
|
|
||||||
source = config.system.build.toplevel + "/${initScript}";
|
|
||||||
target = "/sbin/init";
|
|
||||||
}
|
|
||||||
# Technically this is not required for lxc, but having also make this configuration work with systemd-nspawn.
|
|
||||||
# Nixos will setup the same symlink after start.
|
|
||||||
{
|
|
||||||
source = config.system.build.toplevel + "/etc/os-release";
|
|
||||||
target = "/etc/os-release";
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
extraCommands = "mkdir -p proc sys dev";
|
|
||||||
};
|
|
||||||
|
|
||||||
system.build.squashfs = pkgs.callPackage ../../lib/make-squashfs.nix {
|
|
||||||
fileName = "nixos-lxc-image-${pkgs.stdenv.hostPlatform.system}";
|
|
||||||
|
|
||||||
hydraBuildProduct = true;
|
|
||||||
noStrip = true; # keep directory structure
|
|
||||||
comp = "zstd -Xcompression-level 6";
|
|
||||||
|
|
||||||
storeContents = [config.system.build.toplevel];
|
|
||||||
|
|
||||||
pseudoFiles = [
|
|
||||||
"/sbin d 0755 0 0"
|
|
||||||
"/sbin/init s 0555 0 0 ${config.system.build.toplevel}/${initScript}"
|
|
||||||
"/dev d 0755 0 0"
|
|
||||||
"/proc d 0555 0 0"
|
|
||||||
"/sys d 0555 0 0"
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
system.build.installBootLoader = pkgs.writeScript "install-lxc-sbin-init.sh" ''
|
|
||||||
#!${pkgs.runtimeShell}
|
|
||||||
${pkgs.coreutils}/bin/ln -fs "$1/${initScript}" /sbin/init
|
|
||||||
'';
|
|
||||||
|
|
||||||
# networkd depends on this, but systemd module disables this for containers
|
|
||||||
systemd.additionalUpstreamSystemUnits = ["systemd-udev-trigger.service"];
|
|
||||||
|
|
||||||
systemd.packages = [ pkgs.distrobuilder.generator ];
|
|
||||||
|
|
||||||
system.activationScripts.installInitScript = lib.mkForce ''
|
|
||||||
ln -fs $systemConfig/${initScript} /sbin/init
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,10 @@ let
|
||||||
else { files = []; properties = {}; };
|
else { files = []; properties = {}; };
|
||||||
|
|
||||||
in {
|
in {
|
||||||
|
imports = [
|
||||||
|
../image/file-options.nix
|
||||||
|
];
|
||||||
|
|
||||||
meta = {
|
meta = {
|
||||||
maintainers = lib.teams.lxc.members;
|
maintainers = lib.teams.lxc.members;
|
||||||
};
|
};
|
||||||
|
@ -87,7 +91,12 @@ in {
|
||||||
};
|
};
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
|
system.nixos.tags = [ "lxc" "metadata" ];
|
||||||
|
image.extension = "tar.xz";
|
||||||
|
image.filePath = "tarball/${config.image.fileName}";
|
||||||
|
system.build.image = config.system.build.metadata;
|
||||||
system.build.metadata = pkgs.callPackage ../../lib/make-system-tarball.nix {
|
system.build.metadata = pkgs.callPackage ../../lib/make-system-tarball.nix {
|
||||||
|
fileName = config.image.baseName;
|
||||||
contents = [
|
contents = [
|
||||||
{
|
{
|
||||||
source = toYAML "metadata.yaml" {
|
source = toYAML "metadata.yaml" {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue