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 virtualisation.lxc = {
51 templates = lib.mkOption {
52 description = "Templates for LXD";
53 type = lib.types.attrsOf (lib.types.submodule templateSubmodule);
55 example = lib.literalExpression ''
57 # create /etc/hostname on container creation
60 target = "/etc/hostname";
61 template = builtins.writeFile "hostname.tpl" "{{ container.name }}";
64 # create /etc/nixos/hostname.nix with a configuration for keeping the hostname applied
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" ];
72 # copy allow the user to specify a custom configuration.nix
73 "configuration-nix" = {
75 target = "/etc/nixos/configuration.nix";
76 template = builtins.writeFile "configuration-nix" "{{ config_get(\"user.user-data\", properties.default) }}";
86 system.build.metadata = pkgs.callPackage ../../lib/make-system-tarball.nix {
89 source = toYAML "metadata.yaml" {
90 architecture = builtins.elemAt (builtins.match "^([a-z0-9_]+).+" (toString pkgs.system)) 0;
93 description = "${config.system.nixos.distroName} ${config.system.nixos.codeName} ${config.system.nixos.label} ${pkgs.system}";
94 os = "${config.system.nixos.distroId}";
95 release = "${config.system.nixos.codeName}";
97 templates = templates.properties;
99 target = "/metadata.yaml";
101 ] ++ templates.files;