at 25.11-pre 4.2 kB view raw
1{ 2 lib, 3 config, 4 pkgs, 5 ... 6}: 7 8let 9 templateSubmodule = 10 { ... }: 11 { 12 options = { 13 enable = lib.mkEnableOption "this template"; 14 15 target = lib.mkOption { 16 description = "Path in the container"; 17 type = lib.types.path; 18 }; 19 template = lib.mkOption { 20 description = ".tpl file for rendering the target"; 21 type = lib.types.path; 22 }; 23 when = lib.mkOption { 24 description = "Events which trigger a rewrite (create, copy)"; 25 type = lib.types.listOf (lib.types.str); 26 }; 27 properties = lib.mkOption { 28 description = "Additional properties"; 29 type = lib.types.attrs; 30 default = { }; 31 }; 32 }; 33 }; 34 35 toYAML = name: data: pkgs.writeText name (lib.generators.toYAML { } data); 36 37 cfg = config.virtualisation.lxc; 38 templates = 39 if cfg.templates != { } then 40 let 41 list = lib.mapAttrsToList (name: value: { inherit name; } // value) ( 42 lib.filterAttrs (name: value: value.enable) cfg.templates 43 ); 44 in 45 { 46 files = map (tpl: { 47 source = tpl.template; 48 target = "/templates/${tpl.name}.tpl"; 49 }) list; 50 properties = lib.listToAttrs ( 51 map ( 52 tpl: 53 lib.nameValuePair tpl.target { 54 when = tpl.when; 55 template = "${tpl.name}.tpl"; 56 properties = tpl.properties; 57 } 58 ) list 59 ); 60 } 61 else 62 { 63 files = [ ]; 64 properties = { }; 65 }; 66 67in 68{ 69 imports = [ 70 ../image/file-options.nix 71 ]; 72 73 meta = { 74 maintainers = lib.teams.lxc.members; 75 }; 76 77 options = { 78 virtualisation.lxc = { 79 templates = lib.mkOption { 80 description = "Templates for LXD"; 81 type = lib.types.attrsOf (lib.types.submodule templateSubmodule); 82 default = { }; 83 example = lib.literalExpression '' 84 { 85 # create /etc/hostname on container creation 86 "hostname" = { 87 enable = true; 88 target = "/etc/hostname"; 89 template = builtins.writeFile "hostname.tpl" "{{ container.name }}"; 90 when = [ "create" ]; 91 }; 92 # create /etc/nixos/hostname.nix with a configuration for keeping the hostname applied 93 "hostname-nix" = { 94 enable = true; 95 target = "/etc/nixos/hostname.nix"; 96 template = builtins.writeFile "hostname-nix.tpl" "{ ... }: { networking.hostName = "{{ container.name }}"; }"; 97 # copy keeps the file updated when the container is changed 98 when = [ "create" "copy" ]; 99 }; 100 # copy allow the user to specify a custom configuration.nix 101 "configuration-nix" = { 102 enable = true; 103 target = "/etc/nixos/configuration.nix"; 104 template = builtins.writeFile "configuration-nix" "{{ config_get(\"user.user-data\", properties.default) }}"; 105 when = [ "create" ]; 106 }; 107 }; 108 ''; 109 }; 110 }; 111 }; 112 113 config = { 114 system.nixos.tags = [ 115 "lxc" 116 "metadata" 117 ]; 118 image.extension = "tar.xz"; 119 image.filePath = "tarball/${config.image.fileName}"; 120 system.build.image = config.system.build.metadata; 121 system.build.metadata = pkgs.callPackage ../../lib/make-system-tarball.nix { 122 fileName = config.image.baseName; 123 contents = [ 124 { 125 source = toYAML "metadata.yaml" { 126 architecture = builtins.elemAt (builtins.match "^([a-z0-9_]+).+" (toString pkgs.stdenv.hostPlatform.system)) 0; 127 creation_date = 1; 128 properties = { 129 description = "${config.system.nixos.distroName} ${config.system.nixos.codeName} ${config.system.nixos.label} ${pkgs.stdenv.hostPlatform.system}"; 130 os = "${config.system.nixos.distroId}"; 131 release = "${config.system.nixos.codeName}"; 132 }; 133 templates = templates.properties; 134 }; 135 target = "/metadata.yaml"; 136 } 137 ] ++ templates.files; 138 }; 139 }; 140}