1 { config, lib, options, pkgs, ... }:
4 cfg = config.services.forgejo;
5 opt = options.services.forgejo;
6 format = pkgs.formats.ini { };
8 exe = lib.getExe cfg.package;
10 pg = config.services.postgresql;
11 useMysql = cfg.database.type == "mysql";
12 usePostgresql = cfg.database.type == "postgres";
13 useSqlite = cfg.database.type == "sqlite3";
16 mkSecret = section: values: lib.mapAttrsToList (key: value: {
17 env = envEscape "FORGEJO__${section}__${key}__FILE";
20 # https://codeberg.org/forgejo/forgejo/src/tag/v7.0.2/contrib/environment-to-ini/environment-to-ini.go
21 envEscape = string: lib.replaceStrings [ "." "-" ] [ "_0X2E_" "_0X2D_" ] (lib.strings.toUpper string);
22 in lib.flatten (lib.mapAttrsToList mkSecret cfg.secrets);
43 (mkRenamedOptionModule [ "services" "forgejo" "appName" ] [ "services" "forgejo" "settings" "DEFAULT" "APP_NAME" ])
44 (mkRemovedOptionModule [ "services" "forgejo" "extraConfig" ] "services.forgejo.extraConfig has been removed. Please use the freeform services.forgejo.settings option instead")
45 (mkRemovedOptionModule [ "services" "forgejo" "database" "password" ] "services.forgejo.database.password has been removed. Please use services.forgejo.database.passwordFile instead")
46 (mkRenamedOptionModule [ "services" "forgejo" "mailerPasswordFile" ] [ "services" "forgejo" "secrets" "mailer" "PASSWD" ])
48 # copied from services.gitea; remove at some point
49 (mkRenamedOptionModule [ "services" "forgejo" "cookieSecure" ] [ "services" "forgejo" "settings" "session" "COOKIE_SECURE" ])
50 (mkRenamedOptionModule [ "services" "forgejo" "disableRegistration" ] [ "services" "forgejo" "settings" "service" "DISABLE_REGISTRATION" ])
51 (mkRenamedOptionModule [ "services" "forgejo" "domain" ] [ "services" "forgejo" "settings" "server" "DOMAIN" ])
52 (mkRenamedOptionModule [ "services" "forgejo" "httpAddress" ] [ "services" "forgejo" "settings" "server" "HTTP_ADDR" ])
53 (mkRenamedOptionModule [ "services" "forgejo" "httpPort" ] [ "services" "forgejo" "settings" "server" "HTTP_PORT" ])
54 (mkRenamedOptionModule [ "services" "forgejo" "log" "level" ] [ "services" "forgejo" "settings" "log" "LEVEL" ])
55 (mkRenamedOptionModule [ "services" "forgejo" "log" "rootPath" ] [ "services" "forgejo" "settings" "log" "ROOT_PATH" ])
56 (mkRenamedOptionModule [ "services" "forgejo" "rootUrl" ] [ "services" "forgejo" "settings" "server" "ROOT_URL" ])
57 (mkRenamedOptionModule [ "services" "forgejo" "ssh" "clonePort" ] [ "services" "forgejo" "settings" "server" "SSH_PORT" ])
58 (mkRenamedOptionModule [ "services" "forgejo" "staticRootPath" ] [ "services" "forgejo" "settings" "server" "STATIC_ROOT_PATH" ])
59 (mkChangedOptionModule [ "services" "forgejo" "enableUnixSocket" ] [ "services" "forgejo" "settings" "server" "PROTOCOL" ] (
60 config: if config.services.forgejo.enableUnixSocket then "http+unix" else "http"
62 (mkRemovedOptionModule [ "services" "forgejo" "ssh" "enable" ] "services.forgejo.ssh.enable has been migrated into freeform setting services.forgejo.settings.server.DISABLE_SSH. Keep in mind that the setting is inverted")
67 enable = mkEnableOption "Forgejo, a software forge";
69 package = mkPackageOption pkgs "forgejo-lts" { };
71 useWizard = mkOption {
75 Whether to use the built-in installation wizard instead of
76 declaratively managing the {file}`app.ini` config file in nix.
81 default = "/var/lib/forgejo";
83 description = "Forgejo data directory.";
86 customDir = mkOption {
87 default = "${cfg.stateDir}/custom";
88 defaultText = literalExpression ''"''${config.${opt.stateDir}}/custom"'';
91 Base directory for custom templates and other options.
93 If {option}`${opt.useWizard}` is disabled (default), this directory will also
94 hold secrets and the resulting {file}`app.ini` config at runtime.
101 description = "User account under which Forgejo runs.";
107 description = "Group under which Forgejo runs.";
112 type = types.enum [ "sqlite3" "mysql" "postgres" ];
115 description = "Database engine to use.";
120 default = "127.0.0.1";
121 description = "Database host address.";
126 default = if usePostgresql then pg.settings.port else 3306;
127 defaultText = literalExpression ''
128 if config.${opt.database.type} != "postgresql"
132 description = "Database host port.";
138 description = "Database name.";
144 description = "Database user.";
147 passwordFile = mkOption {
148 type = types.nullOr types.path;
150 example = "/run/keys/forgejo-dbpassword";
152 A file containing the password corresponding to
153 {option}`${opt.database.user}`.
158 type = types.nullOr types.path;
159 default = if (cfg.database.createDatabase && usePostgresql) then "/run/postgresql" else if (cfg.database.createDatabase && useMysql) then "/run/mysqld/mysqld.sock" else null;
160 defaultText = literalExpression "null";
161 example = "/run/mysqld/mysqld.sock";
162 description = "Path to the unix socket file to use for authentication.";
167 default = "${cfg.stateDir}/data/forgejo.db";
168 defaultText = literalExpression ''"''${config.${opt.stateDir}}/data/forgejo.db"'';
169 description = "Path to the sqlite3 database file.";
172 createDatabase = mkOption {
175 description = "Whether to create a local database automatically.";
180 enable = mkEnableOption "periodic dumps via the [built-in {command}`dump` command](https://forgejo.org/docs/latest/admin/command-line/#dump)";
182 interval = mkOption {
187 Run a Forgejo dump at this interval. Runs by default at 04:31 every day.
189 The format is described in
190 {manpage}`systemd.time(7)`.
194 backupDir = mkOption {
196 default = "${cfg.stateDir}/dump";
197 defaultText = literalExpression ''"''${config.${opt.stateDir}}/dump"'';
198 description = "Path to the directory where the dump archives will be stored.";
202 type = types.enum [ "zip" "tar" "tar.sz" "tar.gz" "tar.xz" "tar.bz2" "tar.br" "tar.lz4" "tar.zst" ];
204 description = "Archive format used to store the dump file.";
208 type = types.nullOr types.str;
210 description = "Filename to be used for the dump. If `null` a default name is chosen by forgejo.";
211 example = "forgejo-dump";
219 description = "Enables git-lfs support.";
222 contentDir = mkOption {
224 default = "${cfg.stateDir}/data/lfs";
225 defaultText = literalExpression ''"''${config.${opt.stateDir}}/data/lfs"'';
226 description = "Where to store LFS files.";
230 repositoryRoot = mkOption {
232 default = "${cfg.stateDir}/repositories";
233 defaultText = literalExpression ''"''${config.${opt.stateDir}}/repositories"'';
234 description = "Path to the git repositories.";
237 settings = mkOption {
240 Free-form settings written directly to the `app.ini` configfile file.
241 Refer to <https://forgejo.org/docs/latest/admin/config-cheat-sheet/> for supported values.
243 example = literalExpression ''
248 "cron.sync_external_users" = {
250 SCHEDULE = "@every 24h";
251 UPDATE_EXISTING = true;
255 MAILER_TYPE = "sendmail";
256 FROM = "do-not-reply@example.org";
257 SENDMAIL_PATH = "''${pkgs.system-sendmail}/bin/sendmail";
260 SHOW_FOOTER_VERSION = false;
264 type = types.submodule {
265 freeformType = format.type;
268 ROOT_PATH = mkOption {
269 default = "${cfg.stateDir}/log";
270 defaultText = literalExpression ''"''${config.${opt.stateDir}}/log"'';
272 description = "Root path for log files.";
276 type = types.enum [ "Trace" "Debug" "Info" "Warn" "Error" "Critical" ];
277 description = "General log level.";
282 PROTOCOL = mkOption {
283 type = types.enum [ "http" "https" "fcgi" "http+unix" "fcgi+unix" ];
285 description = ''Listen protocol. `+unix` means "over unix", not "in addition to."'';
288 HTTP_ADDR = mkOption {
289 type = types.either types.str types.path;
290 default = if lib.hasSuffix "+unix" cfg.settings.server.PROTOCOL then "/run/forgejo/forgejo.sock" else "0.0.0.0";
291 defaultText = literalExpression ''if lib.hasSuffix "+unix" cfg.settings.server.PROTOCOL then "/run/forgejo/forgejo.sock" else "0.0.0.0"'';
292 description = "Listen address. Must be a path when using a unix socket.";
295 HTTP_PORT = mkOption {
298 description = "Listen port. Ignored when using a unix socket.";
303 default = "localhost";
304 description = "Domain name of your server.";
307 ROOT_URL = mkOption {
309 default = "http://${cfg.settings.server.DOMAIN}:${toString cfg.settings.server.HTTP_PORT}/";
310 defaultText = literalExpression ''"http://''${config.services.forgejo.settings.server.DOMAIN}:''${toString config.services.forgejo.settings.server.HTTP_PORT}/"'';
311 description = "Full public URL of Forgejo server.";
314 STATIC_ROOT_PATH = mkOption {
315 type = types.either types.str types.path;
316 default = cfg.package.data;
317 defaultText = literalExpression "config.${opt.package}.data";
318 example = "/var/lib/forgejo/data";
319 description = "Upper level of template and static files path.";
322 DISABLE_SSH = mkOption {
325 description = "Disable external SSH feature.";
328 SSH_PORT = mkOption {
333 SSH port displayed in clone URL.
334 The option is required to configure a service when the external visible port
335 differs from the local listening port i.e. if port forwarding is used.
341 COOKIE_SECURE = mkOption {
345 Marks session cookies as "secure" as a hint for browsers to only send
346 them via HTTPS. This option is recommend, if Forgejo is being served over HTTPS.
357 This is a small wrapper over systemd's `LoadCredential`.
359 It takes the same sections and keys as {option}`services.forgejo.settings`,
360 but the value of each key is a path instead of a string or bool.
362 The path is then loaded as credential, exported as environment variable
363 and then feed through
364 <https://codeberg.org/forgejo/forgejo/src/branch/forgejo/contrib/environment-to-ini/environment-to-ini.go>.
366 It does the required environment variable escaping for you.
369 Keys specified here take priority over the ones in {option}`services.forgejo.settings`!
372 example = literalExpression ''
375 TOKEN = "/run/keys/forgejo-metrics-token";
378 HMAC_KEY = "/run/keys/forgejo-camo-hmac";
381 HCAPTCHA_SECRET = "/run/keys/forgejo-hcaptcha-secret";
382 HCAPTCHA_SITEKEY = "/run/keys/forgejo-hcaptcha-sitekey";
386 type = types.submodule {
387 freeformType = with types; attrsOf (attrsOf path);
394 config = mkIf cfg.enable {
397 assertion = cfg.database.createDatabase -> useSqlite || cfg.database.user == cfg.user;
398 message = "services.forgejo.database.user must match services.forgejo.user if the database is to be automatically provisioned";
400 { assertion = cfg.database.createDatabase && usePostgresql -> cfg.database.user == cfg.database.name;
402 When creating a database via NixOS, the db user and db name must be equal!
403 If you already have an existing DB+user and this assertion is new, you can safely set
404 `services.forgejo.createDatabase` to `false` because removal of `ensureUsers`
405 and `ensureDatabases` doesn't have any effect.
410 services.forgejo.settings = {
412 RUN_MODE = mkDefault "prod";
413 RUN_USER = mkDefault cfg.user;
414 WORK_PATH = mkDefault cfg.stateDir;
419 DB_TYPE = cfg.database.type;
421 (mkIf (useMysql || usePostgresql) {
422 HOST = if cfg.database.socket != null then cfg.database.socket else cfg.database.host + ":" + toString cfg.database.port;
423 NAME = cfg.database.name;
424 USER = cfg.database.user;
427 PATH = cfg.database.path;
429 (mkIf usePostgresql {
430 SSL_MODE = "disable";
435 ROOT = cfg.repositoryRoot;
438 server = mkIf cfg.lfs.enable {
439 LFS_START_SERVER = true;
443 COOKIE_NAME = mkDefault "session";
450 lfs = mkIf cfg.lfs.enable {
451 PATH = cfg.lfs.contentDir;
455 services.forgejo.secrets = {
457 SECRET_KEY = "${cfg.customDir}/conf/secret_key";
458 INTERNAL_TOKEN = "${cfg.customDir}/conf/internal_token";
462 JWT_SECRET = "${cfg.customDir}/conf/oauth2_jwt_secret";
465 database = mkIf (cfg.database.passwordFile != null) {
466 PASSWD = cfg.database.passwordFile;
469 server = mkIf cfg.lfs.enable {
470 LFS_JWT_SECRET = "${cfg.customDir}/conf/lfs_jwt_secret";
474 services.postgresql = optionalAttrs (usePostgresql && cfg.database.createDatabase) {
475 enable = mkDefault true;
477 ensureDatabases = [ cfg.database.name ];
480 name = cfg.database.user;
481 ensureDBOwnership = true;
486 services.mysql = optionalAttrs (useMysql && cfg.database.createDatabase) {
487 enable = mkDefault true;
488 package = mkDefault pkgs.mariadb;
490 ensureDatabases = [ cfg.database.name ];
493 name = cfg.database.user;
494 ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; };
499 systemd.tmpfiles.rules = [
500 "d '${cfg.dump.backupDir}' 0750 ${cfg.user} ${cfg.group} - -"
501 "z '${cfg.dump.backupDir}' 0750 ${cfg.user} ${cfg.group} - -"
502 "d '${cfg.repositoryRoot}' 0750 ${cfg.user} ${cfg.group} - -"
503 "z '${cfg.repositoryRoot}' 0750 ${cfg.user} ${cfg.group} - -"
504 "d '${cfg.stateDir}' 0750 ${cfg.user} ${cfg.group} - -"
505 "d '${cfg.stateDir}/conf' 0750 ${cfg.user} ${cfg.group} - -"
506 "d '${cfg.customDir}' 0750 ${cfg.user} ${cfg.group} - -"
507 "d '${cfg.customDir}/conf' 0750 ${cfg.user} ${cfg.group} - -"
508 "d '${cfg.stateDir}/data' 0750 ${cfg.user} ${cfg.group} - -"
509 "d '${cfg.stateDir}/log' 0750 ${cfg.user} ${cfg.group} - -"
510 "z '${cfg.stateDir}' 0750 ${cfg.user} ${cfg.group} - -"
511 "z '${cfg.stateDir}/.ssh' 0700 ${cfg.user} ${cfg.group} - -"
512 "z '${cfg.stateDir}/conf' 0750 ${cfg.user} ${cfg.group} - -"
513 "z '${cfg.customDir}' 0750 ${cfg.user} ${cfg.group} - -"
514 "z '${cfg.customDir}/conf' 0750 ${cfg.user} ${cfg.group} - -"
515 "z '${cfg.stateDir}/data' 0750 ${cfg.user} ${cfg.group} - -"
516 "z '${cfg.stateDir}/log' 0750 ${cfg.user} ${cfg.group} - -"
518 # If we have a folder or symlink with Forgejo locales, remove it
519 # And symlink the current Forgejo locales in place
520 "L+ '${cfg.stateDir}/conf/locale' - - - - ${cfg.package.out}/locale"
522 ] ++ optionals cfg.lfs.enable [
523 "d '${cfg.lfs.contentDir}' 0750 ${cfg.user} ${cfg.group} - -"
524 "z '${cfg.lfs.contentDir}' 0750 ${cfg.user} ${cfg.group} - -"
527 systemd.services.forgejo-secrets = mkIf (!cfg.useWizard) {
528 description = "Forgejo secret bootstrap helper";
530 if [ ! -s '${cfg.secrets.security.SECRET_KEY}' ]; then
531 ${exe} generate secret SECRET_KEY > '${cfg.secrets.security.SECRET_KEY}'
534 if [ ! -s '${cfg.secrets.oauth2.JWT_SECRET}' ]; then
535 ${exe} generate secret JWT_SECRET > '${cfg.secrets.oauth2.JWT_SECRET}'
538 ${optionalString cfg.lfs.enable ''
539 if [ ! -s '${cfg.secrets.server.LFS_JWT_SECRET}' ]; then
540 ${exe} generate secret LFS_JWT_SECRET > '${cfg.secrets.server.LFS_JWT_SECRET}'
544 if [ ! -s '${cfg.secrets.security.INTERNAL_TOKEN}' ]; then
545 ${exe} generate secret INTERNAL_TOKEN > '${cfg.secrets.security.INTERNAL_TOKEN}'
550 RemainAfterExit = true;
553 ReadWritePaths = [ cfg.customDir ];
558 systemd.services.forgejo = {
559 description = "Forgejo (Beyond coding. We forge.)";
562 ] ++ optionals usePostgresql [
564 ] ++ optionals useMysql [
566 ] ++ optionals (!cfg.useWizard) [
567 "forgejo-secrets.service"
569 requires = optionals (cfg.database.createDatabase && usePostgresql) [
571 ] ++ optionals (cfg.database.createDatabase && useMysql) [
573 ] ++ optionals (!cfg.useWizard) [
574 "forgejo-secrets.service"
576 wantedBy = [ "multi-user.target" ];
577 path = [ cfg.package pkgs.git pkgs.gnupg ];
579 # In older versions the secret naming for JWT was kind of confusing.
580 # The file jwt_secret hold the value for LFS_JWT_SECRET and JWT_SECRET
581 # wasn't persistent at all.
582 # To fix that, there is now the file oauth2_jwt_secret containing the
583 # values for JWT_SECRET and the file jwt_secret gets renamed to
585 # We have to consider this to stay compatible with older installations.
588 ${optionalString (!cfg.useWizard) ''
589 function forgejo_setup {
590 config='${cfg.customDir}/conf/app.ini'
591 cp -f '${format.generate "app.ini" cfg.settings}' "$config"
594 ${lib.getExe' cfg.package "environment-to-ini"} --config "$config"
597 (umask 027; forgejo_setup)
600 # run migrations/init the database
603 # update all hooks' binary paths
604 ${exe} admin regenerate hooks
606 # update command option in authorized_keys
607 if [ -r ${cfg.stateDir}/.ssh/authorized_keys ]
609 ${exe} admin regenerate keys
617 WorkingDirectory = cfg.stateDir;
618 ExecStart = "${exe} web --pid /run/forgejo/forgejo.pid";
620 # Runtime directory and mode
621 RuntimeDirectory = "forgejo";
622 RuntimeDirectoryMode = "0755";
625 ProtectProc = "invisible";
626 # Access write directories
627 ReadWritePaths = [ cfg.customDir cfg.dump.backupDir cfg.repositoryRoot cfg.stateDir cfg.lfs.contentDir ];
630 CapabilityBoundingSet = "";
632 NoNewPrivileges = true;
634 ProtectSystem = "strict";
637 PrivateDevices = true;
639 ProtectHostname = true;
641 ProtectKernelTunables = true;
642 ProtectKernelModules = true;
643 ProtectKernelLogs = true;
644 ProtectControlGroups = true;
645 RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
646 RestrictNamespaces = true;
647 LockPersonality = true;
648 MemoryDenyWriteExecute = true;
649 RestrictRealtime = true;
650 RestrictSUIDSGID = true;
652 PrivateMounts = true;
653 # System Call Filtering
654 SystemCallArchitectures = "native";
655 SystemCallFilter = [ "~@cpu-emulation @debug @keyring @mount @obsolete @privileged @setuid" "setrlimit" ];
657 LoadCredential = map (e: "${e.env}:${e.path}") secrets;
663 # `GITEA_` prefix until https://codeberg.org/forgejo/forgejo/issues/497
665 GITEA_WORK_DIR = cfg.stateDir;
666 GITEA_CUSTOM = cfg.customDir;
667 } // lib.listToAttrs (map (e: lib.nameValuePair e.env "%d/${e.env}") secrets);
670 services.openssh.settings.AcceptEnv = mkIf (!cfg.settings.START_SSH_SERVER or false) "GIT_PROTOCOL";
672 users.users = mkIf (cfg.user == "forgejo") {
675 useDefaultShell = true;
681 users.groups = mkIf (cfg.group == "forgejo") {
685 systemd.services.forgejo-dump = mkIf cfg.dump.enable {
686 description = "forgejo dump";
687 after = [ "forgejo.service" ];
688 path = [ cfg.package ];
693 # `GITEA_` prefix until https://codeberg.org/forgejo/forgejo/issues/497
695 GITEA_WORK_DIR = cfg.stateDir;
696 GITEA_CUSTOM = cfg.customDir;
702 ExecStart = "${exe} dump --type ${cfg.dump.type}" + optionalString (cfg.dump.file != null) " --file ${cfg.dump.file}";
703 WorkingDirectory = cfg.dump.backupDir;
707 systemd.timers.forgejo-dump = mkIf cfg.dump.enable {
708 description = "Forgejo dump timer";
709 partOf = [ "forgejo-dump.service" ];
710 wantedBy = [ "timers.target" ];
711 timerConfig.OnCalendar = cfg.dump.interval;
715 meta.doc = ./forgejo.md;
716 meta.maintainers = with lib.maintainers; [ bendlas emilylange ];