summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorMaciej Krüger <mkg20001@gmail.com>2021-04-28 04:55:09 +0200
committerMaciej Krüger <mkg20001@gmail.com>2021-11-03 07:49:51 +0100
commitcaabd8933c90bf2d46a1121568bd42ffe1654bc1 (patch)
treeab60b254f980bda0b3843486d7103d85eb919d2f /nixos
parentb9df9ca2c4bd21f89a1fcbfc3a1c9f65077fada2 (diff)
downloadnixpkgs-caabd8933c90bf2d46a1121568bd42ffe1654bc1.tar
nixpkgs-caabd8933c90bf2d46a1121568bd42ffe1654bc1.tar.gz
nixpkgs-caabd8933c90bf2d46a1121568bd42ffe1654bc1.tar.bz2
nixpkgs-caabd8933c90bf2d46a1121568bd42ffe1654bc1.tar.lz
nixpkgs-caabd8933c90bf2d46a1121568bd42ffe1654bc1.tar.xz
nixpkgs-caabd8933c90bf2d46a1121568bd42ffe1654bc1.tar.zst
nixpkgs-caabd8933c90bf2d46a1121568bd42ffe1654bc1.zip
virtualisation.lxc: add support for lxd templates
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/virtualisation/lxc-container.nix86
1 files changed, 75 insertions, 11 deletions
diff --git a/nixos/modules/virtualisation/lxc-container.nix b/nixos/modules/virtualisation/lxc-container.nix
index 3b70ed14f55..21824c72a8b 100644
--- a/nixos/modules/virtualisation/lxc-container.nix
+++ b/nixos/modules/virtualisation/lxc-container.nix
@@ -1,37 +1,101 @@
-{ lib, ... }:
+{ lib, config, pkgs, ... }:
 
 with lib;
 
+let
+  templateSubmodule = { ... }: {
+    options = {
+      enable = mkEnableOption "this template";
+
+      target = mkOption {
+        description = "Path in the container";
+        type = types.path;
+      };
+      template = mkOption {
+        description = ".tpl file for rendering the target";
+        type = types.path;
+      };
+      when = mkOption {
+        description = "Events which trigger a rewrite (create, copy)";
+        type = types.listOf (types.str);
+      };
+      properties = mkOption {
+        description = "Additional properties";
+        type = types.attrs;
+        default = {};
+      };
+    };
+  };
+
+  toYAML = name: attrs: pkgs.runCommandNoCC name {
+    preferLocalBuild = true;
+    json = builtins.toFile "${name}.json" (builtins.toJSON attrs);
+    nativeBuildInputs = [ pkgs.remarshal ];
+  } "json2yaml -i $json -o $out";
+
+  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
 {
   imports = [
     ../profiles/docker-container.nix # FIXME, shouldn't include something from profiles/
   ];
 
+  options = {
+    virtualisation.lxc = {
+      templates = mkOption {
+        description = "Templates for LXD";
+        type = types.attrsOf (types.submodule (templateSubmodule));
+      };
+    };
+  };
+
   config = {
     system.build.tarball = mkForce (pkgs.callPackage ../../lib/make-system-tarball.nix {
       extraArgs = "--owner=0";
 
       storeContents = [
-        config.system.build.toplevel
+        {
+          object = config.system.build.toplevel;
+          symlink = "none";
+        }
       ];
 
       contents = [
         {
-          source = pkgs.writeText "metadata.yaml" ''
-            architecture: ${builtins.elemAt (builtins.match "^([a-z0-9_]+).+" (toString pkgs.system)) 0}
-            creation_date: 0
-            properties:
-              description: NixOS ${config.system.nixos.codeName} ${config.system.nixos.label} ${pkgs.system}
-              os: nixos
-              release: ${config.system.nixos.codeName}
-          '';
+          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;
+          };
           target = "/metadata.yaml";
         }
         {
           source = config.system.build.toplevel + "/init";
           target = "/sbin/init";
         }
-      ];
+      ] ++ templates.files;
 
       extraCommands = "mkdir -p proc sys dev";
     });