@@ -14,7 +14,7 @@ let
1414 HOST = ${cfg.database.host}:${toString cfg.database.port}
1515 NAME = ${cfg.database.name}
1616 USER = ${cfg.database.user}
17− PASSWD = ${cfg.database.password}
17+ PASSWD = #dbpass#
1818 PATH = ${cfg.database.path}
1919
2020 [repository]
@@ -26,6 +26,10 @@ let
2626 HTTP_PORT = ${toString cfg.httpPort}
2727 ROOT_URL = ${cfg.rootUrl}
2828
29+ [session]
30+ COOKIE_NAME = session
31+ COOKIE_SECURE = ${boolToString cfg.cookieSecure}
32+
2933 [security]
3034 SECRET_KEY = #secretkey#
3135 INSTALL_LOCK = true
@@ -102,7 +106,21 @@ in
102106 password = mkOption {
103107 type = types.str;
104108 default = "";
105− description = "Database password.";
109+ description = ''
110+ The password corresponding to <option>database.user</option>.
111+ Warning: this is stored in cleartext in the Nix store!
112+ Use <option>database.passwordFile</option> instead.
113+ '';
114+ };
115+
116+ passwordFile = mkOption {
117+ type = types.nullOr types.path;
118+ default = null;
119+ example = "/run/keys/gogs-dbpassword";
120+ description = ''
121+ A file containing the password corresponding to
122+ <option>database.user</option>.
123+ '';
106124 };
107125
108126 path = mkOption {
@@ -148,6 +166,15 @@ in
148166 description = "HTTP listen port.";
149167 };
150168
169+ cookieSecure = mkOption {
170+ type = types.bool;
171+ default = false;
172+ description = ''
173+ Marks session cookies as "secure" as a hint for browsers to only send
174+ them via HTTPS. This option is recommend, if Gogs is being served over HTTPS.
175+ '';
176+ };
177+
151178 extraConfig = mkOption {
152179 type = types.str;
153180 default = "";
@@ -164,13 +191,25 @@ in
164191 wantedBy = [ "multi-user.target" ];
165192 path = [ pkgs.gogs.bin ];
166193
167− preStart = ''
194+ preStart = let
195+ runConfig = "${cfg.stateDir}/custom/conf/app.ini";
196+ secretKey = "${cfg.stateDir}/custom/conf/secret_key";
197+ in ''
168198 # copy custom configuration and generate a random secret key if needed
169199 ${optionalString (cfg.useWizard == false) ''
170200 mkdir -p ${cfg.stateDir}/custom/conf
171− cp -f ${configFile} ${cfg.stateDir}/custom/conf/app.ini
172− KEY=$(head -c 16 /dev/urandom | tr -dc A-Za-z0-9)
173− sed -i "s,#secretkey#,$KEY,g" ${cfg.stateDir}/custom/conf/app.ini
201+ cp -f ${configFile} ${runConfig}
202+
203+ if [ ! -e ${secretKey} ]; then
204+ head -c 16 /dev/urandom | base64 > ${secretKey}
205+ fi
206+
207+ KEY=$(head -n1 ${secretKey})
208+ DBPASS=$(head -n1 ${cfg.database.passwordFile})
209+ sed -e "s,#secretkey#,$KEY,g" \
210+ -e "s,#dbpass#,$DBPASS,g" \
211+ -i ${runConfig}
212+ chmod 440 ${runConfig} ${secretKey}
174213 ''}
175214
176215 mkdir -p ${cfg.repositoryRoot}
@@ -212,5 +251,16 @@ in
212251 };
213252 extraGroups.gogs.gid = config.ids.gids.gogs;
214253 };
254+
255+ warnings = optional (cfg.database.password != "")
256+ ''config.services.gogs.database.password will be stored as plaintext
257+ in the Nix store. Use database.passwordFile instead.'';
258+
259+ # Create database passwordFile default when password is configured.
260+ services.gogs.database.passwordFile = mkIf (cfg.database.password != "")
261+ (mkDefault (toString (pkgs.writeTextFile {
262+ name = "gogs-database-password";
263+ text = cfg.database.password;
264+ })));
215265 };
216266}