1 { config, lib, pkgs, ... }:
 
   5   cfg = config.services.sourcehut;
 
   9   rcfg = config.services.redis;
 
  10   drv = pkgs.sourcehut.hgsrht;
 
  13   options.services.sourcehut.hg = {
 
  28         Port on which the "hg" module should listen.
 
  36         PostgreSQL database name for hg.sr.ht.
 
  40     statePath = mkOption {
 
  42       default = "${cfg.statePath}/hgsrht";
 
  44         State path for hg.sr.ht.
 
  48     cloneBundles = mkOption {
 
  52         Generate clonebundles (which require more disk space but dramatically speed up cloning large repositories).
 
  57   config = with scfg; lib.mkIf (cfg.enable && elem "hg" cfg.services) {
 
  58     # In case it ever comes into being
 
  59     environment.etc."ssh/hgsrht-dispatch" = {
 
  62         #! ${pkgs.stdenv.shell}
 
  63         ${cfg.python}/bin/gitsrht-dispatch $@
 
  67     environment.systemPackages = [ pkgs.mercurial ];
 
  74           # Assuming hg.sr.ht needs this too
 
  76           description = "hg.sr.ht user";
 
  86       cron.systemCronJobs = [ "*/20 * * * * ${cfg.python}/bin/hgsrht-periodic" ]
 
  87         ++ optional cloneBundles "0 * * * * ${cfg.python}/bin/hgsrht-clonebundles";
 
  89       openssh.authorizedKeysCommand = ''/etc/ssh/hgsrht-dispatch "%u" "%h" "%t" "%k"'';
 
  90       openssh.authorizedKeysCommandUser = "root";
 
  91       openssh.extraConfig = ''
 
  92         PermitUserEnvironment SRHT_*
 
  97           local ${database} ${user} trust
 
  99         ensureDatabases = [ database ];
 
 103             ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
 
 111         # /var/log is owned by root
 
 112         "f /var/log/hg-srht-shell 0644 ${user} ${user} -"
 
 114         "d ${statePath} 0750 ${user} ${user} -"
 
 115         "d ${cfg.settings."${iniKey}".repos} 2755 ${user} ${user} -"
 
 118       services.hgsrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey {
 
 119         after = [ "redis.service" "postgresql.service" "network.target" ];
 
 120         requires = [ "redis.service" "postgresql.service" ];
 
 121         wantedBy = [ "multi-user.target" ];
 
 123         path = [ pkgs.mercurial ];
 
 124         description = "hg.sr.ht website service";
 
 126         serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";
 
 130     services.sourcehut.settings = {
 
 131       # URL hg.sr.ht is being served at (protocol://domain)
 
 132       "hg.sr.ht".origin = mkDefault "http://hg.${cfg.originBase}";
 
 133       # Address and port to bind the debug server to
 
 134       "hg.sr.ht".debug-host = mkDefault "0.0.0.0";
 
 135       "hg.sr.ht".debug-port = mkDefault port;
 
 136       # Configures the SQLAlchemy connection string for the database.
 
 137       "hg.sr.ht".connection-string = mkDefault "postgresql:///${database}?user=${user}&host=/var/run/postgresql";
 
 138       # The redis connection used for the webhooks worker
 
 139       "hg.sr.ht".webhooks = mkDefault "redis://${rcfg.bind}:${toString rcfg.port}/1";
 
 140       # A post-update script which is installed in every mercurial repo.
 
 141       "hg.sr.ht".changegroup-script = mkDefault "${cfg.python}/bin/hgsrht-hook-changegroup";
 
 142       # hg.sr.ht's OAuth client ID and secret for meta.sr.ht
 
 143       # Register your client at meta.example.org/oauth
 
 144       "hg.sr.ht".oauth-client-id = mkDefault null;
 
 145       "hg.sr.ht".oauth-client-secret = mkDefault null;
 
 146       # Path to mercurial repositories on disk
 
 147       "hg.sr.ht".repos = mkDefault "/var/lib/hg";
 
 148       # Path to the srht mercurial extension
 
 149       # (defaults to where the hgsrht code is)
 
 150       # "hg.sr.ht".srhtext = mkDefault null;
 
 151       # .hg/store size (in MB) past which the nightly job generates clone bundles.
 
 152       # "hg.sr.ht".clone_bundle_threshold = mkDefault 50;
 
 153       # Path to hg-ssh (if not in $PATH)
 
 154       # "hg.sr.ht".hg_ssh = mkDefault /path/to/hg-ssh;
 
 156       # The authorized keys hook uses this to dispatch to various handlers
 
 157       # The format is a program to exec into as the key, and the user to match as the
 
 158       # value. When someone tries to log in as this user, this program is executed
 
 159       # and is expected to omit an AuthorizedKeys file.
 
 161       # Uncomment the relevant lines to enable the various sr.ht dispatchers.
 
 162       "hg.sr.ht::dispatch"."/run/current-system/sw/bin/hgsrht-keys" = mkDefault "${user}:${user}";
 
 165     # TODO: requires testing and addition of hg-specific requirements
 
 166     services.nginx.virtualHosts."hg.${cfg.originBase}" = {
 
 168       locations."/".proxyPass = "http://${cfg.address}:${toString port}";
 
 169       locations."/query".proxyPass = "http://${cfg.address}:${toString (port + 100)}";
 
 170       locations."/static".root = "${pkgs.sourcehut.hgsrht}/${pkgs.sourcehut.python.sitePackages}/hgsrht";