Changed option namespace for better compatibility

This commit is contained in:
pazpi 2024-08-27 17:49:31 +02:00
parent fe8045ee55
commit 8b25f46384
26 changed files with 407 additions and 232 deletions

View file

@ -1,4 +1,3 @@
# Save this as podman-pod.nix
{
config,
lib,
@ -9,44 +8,67 @@
with lib;
let
cfg = config.services.podmanPods;
cfg = config.my.virtualisation.podmanPods;
# Get the options from the original oci-containers module
containerOptions = (
filterAttrs (
n: v: n != "definition"
) config.virtualisation.oci-containers.containers.type.getSubOptions
);
# containerOptions = (
# filterAttrs (
# n: v: n != "definition"
# ) config.virtualisation.oci-containers.containers.type.getSubOptions
# );
# containerDefinition = mkOption {
# type = types.attrs;
# description = "The container definition, derived from virtualisation.oci-containers.containers.";
# example = {
# image = "nginx:latest";
# ports = [ "8080:80" ];
# };
# default = { };
# };
# containerDefinition = mkOption {
# type = types.attrs;
# description = "The container definition, derived from virtualisation.oci-containers.containers.";
# example = {
# image = "nginx:latest";
# ports = [ "8080:80" ];
# };
# default = {};
# };
# # config.virtualisation.oci-containers.containers.type.getSubOptions;
containerDefinition = config.virtualisation.oci-containers.containers.type.getSubOptions;
# Add our enable option
extendedContainerOptions = containerOptions // {
extendedContainerOptions = containerDefinition // {
enable = mkEnableOption "Enable this container";
};
podOptions =
{ name, config, ... }:
{
options = {
podOptions = {
options = with types; {
name = mkOption {
type = types.str;
description = "Name of the pod";
};
name = mkOption {
type = str;
description = "Name of the pod";
};
ports = mkOption {
type = types.listOf types.str;
default = [ ];
description = "List of port mappings (e.g. ['8080:80'])";
};
containers = mkOption {
type = types.attrsOf (types.submodule { options = extendedContainerOptions; });
default = { };
description = "Attribute set of OCI container configurations for this set";
};
ports = mkOption {
type = listOf str;
default = [ ];
description = "List of port mappings (e.g. ['8080:80'])";
};
containers = mkOption {
type = attrsOf (submodule {
options = extendedContainerOptions;
});
default = { };
description = "Attribute set of OCI container configurations for this set";
};
};
};
createPodScript =
name: podDef:
@ -82,27 +104,49 @@ let
fi
'';
enabledContainers = lib.flatten (
mapAttrs (
podName: podConfig: filterAttrs (name: value: value.enable or true) podConfig.containers
) cfg
);
# enabledContainers = lib.flatten (
# mapAttrs (
# podName: podConfig: filterAttrs (name: value: value.enable or true) podConfig.containers
# ) cfg
# );
# flattenPodContainers =
# pods:
# mapAttrs' (
# podName: pod:
# mapAttrs' (
# containerName: container:
# nameValuePair "${podName}-${containerName}" (
# container
# # // {
# # inherit (pod) name;
# # extraOptions = (container.extraOptions or [ ]) ++ [ "--pod=${pod.name}" ];
# # }
# )
# ) (filterAttrs (n: v: v.enable or true) pod.containers)
# ) pods;
in
{
options.services.podmanPods = mkOption {
options.my.virtualisation.podmanPods = mkOption {
type = types.attrsOf (types.submodule podOptions);
default = { };
description = "Podman pods to create";
};
config = mkIf (cfg != { }) {
virtualisation.podman.enable = true;
virtualisation.oci-containers.backend = "podman";
my.virtualisation.podman.enable = true;
environment.systemPackages = [ pkgs.jq ];
virtualisation.oci-containers.containers = enabledContainers;
# virtualisation.oci-containers.containers = enabledContainers;
# virtualisation.oci-containers.containers = flattenPodContainers cfg;
# virtualisation.oci-containers.containers = lib.mkMerge (
# map (entry: entry.container) (filter (entry: entry.enable) cfg.containers)
# );
# virtualisation.oci-containers.containers = listToAttrs (flatten (mapAttrsToList
# (podName: podDef:
@ -159,6 +203,7 @@ in
wantedBy = [ "multi-user.target" ];
}
) cfg;
containerServices = mapAttrs' (
name: container:
nameValuePair "podman-${name}" {
@ -166,8 +211,9 @@ in
requires = [ "podman-pod-${lib.head (lib.splitString "-" name)}.service" ];
partOf = [ "podman-pod-${lib.head (lib.splitString "-" name)}.service" ];
}
) config.virtualisation.oci-containers.containers;
) config.containers;
in
podServices // containerServices;
};
}