2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.dockerRegistry;
|
|
|
|
|
|
|
|
blobCache = if cfg.enableRedisCache
|
|
|
|
then "redis"
|
|
|
|
else "inmemory";
|
|
|
|
|
|
|
|
registryConfig = {
|
|
|
|
version = "0.1";
|
|
|
|
log.fields.service = "registry";
|
|
|
|
storage = {
|
|
|
|
cache.blobdescriptor = blobCache;
|
|
|
|
delete.enabled = cfg.enableDelete;
|
|
|
|
} // (if cfg.storagePath != null
|
|
|
|
then { filesystem.rootdirectory = cfg.storagePath; }
|
|
|
|
else {});
|
|
|
|
http = {
|
|
|
|
addr = "${cfg.listenAddress}:${builtins.toString cfg.port}";
|
|
|
|
headers.X-Content-Type-Options = ["nosniff"];
|
|
|
|
};
|
|
|
|
health.storagedriver = {
|
|
|
|
enabled = true;
|
|
|
|
interval = "10s";
|
|
|
|
threshold = 3;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
registryConfig.redis = mkIf cfg.enableRedisCache {
|
|
|
|
addr = "${cfg.redisUrl}";
|
|
|
|
password = "${cfg.redisPassword}";
|
|
|
|
db = 0;
|
|
|
|
dialtimeout = "10ms";
|
|
|
|
readtimeout = "10ms";
|
|
|
|
writetimeout = "10ms";
|
|
|
|
pool = {
|
|
|
|
maxidle = 16;
|
|
|
|
maxactive = 64;
|
|
|
|
idletimeout = "300s";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
configFile = pkgs.writeText "docker-registry-config.yml" (builtins.toJSON (recursiveUpdate registryConfig cfg.extraConfig));
|
|
|
|
|
|
|
|
in {
|
|
|
|
options.services.dockerRegistry = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "Docker Registry");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
listenAddress = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Docker registry host or ip to bind to.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "127.0.0.1";
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Docker registry port to bind to.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = 5000;
|
2021-07-14 22:03:04 +00:00
|
|
|
type = types.port;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
storagePath = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = "/var/lib/docker-registry";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Docker registry storage path for the filesystem storage backend. Set to
|
|
|
|
null to configure another backend via extraConfig.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableDelete = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Enable delete for manifests and blobs.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
enableRedisCache = mkEnableOption (lib.mdDoc "redis as blob cache");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
redisUrl = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "localhost:6379";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Set redis host and port.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
redisPassword = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Set redis password.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Docker extra registry configuration via environment variables.
|
|
|
|
'';
|
|
|
|
default = {};
|
|
|
|
type = types.attrs;
|
|
|
|
};
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
enableGarbageCollect = mkEnableOption (lib.mdDoc "garbage collect");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
garbageCollectDates = mkOption {
|
|
|
|
default = "daily";
|
|
|
|
type = types.str;
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specification (in the format described by
|
2022-08-21 13:32:41 +00:00
|
|
|
{manpage}`systemd.time(7)`) of the time at
|
2020-04-24 23:36:52 +00:00
|
|
|
which the garbage collect will occur.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.docker-registry = {
|
|
|
|
description = "Docker Container Registry";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
script = ''
|
|
|
|
${pkgs.docker-distribution}/bin/registry serve ${configFile}
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
User = "docker-registry";
|
|
|
|
WorkingDirectory = cfg.storagePath;
|
|
|
|
AmbientCapabilities = mkIf (cfg.port < 1024) "cap_net_bind_service";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.docker-registry-garbage-collect = {
|
|
|
|
description = "Run Garbage Collection for docker registry";
|
|
|
|
|
|
|
|
restartIfChanged = false;
|
|
|
|
unitConfig.X-StopOnRemoval = false;
|
|
|
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
|
|
|
script = ''
|
|
|
|
${pkgs.docker-distribution}/bin/registry garbage-collect ${configFile}
|
2020-05-29 06:06:01 +00:00
|
|
|
/run/current-system/systemd/bin/systemctl restart docker-registry.service
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
startAt = optional cfg.enableGarbageCollect cfg.garbageCollectDates;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users.docker-registry =
|
|
|
|
(if cfg.storagePath != null
|
|
|
|
then {
|
|
|
|
createHome = true;
|
|
|
|
home = cfg.storagePath;
|
|
|
|
}
|
|
|
|
else {}) // {
|
2021-09-18 10:52:07 +00:00
|
|
|
group = "docker-registry";
|
2020-04-24 23:36:52 +00:00
|
|
|
isSystemUser = true;
|
|
|
|
};
|
2021-09-18 10:52:07 +00:00
|
|
|
users.groups.docker-registry = {};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
}
|