1 { lib, config, pkgs, ... }:
4 templateSubmodule = {...}: {
6 enable = lib.mkEnableOption "this template";
8 target = lib.mkOption {
9 description = "Path in the container";
10 type = lib.types.path;
12 template = lib.mkOption {
13 description = ".tpl file for rendering the target";
14 type = lib.types.path;
17 description = "Events which trigger a rewrite (create, copy)";
18 type = lib.types.listOf (lib.types.str);
20 properties = lib.mkOption {
21 description = "Additional properties";
22 type = lib.types.attrs;
28 toYAML = name: data: pkgs.writeText name (lib.generators.toYAML {} data);
30 cfg = config.virtualisation.lxc;
31 templates = if cfg.templates != {} then let
32 list = lib.mapAttrsToList (name: value: { inherit name; } // value)
33 (lib.filterAttrs (name: value: value.enable) cfg.templates);
37 source = tpl.template;
38 target = "/templates/${tpl.name}.tpl";
40 properties = lib.listToAttrs (map (tpl: lib.nameValuePair tpl.target {
42 template = "${tpl.name}.tpl";
43 properties = tpl.properties;
46 else { files = []; properties = {}; };
50 maintainers = lib.teams.lxc.members;
54 virtualisation.lxc = {
55 templates = lib.mkOption {
56 description = "Templates for LXD";
57 type = lib.types.attrsOf (lib.types.submodule templateSubmodule);
59 example = lib.literalExpression ''
61 # create /etc/hostname on container creation
64 target = "/etc/hostname";
65 template = builtins.writeFile "hostname.tpl" "{{ container.name }}";
68 # create /etc/nixos/hostname.nix with a configuration for keeping the hostname applied
71 target = "/etc/nixos/hostname.nix";
72 template = builtins.writeFile "hostname-nix.tpl" "{ ... }: { networking.hostName = "{{ container.name }}"; }";
73 # copy keeps the file updated when the container is changed
74 when = [ "create" "copy" ];
76 # copy allow the user to specify a custom configuration.nix
77 "configuration-nix" = {
79 target = "/etc/nixos/configuration.nix";
80 template = builtins.writeFile "configuration-nix" "{{ config_get(\"user.user-data\", properties.default) }}";
90 system.build.metadata = pkgs.callPackage ../../lib/make-system-tarball.nix {
93 source = toYAML "metadata.yaml" {
94 architecture = builtins.elemAt (builtins.match "^([a-z0-9_]+).+" (toString pkgs.stdenv.hostPlatform.system)) 0;
97 description = "${config.system.nixos.distroName} ${config.system.nixos.codeName} ${config.system.nixos.label} ${pkgs.stdenv.hostPlatform.system}";
98 os = "${config.system.nixos.distroId}";
99 release = "${config.system.nixos.codeName}";
101 templates = templates.properties;
103 target = "/metadata.yaml";
105 ] ++ templates.files;