1{ lib, config, pkgs, ... }:
2
3let
4 templateSubmodule = {...}: {
5 options = {
6 enable = lib.mkEnableOption "this template";
7
8 target = lib.mkOption {
9 description = "Path in the container";
10 type = lib.types.path;
11 };
12 template = lib.mkOption {
13 description = ".tpl file for rendering the target";
14 type = lib.types.path;
15 };
16 when = lib.mkOption {
17 description = "Events which trigger a rewrite (create, copy)";
18 type = lib.types.listOf (lib.types.str);
19 };
20 properties = lib.mkOption {
21 description = "Additional properties";
22 type = lib.types.attrs;
23 default = {};
24 };
25 };
26 };
27
28 toYAML = name: data: pkgs.writeText name (lib.generators.toYAML {} data);
29
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);
34 in
35 {
36 files = map (tpl: {
37 source = tpl.template;
38 target = "/templates/${tpl.name}.tpl";
39 }) list;
40 properties = lib.listToAttrs (map (tpl: lib.nameValuePair tpl.target {
41 when = tpl.when;
42 template = "${tpl.name}.tpl";
43 properties = tpl.properties;
44 }) list);
45 }
46 else { files = []; properties = {}; };
47
48in {
49 options = {
50 virtualisation.lxc = {
51 templates = lib.mkOption {
52 description = "Templates for LXD";
53 type = lib.types.attrsOf (lib.types.submodule templateSubmodule);
54 default = {};
55 example = lib.literalExpression ''
56 {
57 # create /etc/hostname on container creation
58 "hostname" = {
59 enable = true;
60 target = "/etc/hostname";
61 template = builtins.writeFile "hostname.tpl" "{{ container.name }}";
62 when = [ "create" ];
63 };
64 # create /etc/nixos/hostname.nix with a configuration for keeping the hostname applied
65 "hostname-nix" = {
66 enable = true;
67 target = "/etc/nixos/hostname.nix";
68 template = builtins.writeFile "hostname-nix.tpl" "{ ... }: { networking.hostName = "{{ container.name }}"; }";
69 # copy keeps the file updated when the container is changed
70 when = [ "create" "copy" ];
71 };
72 # copy allow the user to specify a custom configuration.nix
73 "configuration-nix" = {
74 enable = true;
75 target = "/etc/nixos/configuration.nix";
76 template = builtins.writeFile "configuration-nix" "{{ config_get(\"user.user-data\", properties.default) }}";
77 when = [ "create" ];
78 };
79 };
80 '';
81 };
82 };
83 };
84
85 config = {
86 system.build.metadata = pkgs.callPackage ../../lib/make-system-tarball.nix {
87 contents = [
88 {
89 source = toYAML "metadata.yaml" {
90 architecture = builtins.elemAt (builtins.match "^([a-z0-9_]+).+" (toString pkgs.stdenv.hostPlatform.system)) 0;
91 creation_date = 1;
92 properties = {
93 description = "${config.system.nixos.distroName} ${config.system.nixos.codeName} ${config.system.nixos.label} ${pkgs.stdenv.hostPlatform.system}";
94 os = "${config.system.nixos.distroId}";
95 release = "${config.system.nixos.codeName}";
96 };
97 templates = templates.properties;
98 };
99 target = "/metadata.yaml";
100 }
101 ] ++ templates.files;
102 };
103 };
104}