<commit_before>{ 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 storage 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;
  };
}
<commit_msg>nixos/docker-registry: docker independant docker registry<commit_after>{ 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 storage 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;
        WorkingDirectory = cfg.storagePath;
      };

      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 = {
      createHome = true;
      home = cfg.storagePath;
      uid = config.ids.uids.docker-registry;
    };
  };
}

{'fraction_non_alphanumeric': 0.13517179023508138, 'fraction_numerical': 0.005198915009041591, 'mean_word_length': 2.190338860850757, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5036770', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1313, 'n_words': 283}