about summary refs log tree commit diff
path: root/nixos/modules/services/misc/docker-registry.nix
blob: 67580a1c6277291a130079e90cf689267c21815f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.dockerRegistry;

in {
  ###### interface

  options.services.dockerRegistry = {
    enable = mkOption {
      description = "Whether to enable docker registry server.";
      default = false;
      type = types.bool;
    };

    host = mkOption {
      description = "Docker registry host or ip to bind to.";
      default = "127.0.0.1";
      type = types.str;
    };

    port = mkOption {
      description = "Docker registry port to bind to.";
      default = 5000;
      type = types.int;
    };

    storagePath = mkOption {
      type = types.path;
      default = "/var/lib/docker/registry";
      description = "Docker registry strorage path.";
    };

    extraConfig = mkOption {
      description = ''
        Docker extra registry configuration. See
        <link xlink:href="https://github.com/docker/docker-registry/blob/master/config/config_sample.yml"/>
      '';
      default = {};
      type = types.attrsOf types.str;
    };
  };

  config = mkIf cfg.enable {
    systemd.services.docker-registry = {
      description = "Docker Container Registry";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      environment = {
        REGISTRY_HOST = cfg.host;
        REGISTRY_PORT = toString cfg.port;
        GUNICORN_OPTS = "[--preload]"; # see https://github.com/docker/docker-registry#sqlalchemy
        STORAGE_PATH = cfg.storagePath;
      } // cfg.extraConfig;

      serviceConfig = {
        ExecStart = "${pkgs.pythonPackages.docker_registry}/bin/docker-registry";
        User = "docker-registry";
        Group = "docker";
        PermissionsStartOnly = true;
      };

      preStart = ''
        mkdir -p ${cfg.storagePath}
        if [ "$(id -u)" = 0 ]; then
          chown -R docker-registry:docker ${cfg.storagePath}
        fi
      '';
      postStart = ''
        until ${pkgs.curl}/bin/curl -s -o /dev/null 'http://${cfg.host}:${toString cfg.port}/'; do
          sleep 1;
        done
      '';
    };

    users.extraGroups.docker.gid = mkDefault config.ids.gids.docker;
    users.extraUsers.docker-registry.uid = config.ids.uids.docker-registry;
  };
}