1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00

docker-cli: add docker contexts support

Adds docker-cli.contexts support. This allows declarative configuration
of [docker
contexts](https://docs.docker.com/engine/manage-resources/contexts/).
This commit is contained in:
will 2025-09-28 09:45:58 +10:00 committed by Austin Horstman
parent 25ca7d297f
commit 990e5ce679
4 changed files with 93 additions and 1 deletions

View file

@ -32,6 +32,38 @@ in
'';
};
contexts = mkOption {
type = lib.types.attrsOf (
lib.types.submodule (
{ name, config, ... }:
{
freeformType = jsonFormat.type;
options = {
Name = mkOption {
type = lib.types.str;
readOnly = true;
description = "Name of the Docker context. Defaults to the attribute name (the <name> in programs.docker-cli.contexts.<name>). Overriding requires lib.mkForce.";
};
};
config.Name = name;
}
)
);
default = { };
example = lib.literalExpression ''
{
example = {
Metadata = { Description = "example1"; };
Endpoints.docker.Host = "unix://example2";
};
}
'';
description = ''
Attribute set of Docker context configurations. Each attribute name becomes the context Name; overriding requires lib.mkForce. See:
<https://docs.docker.com/engine/manage-resources/contexts/
'';
};
settings = mkOption {
type = jsonFormat.type;
default = { };
@ -62,7 +94,19 @@ in
"${cfg.configDir}/config.json" = {
source = jsonFormat.generate "config.json" cfg.settings;
};
};
}
// lib.mapAttrs' (
n: ctx:
let
path = "${cfg.configDir}/contexts/meta/${builtins.hashString "sha256" ctx.Name}/meta.json";
in
{
name = path;
value = {
source = jsonFormat.generate "config.json" (ctx);
};
}
) cfg.contexts;
};
};
}

View file

@ -1,4 +1,5 @@
{
docker-cli = ./example-config.nix;
docker-cli-empty-config = ./empty-config.nix;
docker-cli-contexts = ./example-contexts.nix;
}

View file

@ -0,0 +1,11 @@
{
"Endpoints": {
"docker": {
"Host": "unix://example2"
}
},
"Metadata": {
"Description": "example1"
},
"Name": "example"
}

View file

@ -0,0 +1,36 @@
{
config,
...
}:
{
programs.docker-cli = {
enable = true;
configDir = ".docker2";
contexts = {
example = {
Metadata = {
Description = "example1";
};
Endpoints = {
docker = {
Host = "unix://example2";
};
};
};
};
};
nmt.script =
let
cfgDocker = config.programs.docker-cli;
configTestPath = "home-files/${cfgDocker.configDir}/contexts/meta/50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c/meta.json";
in
''
assertPathNotExists home-files/.docker/config.json
assertFileExists ${configTestPath}
assertFileContent ${configTestPath} \
${./example-contexts.json}
'';
}