gogs.nix 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. {
  2. config,
  3. lib,
  4. pkgs,
  5. ...
  6. }:
  7. let
  8. cfg = config.services.gogs;
  9. iniFormat = pkgs.formats.ini { };
  10. configFile = iniFormat.generate "gogs.ini" cfg.settings;
  11. in
  12. {
  13. options.services.gogs = {
  14. enable = lib.mkEnableOption "Gogs Git service";
  15. package = lib.mkPackageOption pkgs "gogs" { };
  16. user = lib.mkOption {
  17. type = lib.types.str;
  18. default = "gogs";
  19. description = "User account under which Gogs runs.";
  20. };
  21. group = lib.mkOption {
  22. type = lib.types.str;
  23. default = "gogs";
  24. description = "Group under which Gogs runs.";
  25. };
  26. stateDir = lib.mkOption {
  27. type = lib.types.str;
  28. default = "/var/lib/gogs";
  29. description = "Persistent data directory.";
  30. };
  31. environmentFile = lib.mkOption {
  32. type = lib.types.nullOr lib.types.path;
  33. default = null;
  34. description = ''
  35. File containing environment variables to pass to the Gogs service,
  36. formatted as VARIABLE=VALUE per line. Values set here are merged into
  37. the service's environment and can be used to pass secrets (e.g.
  38. database passwords) without putting them in the Nix store.
  39. '';
  40. };
  41. settings = lib.mkOption {
  42. type = iniFormat.type;
  43. default = { };
  44. example = lib.literalExpression ''
  45. {
  46. server = {
  47. DOMAIN = "git.example.com";
  48. ROOT_URL = "https://git.example.com/";
  49. HTTP_PORT = 3000;
  50. };
  51. database = {
  52. TYPE = "sqlite3";
  53. PATH = "/var/lib/gogs/data/gogs.db";
  54. };
  55. }
  56. '';
  57. description = ''
  58. Settings written to app.ini.
  59. See:
  60. https://gogs.io/docs/advanced/configuration_cheat_sheet
  61. '';
  62. };
  63. };
  64. config = lib.mkIf cfg.enable {
  65. users.users.${cfg.user} = {
  66. isSystemUser = true;
  67. group = cfg.group;
  68. home = cfg.stateDir;
  69. createHome = true;
  70. };
  71. users.groups.${cfg.group} = { };
  72. systemd.tmpfiles.rules = [
  73. "d ${cfg.stateDir} 0750 ${cfg.user} ${cfg.group} -"
  74. "d ${cfg.stateDir}/repositories 0750 ${cfg.user} ${cfg.group} -"
  75. "d ${cfg.stateDir}/data 0750 ${cfg.user} ${cfg.group} -"
  76. "d ${cfg.stateDir}/log 0750 ${cfg.user} ${cfg.group} -"
  77. ];
  78. systemd.services.gogs = {
  79. description = "Gogs Git Service";
  80. after = [ "network.target" ];
  81. wantedBy = [ "multi-user.target" ];
  82. serviceConfig = {
  83. Type = "simple";
  84. User = cfg.user;
  85. Group = cfg.group;
  86. WorkingDirectory = cfg.stateDir;
  87. ExecStart = "${lib.getExe cfg.package} web --config ${configFile}";
  88. Restart = "on-failure";
  89. EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
  90. NoNewPrivileges = true;
  91. PrivateTmp = true;
  92. ProtectSystem = "strict";
  93. ProtectHome = true;
  94. ReadWritePaths = [
  95. cfg.stateDir
  96. ];
  97. };
  98. };
  99. };
  100. }