Skip to content

Commit f13cb7c

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 b940d43 commit f13cb7c

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

modules/programs/docker-cli.nix

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

35+
contexts = mkOption {
36+
type = lib.types.listOf (
37+
lib.types.submodule {
38+
freeformType = jsonFormat.type;
39+
options.Name = lib.mkOption {
40+
type = lib.types.str;
41+
description = "The name of the Docker context";
42+
};
43+
}
44+
);
45+
default = [ ];
46+
example = lib.literalExpression ''
47+
[
48+
{
49+
Name = "example";
50+
Metadata = {
51+
Description = "example1";
52+
53+
};
54+
Endpoints = {
55+
docker = {
56+
Host = "unix://example2";
57+
};
58+
};
59+
}
60+
];
61+
'';
62+
description = ''
63+
Array of docker context configurations. See:
64+
<https://docs.docker.com/engine/manage-resources/contexts/
65+
'';
66+
};
67+
3568
settings = mkOption {
3669
type = jsonFormat.type;
3770
default = { };
@@ -62,7 +95,15 @@ in
6295
"${cfg.configDir}/config.json" = {
6396
source = jsonFormat.generate "config.json" cfg.settings;
6497
};
65-
};
98+
}
99+
// builtins.listToAttrs (
100+
map (s: {
101+
name = "${cfg.configDir}/contexts/meta/${builtins.hashString "sha256" s.Name}/meta.json";
102+
value = {
103+
source = jsonFormat.generate "config.json" s;
104+
};
105+
}) cfg.contexts
106+
);
66107
};
67108
};
68109
}
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
config,
3+
...
4+
}:
5+
{
6+
programs.docker-cli = {
7+
enable = true;
8+
9+
configDir = ".docker2";
10+
11+
contexts = [
12+
{
13+
Name = "example";
14+
Metadata = {
15+
Description = "example1";
16+
17+
};
18+
Endpoints = {
19+
docker = {
20+
Host = "unix://example2";
21+
};
22+
};
23+
}
24+
];
25+
};
26+
27+
nmt.script =
28+
let
29+
cfgDocker = config.programs.docker-cli;
30+
configTestPath = "home-files/${cfgDocker.configDir}/contexts/meta/50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c/meta.json";
31+
in
32+
''
33+
assertPathNotExists home-files/.docker/config.json
34+
assertFileExists ${configTestPath}
35+
assertFileContent ${configTestPath} \
36+
${./example-contexts.json}
37+
'';
38+
}

0 commit comments

Comments
 (0)