Skip to content

Commit 24729e9

Browse files
committed
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/).
1 parent 4721427 commit 24729e9

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

modules/programs/docker-cli.nix

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,38 @@ in
3232
'';
3333
};
3434

35+
contexts = mkOption {
36+
type = lib.types.attrsOf (
37+
lib.types.submodule (
38+
{ name, config, ... }:
39+
{
40+
freeformType = jsonFormat.type;
41+
options = {
42+
Name = mkOption {
43+
type = lib.types.str;
44+
defaultText = lib.literalExpression "name";
45+
description = "Name of the Docker context.";
46+
};
47+
};
48+
config.Name = name;
49+
}
50+
)
51+
);
52+
default = { };
53+
example = lib.literalExpression ''
54+
{
55+
example = {
56+
Metadata = { Description = "example1"; };
57+
Endpoints.docker.Host = "unix://example2";
58+
};
59+
}
60+
'';
61+
description = ''
62+
Attrset of docker context configurations keyed by context name. See:
63+
<https://docs.docker.com/engine/manage-resources/contexts/
64+
'';
65+
};
66+
3567
settings = mkOption {
3668
type = jsonFormat.type;
3769
default = { };
@@ -62,7 +94,19 @@ in
6294
"${cfg.configDir}/config.json" = {
6395
source = jsonFormat.generate "config.json" cfg.settings;
6496
};
65-
};
97+
}
98+
// lib.mapAttrs' (
99+
n: ctx:
100+
let
101+
path = "${cfg.configDir}/contexts/meta/${builtins.hashString "sha256" ctx.Name}/meta.json";
102+
in
103+
{
104+
name = path;
105+
value = {
106+
source = jsonFormat.generate "config.json" (ctx);
107+
};
108+
}
109+
) cfg.contexts;
66110
};
67111
};
68112
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
docker-cli = ./example-config.nix;
33
docker-cli-empty-config = ./empty-config.nix;
4+
docker-cli-contexts = ./example-contexts.nix;
45
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Endpoints": {
3+
"docker": {
4+
"Host": "unix://example2"
5+
}
6+
},
7+
"Metadata": {
8+
"Description": "example1"
9+
},
10+
"Name": "example"
11+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
config,
3+
...
4+
}:
5+
{
6+
programs.docker-cli = {
7+
enable = true;
8+
9+
configDir = ".docker2";
10+
11+
contexts = {
12+
example = {
13+
Metadata = {
14+
Description = "example1";
15+
};
16+
Endpoints = {
17+
docker = {
18+
Host = "unix://example2";
19+
};
20+
};
21+
};
22+
};
23+
};
24+
25+
nmt.script =
26+
let
27+
cfgDocker = config.programs.docker-cli;
28+
configTestPath = "home-files/${cfgDocker.configDir}/contexts/meta/50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c/meta.json";
29+
in
30+
''
31+
assertPathNotExists home-files/.docker/config.json
32+
assertFileExists ${configTestPath}
33+
assertFileContent ${configTestPath} \
34+
${./example-contexts.json}
35+
'';
36+
}

0 commit comments

Comments
 (0)