@@ -5,6 +5,45 @@ with lib;
55let
66 cfg = config.services.dockerRegistry;
77
8+ blobCache = if cfg.enableRedisCache
9+ then "redis"
10+ else "inmemory";
11+
12+ registryConfig = {
13+ version = "0.1";
14+ log.fields.service = "registry";
15+ storage = {
16+ cache.blobdescriptor = blobCache;
17+ filesystem.rootdirectory = cfg.storagePath;
18+ delete.enabled = cfg.enableDelete;
19+ };
20+ http = {
21+ addr = ":${builtins.toString cfg.port}";
22+ headers.X-Content-Type-Options = ["nosniff"];
23+ };
24+ health.storagedriver = {
25+ enabled = true;
26+ interval = "10s";
27+ threshold = 3;
28+ };
29+ };
30+
31+ registryConfig.redis = mkIf cfg.enableRedisCache {
32+ addr = "${cfg.redisUrl}";
33+ password = "${cfg.redisPassword}";
34+ db = 0;
35+ dialtimeout = "10ms";
36+ readtimeout = "10ms";
37+ writetimeout = "10ms";
38+ pool = {
39+ maxidle = 16;
40+ maxactive = 64;
41+ idletimeout = "300s";
42+ };
43+ };
44+
45+ configFile = pkgs.writeText "docker-registry-config.yml" (builtins.toJSON (registryConfig // cfg.extraConfig));
46+
847in {
948 options.services.dockerRegistry = {
1049 enable = mkEnableOption "Docker Registry";
@@ -27,6 +66,26 @@ in {
2766 description = "Docker registry storage path.";
2867 };
2968
69+ enableDelete = mkOption {
70+ type = types.bool;
71+ default = false;
72+ description = "Enable delete for manifests and blobs.";
73+ };
74+
75+ enableRedisCache = mkEnableOption "redis as blob cache";
76+
77+ redisUrl = mkOption {
78+ type = types.str;
79+ default = "localhost:6379";
80+ description = "Set redis host and port.";
81+ };
82+
83+ redisPassword = mkOption {
84+ type = types.str;
85+ default = "";
86+ description = "Set redis password.";
87+ };
88+
3089 extraConfig = mkOption {
3190 description = ''
3291 Docker extra registry configuration via environment variables.
@@ -34,6 +93,19 @@ in {
3493 default = {};
3594 type = types.attrsOf types.str;
3695 };
96+
97+ enableGarbageCollect = mkEnableOption "garbage collect";
98+
99+ garbageCollectDates = mkOption {
100+ default = "daily";
101+ type = types.str;
102+ description = ''
103+ Specification (in the format described by
104+ <citerefentry><refentrytitle>systemd.time</refentrytitle>
105+ <manvolnum>7</manvolnum></citerefentry>) of the time at
106+ which the garbage collect will occur.
107+ '';
108+ };
37109 };
38110
39111 config = mkIf cfg.enable {
@@ -41,15 +113,8 @@ in {
41113 description = "Docker Container Registry";
42114 wantedBy = [ "multi-user.target" ];
43115 after = [ "network.target" ];
44−
45− environment = {
46− REGISTRY_HTTP_ADDR = "${cfg.listenAddress}:${toString cfg.port}";
47− REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY = cfg.storagePath;
48− } // cfg.extraConfig;
49−
50116 script = ''
51− ${pkgs.docker-distribution}/bin/registry serve \
52− ${pkgs.docker-distribution.out}/share/go/src/github.com/docker/distribution/cmd/registry/config-example.yml
117+ ${pkgs.docker-distribution}/bin/registry serve ${configFile}
53118 '';
54119
55120 serviceConfig = {
@@ -58,6 +123,22 @@ in {
58123 };
59124 };
60125
126+ systemd.services.docker-registry-garbage-collect = {
127+ description = "Run Garbage Collection for docker registry";
128+
129+ restartIfChanged = false;
130+ unitConfig.X-StopOnRemoval = false;
131+
132+ serviceConfig.Type = "oneshot";
133+
134+ script = ''
135+ ${pkgs.docker-distribution}/bin/registry garbage-collect ${configFile}
136+ ${pkgs.systemd}/bin/systemctl restart docker-registry.service
137+ '';
138+
139+ startAt = optional cfg.enableGarbageCollect cfg.garbageCollectDates;
140+ };
141+
61142 users.extraUsers.docker-registry = {
62143 createHome = true;
63144 home = cfg.storagePath;