2021-04-28 03:55:09 +01:00
|
|
|
{ lib, config, pkgs, ... }:
|
2014-12-11 21:58:17 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
2021-04-28 03:55:09 +01:00
|
|
|
let
|
|
|
|
templateSubmodule = { ... }: {
|
|
|
|
options = {
|
2022-08-29 18:33:50 +01:00
|
|
|
enable = mkEnableOption (lib.mdDoc "this template");
|
2021-04-28 03:55:09 +01:00
|
|
|
|
|
|
|
target = mkOption {
|
2022-08-29 18:33:50 +01:00
|
|
|
description = lib.mdDoc "Path in the container";
|
2021-04-28 03:55:09 +01:00
|
|
|
type = types.path;
|
|
|
|
};
|
|
|
|
template = mkOption {
|
2022-08-29 18:33:50 +01:00
|
|
|
description = lib.mdDoc ".tpl file for rendering the target";
|
2021-04-28 03:55:09 +01:00
|
|
|
type = types.path;
|
|
|
|
};
|
|
|
|
when = mkOption {
|
2022-08-29 18:33:50 +01:00
|
|
|
description = lib.mdDoc "Events which trigger a rewrite (create, copy)";
|
2021-04-28 03:55:09 +01:00
|
|
|
type = types.listOf (types.str);
|
|
|
|
};
|
|
|
|
properties = mkOption {
|
2022-08-29 18:33:50 +01:00
|
|
|
description = lib.mdDoc "Additional properties";
|
2021-04-28 03:55:09 +01:00
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-10-19 15:50:51 +01:00
|
|
|
toYAML = name: data: pkgs.writeText name (generators.toYAML {} data);
|
2021-04-28 03:55:09 +01:00
|
|
|
|
|
|
|
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
|
2014-12-11 21:58:17 +00:00
|
|
|
{
|
|
|
|
imports = [
|
2021-10-19 15:50:51 +01:00
|
|
|
../installer/cd-dvd/channel.nix
|
|
|
|
../profiles/minimal.nix
|
|
|
|
../profiles/clone-config.nix
|
2014-12-11 21:58:17 +00:00
|
|
|
];
|
|
|
|
|
2021-04-28 03:55:09 +01:00
|
|
|
options = {
|
|
|
|
virtualisation.lxc = {
|
|
|
|
templates = mkOption {
|
2022-08-29 18:33:50 +01:00
|
|
|
description = lib.mdDoc "Templates for LXD";
|
2021-04-28 03:55:09 +01:00
|
|
|
type = types.attrsOf (types.submodule (templateSubmodule));
|
2021-04-30 20:53:01 +01:00
|
|
|
default = {};
|
2021-11-10 21:55:13 +00:00
|
|
|
example = literalExpression ''
|
2021-04-29 07:55:45 +01:00
|
|
|
{
|
2022-06-17 16:42:16 +01:00
|
|
|
# create /etc/hostname on container creation. also requires networking.hostName = "" to be set
|
2021-04-29 07:55:45 +01:00
|
|
|
"hostname" = {
|
|
|
|
enable = true;
|
|
|
|
target = "/etc/hostname";
|
2022-06-17 16:42:16 +01:00
|
|
|
template = builtins.toFile "hostname.tpl" "{{ container.name }}";
|
2021-04-29 07:55:45 +01:00
|
|
|
when = [ "create" ];
|
|
|
|
};
|
|
|
|
# create /etc/nixos/hostname.nix with a configuration for keeping the hostname applied
|
|
|
|
"hostname-nix" = {
|
|
|
|
enable = true;
|
|
|
|
target = "/etc/nixos/hostname.nix";
|
2022-06-17 16:42:16 +01:00
|
|
|
template = builtins.toFile "hostname-nix.tpl" "{ ... }: { networking.hostName = \"{{ container.name }}\"; }";
|
2021-04-29 07:55:45 +01:00
|
|
|
# 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";
|
2022-06-17 16:42:16 +01:00
|
|
|
template = builtins.toFile "configuration-nix" "{{ config_get(\"user.user-data\", properties.default) }}";
|
2021-04-29 07:55:45 +01:00
|
|
|
when = [ "create" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
'';
|
2021-04-28 03:55:09 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-04-28 03:29:09 +01:00
|
|
|
config = {
|
2021-10-19 15:50:51 +01:00
|
|
|
boot.isContainer = true;
|
|
|
|
boot.postBootCommands =
|
|
|
|
''
|
|
|
|
# After booting, register the contents of the Nix store in the Nix
|
|
|
|
# database.
|
|
|
|
if [ -f /nix-path-registration ]; then
|
|
|
|
${config.nix.package.out}/bin/nix-store --load-db < /nix-path-registration &&
|
|
|
|
rm /nix-path-registration
|
|
|
|
fi
|
|
|
|
|
|
|
|
# nixos-rebuild also requires a "system" profile
|
|
|
|
${config.nix.package.out}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
|
|
|
|
'';
|
|
|
|
|
2021-04-28 04:09:30 +01:00
|
|
|
system.build.metadata = pkgs.callPackage ../../lib/make-system-tarball.nix {
|
2021-04-28 03:29:09 +01:00
|
|
|
contents = [
|
|
|
|
{
|
2021-04-28 03:55:09 +01:00
|
|
|
source = toYAML "metadata.yaml" {
|
|
|
|
architecture = builtins.elemAt (builtins.match "^([a-z0-9_]+).+" (toString pkgs.system)) 0;
|
|
|
|
creation_date = 1;
|
|
|
|
properties = {
|
|
|
|
description = "NixOS ${config.system.nixos.codeName} ${config.system.nixos.label} ${pkgs.system}";
|
|
|
|
os = "nixos";
|
|
|
|
release = "${config.system.nixos.codeName}";
|
|
|
|
};
|
|
|
|
templates = templates.properties;
|
|
|
|
};
|
2021-04-28 03:29:09 +01:00
|
|
|
target = "/metadata.yaml";
|
|
|
|
}
|
2021-04-28 04:09:30 +01:00
|
|
|
] ++ templates.files;
|
|
|
|
};
|
|
|
|
|
2021-10-19 16:27:01 +01:00
|
|
|
# TODO: build rootfs as squashfs for faster unpack
|
2021-10-19 15:50:51 +01:00
|
|
|
system.build.tarball = pkgs.callPackage ../../lib/make-system-tarball.nix {
|
2021-04-28 04:09:30 +01:00
|
|
|
extraArgs = "--owner=0";
|
|
|
|
|
|
|
|
storeContents = [
|
|
|
|
{
|
|
|
|
object = config.system.build.toplevel;
|
|
|
|
symlink = "none";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
contents = [
|
2021-04-28 03:29:09 +01:00
|
|
|
{
|
|
|
|
source = config.system.build.toplevel + "/init";
|
|
|
|
target = "/sbin/init";
|
|
|
|
}
|
2021-04-28 04:09:30 +01:00
|
|
|
];
|
2021-04-28 03:29:09 +01:00
|
|
|
|
|
|
|
extraCommands = "mkdir -p proc sys dev";
|
2021-10-19 15:50:51 +01:00
|
|
|
};
|
2021-04-28 03:29:09 +01:00
|
|
|
|
2021-04-28 04:32:50 +01:00
|
|
|
# Add the overrides from lxd distrobuilder
|
|
|
|
systemd.extraConfig = ''
|
|
|
|
[Service]
|
|
|
|
ProtectProc=default
|
|
|
|
ProtectControlGroups=no
|
|
|
|
ProtectKernelTunables=no
|
|
|
|
'';
|
|
|
|
|
2021-04-28 03:29:09 +01:00
|
|
|
# Allow the user to login as root without password.
|
|
|
|
users.users.root.initialHashedPassword = mkOverride 150 "";
|
|
|
|
|
2021-04-30 23:23:45 +01:00
|
|
|
system.activationScripts.installInitScript = mkForce ''
|
|
|
|
ln -fs $systemConfig/init /sbin/init
|
|
|
|
'';
|
|
|
|
|
2021-04-28 03:29:09 +01:00
|
|
|
# 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;
|
|
|
|
};
|
2014-12-11 21:58:17 +00:00
|
|
|
}
|