You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.2KB

  1. {
  2. description = "NixOS Gitea server";
  3. inputs = {
  4. nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
  5. };
  6. outputs = { self, nixpkgs, ... }: {
  7. nixosModules.gitea = { config, lib, pkgs, ... }:
  8. let
  9. cfg = config.gitea;
  10. # https://github.com/majewsky/nixos-modules/blob/master/gitea.nix
  11. ldapOptions = let
  12. inherit (config.services) openldap;
  13. in {
  14. name = "ldap";
  15. security-protocol = "LDAPS";
  16. host = "localhost";
  17. port = "389";
  18. bind-dn = "uid=${openldap.services.gitea.uid},ou=services,dc=${openldap.organization},dc=${openldap.extension}";
  19. bind-password = openldap.services.gitea.passwordFile;
  20. user-search-base = "ou=people,dc=${openldap.organization},dc=${openldap.extension}";
  21. user-filter = "(&(objectclass=*)(|(uniqueIdentifier=%[1]s)(mail=%[1]s)))";
  22. #admin-filter = "(isMemberOf=cn=gitea-admins,ou=groups,${ldap.suffix})";
  23. username-attribute = "uniqueIdentifier";
  24. firstname-attribute = "givenName";
  25. surname-attribute = "sn";
  26. email-attribute = "mail";
  27. };
  28. ldapFlags = "--attributes-in-bind --synchronize-users";
  29. in
  30. {
  31. options.gitea = {
  32. enable = lib.mkOption {type = lib.types.bool;};
  33. hostName = lib.mkOption {type = lib.types.str;};
  34. sshPort = lib.mkOption {type = lib.types.ints.unsigned;};
  35. httpPort = lib.mkOption {type = lib.types.ints.unsigned;};
  36. };
  37. config = lib.mkIf cfg.enable {
  38. services.gitea = {
  39. enable = true;
  40. database = {
  41. type = "sqlite3";
  42. createDatabase = true;
  43. };
  44. stateDir = "/var/lib/gitea";
  45. settings = {
  46. server = {
  47. SSH_PORT = cfg.sshPort;
  48. HTTP_PORT = cfg.httpPort;
  49. };
  50. };
  51. };
  52. # nginx virtual host
  53. services.nginx.virtualHosts.${cfg.hostName} = {
  54. enableACME = true;
  55. acmeRoot = null;
  56. addSSL = true;
  57. # directs traffic to the appropriate port
  58. locations."/" = {
  59. proxyPass = "http://localhost:${cfg.httpPort}";
  60. proxyWebsockets = true;
  61. };
  62. };
  63. # LDAP authentication cannot be set up declaratively, so we have to do it
  64. # at the end of the preStart script
  65. #
  66. # WARNING: This assumes that the LDAP auth source has the internal ID 1.
  67. systemd.services.gitea.preStart = let
  68. giteaBin = "${pkgs.gitea}/bin/gitea";
  69. formatOption = key: value: "--${key} ${lib.strings.escapeShellArg value}";
  70. ldapOptionsStrs = lib.mapAttrsToList formatOption ldapOptions;
  71. ldapOptionsStr = lib.concatStringsSep " " ldapOptionsStrs;
  72. in lib.mkAfter ''
  73. if ${giteaBin} admin auth list | grep -q ${ldapOptions.name}; then
  74. ${giteaBin} admin auth update-ldap --id 1 ${ldapOptionsStr} ${ldapFlags}
  75. else
  76. ${giteaBin} admin auth add-ldap ${ldapOptionsStr} ${ldapFlags}
  77. fi
  78. '';
  79. };
  80. };
  81. };
  82. }