@@ -19,7 +19,6 @@ let
1919 adapter = "postgresql";
2020 database = cfg.databaseName;
2121 host = cfg.databaseHost;
22− password = cfg.databasePassword;
2322 username = cfg.databaseUsername;
2423 encoding = "utf8";
2524 pool = cfg.databasePool;
@@ -66,13 +65,6 @@ let
6665
6766 redisConfig.production.url = "redis://localhost:6379/";
6867
69− secretsConfig.production = {
70− secret_key_base = cfg.secrets.secret;
71− otp_key_base = cfg.secrets.otp;
72− db_key_base = cfg.secrets.db;
73− openid_connect_signing_key = cfg.secrets.jws;
74− };
75−
7668 gitlabConfig = {
7769 # These are the default settings from config/gitlab.example.yml
7870 production = flip recursiveUpdate cfg.extraConfig {
@@ -180,7 +172,7 @@ let
180172 address: "${cfg.smtp.address}",
181173 port: ${toString cfg.smtp.port},
182174 ${optionalString (cfg.smtp.username != null) ''user_name: "${cfg.smtp.username}",''}
183− ${optionalString (cfg.smtp.password != null) ''password: "${cfg.smtp.password}",''}
175+ ${optionalString (cfg.smtp.passwordFile != null) ''password: "@smtpPassword@",''}
184176 domain: "${cfg.smtp.domain}",
185177 ${optionalString (cfg.smtp.authentication != null) "authentication: :${cfg.smtp.authentication},"}
186178 enable_starttls_auto: ${toString cfg.smtp.enableStartTLSAuto},
@@ -249,9 +241,15 @@ in {
249241 description = "Gitlab database hostname.";
250242 };
251243
252− databasePassword = mkOption {
253− type = types.str;
254− description = "Gitlab database user password.";
244+ databasePasswordFile = mkOption {
245+ type = with types; nullOr path;
246+ default = null;
247+ description = ''
248+ File containing the Gitlab database user password.
249+
250+ This should be a string, not a nix path, since nix paths are
251+ copied into the world-readable nix store.
252+ '';
255253 };
256254
257255 databaseName = mkOption {
@@ -339,10 +337,15 @@ in {
339337 '';
340338 };
341339
342− initialRootPassword = mkOption {
343− type = types.str;
340+ initialRootPasswordFile = mkOption {
341+ type = with types; nullOr path;
342+ default = null;
344343 description = ''
345− Initial password of the root account if this is a new install.
344+ File containing the initial password of the root account if
345+ this is a new install.
346+
347+ This should be a string, not a nix path, since nix paths are
348+ copied into the world-readable nix store.
346349 '';
347350 };
348351
@@ -366,15 +369,20 @@ in {
366369 };
367370
368371 username = mkOption {
369− type = types.nullOr types.str;
372+ type = with types; nullOr str;
370373 default = null;
371374 description = "Username of the SMTP server for Gitlab.";
372375 };
373376
374− password = mkOption {
375− type = types.nullOr types.str;
377+ passwordFile = mkOption {
378+ type = types.nullOr types.path;
376379 default = null;
377− description = "Password of the SMTP server for Gitlab.";
380+ description = ''
381+ File containing the password of the SMTP server for Gitlab.
382+
383+ This should be a string, not a nix path, since nix paths
384+ are copied into the world-readable nix store.
385+ '';
378386 };
379387
380388 domain = mkOption {
@@ -384,7 +392,7 @@ in {
384392 };
385393
386394 authentication = mkOption {
387− type = types.nullOr types.str;
395+ type = with types; nullOr str;
388396 default = null;
389397 description = "Authentitcation type to use, see http://api.rubyonrails.org/classes/ActionMailer/Base.html";
390398 };
@@ -402,52 +410,69 @@ in {
402410 };
403411 };
404412
405− secrets.secret = mkOption {
406− type = types.str;
413+ secrets.secretFile = mkOption {
414+ type = with types; nullOr path;
415+ default = null;
407416 description = ''
408− The secret is used to encrypt variables in the DB. If
409− you change or lose this key you will be unable to access variables
410− stored in database.
417+ A file containing the secret used to encrypt variables in
418+ the DB. If you change or lose this key you will be unable to
419+ access variables stored in database.
411420
412421 Make sure the secret is at least 30 characters and all random,
413422 no regular words or you'll be exposed to dictionary attacks.
423+
424+ This should be a string, not a nix path, since nix paths are
425+ copied into the world-readable nix store.
414426 '';
415427 };
416428
417− secrets.db = mkOption {
418− type = types.str;
429+ secrets.dbFile = mkOption {
430+ type = with types; nullOr path;
431+ default = null;
419432 description = ''
420− The secret is used to encrypt variables in the DB. If
421− you change or lose this key you will be unable to access variables
422− stored in database.
433+ A file containing the secret used to encrypt variables in
434+ the DB. If you change or lose this key you will be unable to
435+ access variables stored in database.
423436
424437 Make sure the secret is at least 30 characters and all random,
425438 no regular words or you'll be exposed to dictionary attacks.
439+
440+ This should be a string, not a nix path, since nix paths are
441+ copied into the world-readable nix store.
426442 '';
427443 };
428444
429− secrets.otp = mkOption {
430− type = types.str;
445+ secrets.otpFile = mkOption {
446+ type = with types; nullOr path;
447+ default = null;
431448 description = ''
432− The secret is used to encrypt secrets for OTP tokens. If
433− you change or lose this key, users which have 2FA enabled for login
434− won't be able to login anymore.
449+ A file containing the secret used to encrypt secrets for OTP
450+ tokens. If you change or lose this key, users which have 2FA
451+ enabled for login won't be able to login anymore.
435452
436453 Make sure the secret is at least 30 characters and all random,
437454 no regular words or you'll be exposed to dictionary attacks.
455+
456+ This should be a string, not a nix path, since nix paths are
457+ copied into the world-readable nix store.
438458 '';
439459 };
440460
441− secrets.jws = mkOption {
442− type = types.str;
461+ secrets.jwsFile = mkOption {
462+ type = with types; nullOr path;
463+ default = null;
443464 description = ''
444− The secret is used to encrypt session keys. If you change or lose
445− this key, users will be disconnected.
465+ A file containing the secret used to encrypt session
466+ keys. If you change or lose this key, users will be
467+ disconnected.
446468
447469 Make sure the secret is an RSA private key in PEM format. You can
448470 generate one with
449471
450472 openssl genrsa 2048
473+
474+ This should be a string, not a nix path, since nix paths are
475+ copied into the world-readable nix store.
451476 '';
452477 };
453478
@@ -471,6 +496,33 @@ in {
471496
472497 config = mkIf cfg.enable {
473498
499+ assertions = [
500+ {
501+ assertion = cfg.initialRootPasswordFile != null;
502+ message = "services.gitlab.initialRootPasswordFile must be set!";
503+ }
504+ {
505+ assertion = cfg.databasePasswordFile != null;
506+ message = "services.gitlab.databasePasswordFile must be set!";
507+ }
508+ {
509+ assertion = cfg.secrets.secretFile != null;
510+ message = "services.gitlab.secrets.secretFile must be set!";
511+ }
512+ {
513+ assertion = cfg.secrets.dbFile != null;
514+ message = "services.gitlab.secrets.dbFile must be set!";
515+ }
516+ {
517+ assertion = cfg.secrets.otpFile != null;
518+ message = "services.gitlab.secrets.otpFile must be set!";
519+ }
520+ {
521+ assertion = cfg.secrets.jwsFile != null;
522+ message = "services.gitlab.secrets.jwsFile must be set!";
523+ }
524+ ];
525+
474526 environment.systemPackages = [ pkgs.git gitlab-rake gitlab-rails cfg.packages.gitlab-shell ];
475527
476528 # Redis is required for the sidekiq queue runner.
@@ -529,13 +581,9 @@ in {
529581 "L+ /run/gitlab/shell-config.yml - - - - ${pkgs.writeText "config.yml" (builtins.toJSON gitlabShellConfig)}"
530582
531583 "L+ ${cfg.statePath}/config/gitlab.yml - - - - ${pkgs.writeText "gitlab.yml" (builtins.toJSON gitlabConfig)}"
532− "L+ ${cfg.statePath}/config/database.yml - - - - ${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)}"
533− "L+ ${cfg.statePath}/config/secrets.yml - - - - ${pkgs.writeText "secrets.yml" (builtins.toJSON secretsConfig)}"
534584 "L+ ${cfg.statePath}/config/unicorn.rb - - - - ${./defaultUnicornConfig.rb}"
535−
536585 "L+ ${cfg.statePath}/config/initializers/extra-gitlab.rb - - - - ${extraGitlabRb}"
537− ] ++ optional cfg.smtp.enable
538− "L+ ${cfg.statePath}/config/initializers/smtp_settings.rb - - - - ${smtpSettings}" ;
586+ ];
539587
540588 systemd.services.gitlab-sidekiq = {
541589 after = [ "network.target" "redis.service" "gitlab.service" ];
@@ -632,13 +680,53 @@ in {
632680 ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/config.dist/* ${cfg.statePath}/config
633681 ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/db/* ${cfg.statePath}/db
634682
635− ${pkgs.openssl}/bin/openssl rand -hex 32 > ${cfg.statePath}/gitlab_shell_secret
636−
637683 ${pkgs.sudo}/bin/sudo -u ${cfg.user} ${cfg.packages.gitlab-shell}/bin/install
638684
685+ ${optionalString cfg.smtp.enable ''
686+ install -o ${cfg.user} -g ${cfg.group} -m u=rw ${smtpSettings} ${cfg.statePath}/config/initializers/smtp_settings.rb
687+ ${optionalString (cfg.smtp.passwordFile != null) ''
688+ smtp_password=$(<'${cfg.smtp.passwordFile}')
689+ ${pkgs.replace}/bin/replace-literal -e '@smtpPassword@' "$smtp_password" '${cfg.statePath}/config/initializers/smtp_settings.rb'
690+ ''}
691+ ''}
692+
693+ (
694+ umask u=rwx,g=,o=
695+
696+ ${pkgs.openssl}/bin/openssl rand -hex 32 > ${cfg.statePath}/gitlab_shell_secret
697+
698+ if [[ -h '${cfg.statePath}/config/secrets.yml' ]]; then
699+ rm '${cfg.statePath}/config/secrets.yml'
700+ fi
701+
702+ export secret="$(<'${cfg.secrets.secretFile}')"
703+ export db="$(<'${cfg.secrets.dbFile}')"
704+ export otp="$(<'${cfg.secrets.otpFile}')"
705+ export jws="$(<'${cfg.secrets.jwsFile}')"
706+ ${pkgs.jq}/bin/jq -n '{production: {secret_key_base: $ENV.secret,
707+ otp_key_base: $ENV.db,
708+ db_key_base: $ENV.otp,
709+ openid_connect_signing_key: $ENV.jws}}' \
710+ > '${cfg.statePath}/config/secrets.yml'
711+
712+ chown ${cfg.user}:${cfg.group} '${cfg.statePath}/config/secrets.yml' '${cfg.statePath}/gitlab_shell_secret'
713+ )
714+
715+ export db_password="$(<'${cfg.databasePasswordFile}')"
716+
717+ if [[ -z "$db_password" ]]; then
718+ >&2 echo "Database password was an empty string!"
719+ exit 1
720+ fi
721+
722+ ${pkgs.jq}/bin/jq <${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)} \
723+ '.production.password = $ENV.db_password' \
724+ >'${cfg.statePath}/config/database.yml'
725+ chown ${cfg.user}:${cfg.group} '${cfg.statePath}/config/database.yml'
726+
639727 if ! test -e "${cfg.statePath}/db-created"; then
640728 if [ "${cfg.databaseHost}" = "127.0.0.1" ]; then
641− ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} psql postgres -c "CREATE ROLE ${cfg.databaseUsername} WITH LOGIN NOCREATEDB NOCREATEROLE ENCRYPTED PASSWORD '${cfg.databasePassword}'"
729+ ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} psql postgres -c "CREATE ROLE ${cfg.databaseUsername} WITH LOGIN NOCREATEDB NOCREATEROLE ENCRYPTED PASSWORD '$db_password'"
642730 ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} ${config.services.postgresql.package}/bin/createdb --owner ${cfg.databaseUsername} ${cfg.databaseName}
643731
644732 # enable required pg_trgm extension for gitlab
@@ -654,8 +742,9 @@ in {
654742 ${pkgs.sudo}/bin/sudo -u ${cfg.user} -H ${gitlab-rake}/bin/gitlab-rake db:migrate
655743
656744 if ! test -e "${cfg.statePath}/db-seeded"; then
745+ initial_root_password="$(<'${cfg.initialRootPasswordFile}')"
657746 ${pkgs.sudo}/bin/sudo -u ${cfg.user} ${gitlab-rake}/bin/gitlab-rake db:seed_fu \
658− GITLAB_ROOT_PASSWORD='${cfg.initialRootPassword}' GITLAB_ROOT_EMAIL='${cfg.initialRootEmail}'
747+ GITLAB_ROOT_PASSWORD="$initial_root_password" GITLAB_ROOT_EMAIL='${cfg.initialRootEmail}'
659748 ${pkgs.sudo}/bin/sudo -u ${cfg.user} touch "${cfg.statePath}/db-seeded"
660749 fi
661750