Skip to content

Commit d504090

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 d4ee1bb commit d504090

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

modules/programs/docker-cli.nix

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

35+
contexts = mkOption {
36+
type = lib.types.attrsOf (
37+
lib.types.submodule {
38+
freeformType = jsonFormat.type;
39+
}
40+
);
41+
default = { };
42+
example = lib.literalExpression ''
43+
{
44+
example = {
45+
Metadata = { Description = "example1"; };
46+
Endpoints.docker.Host = "unix://example2";
47+
};
48+
}
49+
'';
50+
description = ''
51+
Attrset of docker context configurations keyed by context name. See:
52+
<https://docs.docker.com/engine/manage-resources/contexts/
53+
'';
54+
};
55+
3556
settings = mkOption {
3657
type = jsonFormat.type;
3758
default = { };
@@ -62,7 +83,20 @@ in
6283
"${cfg.configDir}/config.json" = {
6384
source = jsonFormat.generate "config.json" cfg.settings;
6485
};
65-
};
86+
}
87+
// lib.mapAttrs' (
88+
n: ctx:
89+
let
90+
name = if ctx ? Name then ctx.Name else n;
91+
path = "${cfg.configDir}/contexts/meta/${builtins.hashString "sha256" name}/meta.json";
92+
in
93+
{
94+
name = path;
95+
value = {
96+
source = jsonFormat.generate "config.json" (ctx // { Name = name; });
97+
};
98+
}
99+
) cfg.contexts;
66100
};
67101
};
68102
}
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)