@@ -1,4 +1,4 @@
1−{ config, lib, pkgs, ... }:
1+{ config, lib, pkgs, utils, ... }:
22
33# TODO: support non-postgresql
44
@@ -12,14 +12,12 @@ let
1212 gitlabSocket = "${cfg.statePath}/tmp/sockets/gitlab.socket";
1313 gitalySocket = "${cfg.statePath}/tmp/sockets/gitaly.socket";
1414 pathUrlQuote = url: replaceStrings ["/"] ["%2F"] url;
15− pgSuperUser = config.services.postgresql.superUser;
1615
1716 databaseConfig = {
1817 production = {
1918 adapter = "postgresql";
2019 database = cfg.databaseName;
2120 host = cfg.databaseHost;
22− password = cfg.databasePassword;
2321 username = cfg.databaseUsername;
2422 encoding = "utf8";
2523 pool = cfg.databasePool;
@@ -66,13 +64,6 @@ let
6664
6765 redisConfig.production.url = "redis://localhost:6379/";
6866
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−
7667 gitlabConfig = {
7768 # These are the default settings from config/gitlab.example.yml
7869 production = flip recursiveUpdate cfg.extraConfig {
@@ -180,10 +171,11 @@ let
180171 address: "${cfg.smtp.address}",
181172 port: ${toString cfg.smtp.port},
182173 ${optionalString (cfg.smtp.username != null) ''user_name: "${cfg.smtp.username}",''}
183− ${optionalString (cfg.smtp.password != null) ''password: "${cfg.smtp.password}",''}
174+ ${optionalString (cfg.smtp.passwordFile != null) ''password: "@smtpPassword@",''}
184175 domain: "${cfg.smtp.domain}",
185176 ${optionalString (cfg.smtp.authentication != null) "authentication: :${cfg.smtp.authentication},"}
186177 enable_starttls_auto: ${toString cfg.smtp.enableStartTLSAuto},
178+ ca_file: "/etc/ssl/certs/ca-certificates.crt",
187179 openssl_verify_mode: '${cfg.smtp.opensslVerifyMode}'
188180 }
189181 end
@@ -244,13 +236,33 @@ in {
244236
245237 databaseHost = mkOption {
246238 type = types.str;
247− default = "127.0.0.1";
248− description = "Gitlab database hostname.";
239+ default = "";
240+ description = ''
241+ Gitlab database hostname. An empty string means <quote>use
242+ local unix socket connection</quote>.
243+ '';
249244 };
250245
251− databasePassword = mkOption {
252− type = types.str;
253− description = "Gitlab database user password.";
246+ databasePasswordFile = mkOption {
247+ type = with types; nullOr path;
248+ default = null;
249+ description = ''
250+ File containing the Gitlab database user password.
251+
252+ This should be a string, not a nix path, since nix paths are
253+ copied into the world-readable nix store.
254+ '';
255+ };
256+
257+ databaseCreateLocally = mkOption {
258+ type = types.bool;
259+ default = true;
260+ description = ''
261+ Whether a database should be automatically created on the
262+ local host. Set this to <literal>false</literal> if you plan
263+ on provisioning a local database yourself or use an external
264+ one.
265+ '';
254266 };
255267
256268 databaseName = mkOption {
@@ -338,10 +350,15 @@ in {
338350 '';
339351 };
340352
341− initialRootPassword = mkOption {
342− type = types.str;
353+ initialRootPasswordFile = mkOption {
354+ type = with types; nullOr path;
355+ default = null;
343356 description = ''
344− Initial password of the root account if this is a new install.
357+ File containing the initial password of the root account if
358+ this is a new install.
359+
360+ This should be a string, not a nix path, since nix paths are
361+ copied into the world-readable nix store.
345362 '';
346363 };
347364
@@ -365,15 +382,20 @@ in {
365382 };
366383
367384 username = mkOption {
368− type = types.nullOr types.str;
385+ type = with types; nullOr str;
369386 default = null;
370387 description = "Username of the SMTP server for Gitlab.";
371388 };
372389
373− password = mkOption {
374− type = types.nullOr types.str;
390+ passwordFile = mkOption {
391+ type = types.nullOr types.path;
375392 default = null;
376− description = "Password of the SMTP server for Gitlab.";
393+ description = ''
394+ File containing the password of the SMTP server for Gitlab.
395+
396+ This should be a string, not a nix path, since nix paths
397+ are copied into the world-readable nix store.
398+ '';
377399 };
378400
379401 domain = mkOption {
@@ -383,7 +405,7 @@ in {
383405 };
384406
385407 authentication = mkOption {
386− type = types.nullOr types.str;
408+ type = with types; nullOr str;
387409 default = null;
388410 description = "Authentitcation type to use, see http://api.rubyonrails.org/classes/ActionMailer/Base.html";
389411 };
@@ -401,68 +423,125 @@ in {
401423 };
402424 };
403425
404− secrets.secret = mkOption {
405− type = types.str;
426+ secrets.secretFile = mkOption {
427+ type = with types; nullOr path;
428+ default = null;
406429 description = ''
407− The secret is used to encrypt variables in the DB. If
408− you change or lose this key you will be unable to access variables
409− stored in database.
430+ A file containing the secret used to encrypt variables in
431+ the DB. If you change or lose this key you will be unable to
432+ access variables stored in database.
410433
411434 Make sure the secret is at least 30 characters and all random,
412435 no regular words or you'll be exposed to dictionary attacks.
436+
437+ This should be a string, not a nix path, since nix paths are
438+ copied into the world-readable nix store.
413439 '';
414440 };
415441
416− secrets.db = mkOption {
417− type = types.str;
442+ secrets.dbFile = mkOption {
443+ type = with types; nullOr path;
444+ default = null;
418445 description = ''
419− The secret is used to encrypt variables in the DB. If
420− you change or lose this key you will be unable to access variables
421− stored in database.
446+ A file containing the secret used to encrypt variables in
447+ the DB. If you change or lose this key you will be unable to
448+ access variables stored in database.
422449
423450 Make sure the secret is at least 30 characters and all random,
424451 no regular words or you'll be exposed to dictionary attacks.
452+
453+ This should be a string, not a nix path, since nix paths are
454+ copied into the world-readable nix store.
425455 '';
426456 };
427457
428− secrets.otp = mkOption {
429− type = types.str;
458+ secrets.otpFile = mkOption {
459+ type = with types; nullOr path;
460+ default = null;
430461 description = ''
431− The secret is used to encrypt secrets for OTP tokens. If
432− you change or lose this key, users which have 2FA enabled for login
433− won't be able to login anymore.
462+ A file containing the secret used to encrypt secrets for OTP
463+ tokens. If you change or lose this key, users which have 2FA
464+ enabled for login won't be able to login anymore.
434465
435466 Make sure the secret is at least 30 characters and all random,
436467 no regular words or you'll be exposed to dictionary attacks.
468+
469+ This should be a string, not a nix path, since nix paths are
470+ copied into the world-readable nix store.
437471 '';
438472 };
439473
440− secrets.jws = mkOption {
441− type = types.str;
474+ secrets.jwsFile = mkOption {
475+ type = with types; nullOr path;
476+ default = null;
442477 description = ''
443− The secret is used to encrypt session keys. If you change or lose
444− this key, users will be disconnected.
478+ A file containing the secret used to encrypt session
479+ keys. If you change or lose this key, users will be
480+ disconnected.
445481
446482 Make sure the secret is an RSA private key in PEM format. You can
447483 generate one with
448484
449485 openssl genrsa 2048
486+
487+ This should be a string, not a nix path, since nix paths are
488+ copied into the world-readable nix store.
450489 '';
451490 };
452491
453492 extraConfig = mkOption {
454493 type = types.attrs;
455494 default = {};
456− example = {
457− gitlab = {
458− default_projects_features = {
459− builds = false;
495+ example = literalExample ''
496+ {
497+ gitlab = {
498+ default_projects_features = {
499+ builds = false;
500+ };
501+ };
502+ omniauth = {
503+ enabled = true;
504+ auto_sign_in_with_provider = "openid_connect";
505+ allow_single_sign_on = ["openid_connect"];
506+ block_auto_created_users = false;
507+ providers = [
508+ {
509+ name = "openid_connect";
510+ label = "OpenID Connect";
511+ args = {
512+ name = "openid_connect";
513+ scope = ["openid" "profile"];
514+ response_type = "code";
515+ issuer = "https://keycloak.example.com/auth/realms/My%20Realm";
516+ discovery = true;
517+ client_auth_method = "query";
518+ uid_field = "preferred_username";
519+ client_options = {
520+ identifier = "gitlab";
521+ secret = { _secret = "/var/keys/gitlab_oidc_secret"; };
522+ redirect_uri = "https://git.example.com/users/auth/openid_connect/callback";
523+ };
524+ };
525+ }
526+ ];
460527 };
461528 };
462− };
529+ '';
463530 description = ''
464− Extra options to be merged into config/gitlab.yml as nix
465− attribute set.
531+ Extra options to be added under
532+ <literal>production</literal> in
533+ <filename>config/gitlab.yml</filename>, as a nix attribute
534+ set.
535+
536+ Options containing secret data should be set to an attribute
537+ set containing the attribute <literal>_secret</literal> - a
538+ string pointing to a file containing the value the option
539+ should be set to. See the example to get a better picture of
540+ this: in the resulting
541+ <filename>config/gitlab.yml</filename> file, the
542+ <literal>production.omniauth.providers[0].args.client_options.secret</literal>
543+ key will be set to the contents of the
544+ <filename>/var/keys/gitlab_oidc_secret</filename> file.
466545 '';
467546 };
468547 };
@@ -470,12 +549,66 @@ in {
470549
471550 config = mkIf cfg.enable {
472551
552+ assertions = [
553+ {
554+ assertion = cfg.databaseCreateLocally -> (cfg.user == cfg.databaseUsername);
555+ message = "For local automatic database provisioning services.gitlab.user and services.gitlab.databaseUsername should be identical.";
556+ }
557+ {
558+ assertion = (cfg.databaseHost != "") -> (cfg.databasePasswordFile != null);
559+ message = "When services.gitlab.databaseHost is customized, services.gitlab.databasePasswordFile must be set!";
560+ }
561+ {
562+ assertion = cfg.initialRootPasswordFile != null;
563+ message = "services.gitlab.initialRootPasswordFile must be set!";
564+ }
565+ {
566+ assertion = cfg.secrets.secretFile != null;
567+ message = "services.gitlab.secrets.secretFile must be set!";
568+ }
569+ {
570+ assertion = cfg.secrets.dbFile != null;
571+ message = "services.gitlab.secrets.dbFile must be set!";
572+ }
573+ {
574+ assertion = cfg.secrets.otpFile != null;
575+ message = "services.gitlab.secrets.otpFile must be set!";
576+ }
577+ {
578+ assertion = cfg.secrets.jwsFile != null;
579+ message = "services.gitlab.secrets.jwsFile must be set!";
580+ }
581+ ];
582+
473583 environment.systemPackages = [ pkgs.git gitlab-rake gitlab-rails cfg.packages.gitlab-shell ];
474584
475585 # Redis is required for the sidekiq queue runner.
476586 services.redis.enable = mkDefault true;
587+
477588 # We use postgres as the main data store.
478− services.postgresql.enable = mkDefault true;
589+ services.postgresql = optionalAttrs cfg.databaseCreateLocally {
590+ enable = true;
591+ ensureUsers = singleton { name = cfg.databaseUsername; };
592+ };
593+ # The postgresql module doesn't currently support concepts like
594+ # objects owners and extensions; for now we tack on what's needed
595+ # here.
596+ systemd.services.postgresql.postStart = mkAfter (optionalString cfg.databaseCreateLocally ''
597+ $PSQL -tAc "SELECT 1 FROM pg_database WHERE datname = '${cfg.databaseName}'" | grep -q 1 || $PSQL -tAc 'CREATE DATABASE "${cfg.databaseName}" OWNER "${cfg.databaseUsername}"'
598+ current_owner=$($PSQL -tAc "SELECT pg_catalog.pg_get_userbyid(datdba) FROM pg_catalog.pg_database WHERE datname = '${cfg.databaseName}'")
599+ if [[ "$current_owner" != "${cfg.databaseUsername}" ]]; then
600+ $PSQL -tAc 'ALTER DATABASE "${cfg.databaseName}" OWNER TO "${cfg.databaseUsername}"'
601+ if [[ -e "${config.services.postgresql.dataDir}/.reassigning_${cfg.databaseName}" ]]; then
602+ echo "Reassigning ownership of database ${cfg.databaseName} to user ${cfg.databaseUsername} failed on last boot. Failing..."
603+ exit 1
604+ fi
605+ touch "${config.services.postgresql.dataDir}/.reassigning_${cfg.databaseName}"
606+ $PSQL "${cfg.databaseName}" -tAc "REASSIGN OWNED BY \"$current_owner\" TO \"${cfg.databaseUsername}\""
607+ rm "${config.services.postgresql.dataDir}/.reassigning_${cfg.databaseName}"
608+ fi
609+ $PSQL '${cfg.databaseName}' -tAc "CREATE EXTENSION IF NOT EXISTS pg_trgm"
610+ '');
611+
479612 # Use postfix to send out mails.
480613 services.postfix.enable = mkDefault true;
481614
@@ -527,14 +660,9 @@ in {
527660
528661 "L+ /run/gitlab/shell-config.yml - - - - ${pkgs.writeText "config.yml" (builtins.toJSON gitlabShellConfig)}"
529662
530− "L+ ${cfg.statePath}/config/gitlab.yml - - - - ${pkgs.writeText "gitlab.yml" (builtins.toJSON gitlabConfig)}"
531− "L+ ${cfg.statePath}/config/database.yml - - - - ${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)}"
532− "L+ ${cfg.statePath}/config/secrets.yml - - - - ${pkgs.writeText "secrets.yml" (builtins.toJSON secretsConfig)}"
533663 "L+ ${cfg.statePath}/config/unicorn.rb - - - - ${./defaultUnicornConfig.rb}"
534−
535664 "L+ ${cfg.statePath}/config/initializers/extra-gitlab.rb - - - - ${extraGitlabRb}"
536− ] ++ optional cfg.smtp.enable
537− "L+ ${cfg.statePath}/config/initializers/smtp_settings.rb - - - - ${smtpSettings}" ;
665+ ];
538666
539667 systemd.services.gitlab-sidekiq = {
540668 after = [ "network.target" "redis.service" "gitlab.service" ];
@@ -626,46 +754,75 @@ in {
626754 gnupg
627755 ];
628756 preStart = ''
629− ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -f ${cfg.packages.gitlab}/share/gitlab/VERSION ${cfg.statePath}/VERSION
630− ${pkgs.sudo}/bin/sudo -u ${cfg.user} rm -rf ${cfg.statePath}/db/*
631− ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/config.dist/* ${cfg.statePath}/config
632− ${pkgs.sudo}/bin/sudo -u ${cfg.user} cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/db/* ${cfg.statePath}/db
633−
634− ${pkgs.openssl}/bin/openssl rand -hex 32 > ${cfg.statePath}/gitlab_shell_secret
635−
636− ${pkgs.sudo}/bin/sudo -u ${cfg.user} ${cfg.packages.gitlab-shell}/bin/install
637−
638− if ! test -e "${cfg.statePath}/db-created"; then
639− if [ "${cfg.databaseHost}" = "127.0.0.1" ]; then
640− ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} psql postgres -c "CREATE ROLE ${cfg.databaseUsername} WITH LOGIN NOCREATEDB NOCREATEROLE ENCRYPTED PASSWORD '${cfg.databasePassword}'"
641− ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} ${config.services.postgresql.package}/bin/createdb --owner ${cfg.databaseUsername} ${cfg.databaseName}
642−
643− # enable required pg_trgm extension for gitlab
644− ${pkgs.sudo}/bin/sudo -u ${pgSuperUser} psql ${cfg.databaseName} -c "CREATE EXTENSION IF NOT EXISTS pg_trgm"
757+ cp -f ${cfg.packages.gitlab}/share/gitlab/VERSION ${cfg.statePath}/VERSION
758+ rm -rf ${cfg.statePath}/db/*
759+ cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/config.dist/* ${cfg.statePath}/config
760+ cp -rf --no-preserve=mode ${cfg.packages.gitlab}/share/gitlab/db/* ${cfg.statePath}/db
761+
762+ ${cfg.packages.gitlab-shell}/bin/install
763+
764+ ${optionalString cfg.smtp.enable ''
765+ install -m u=rw ${smtpSettings} ${cfg.statePath}/config/initializers/smtp_settings.rb
766+ ${optionalString (cfg.smtp.passwordFile != null) ''
767+ smtp_password=$(<'${cfg.smtp.passwordFile}')
768+ ${pkgs.replace}/bin/replace-literal -e '@smtpPassword@' "$smtp_password" '${cfg.statePath}/config/initializers/smtp_settings.rb'
769+ ''}
770+ ''}
771+
772+ (
773+ umask u=rwx,g=,o=
774+
775+ ${pkgs.openssl}/bin/openssl rand -hex 32 > ${cfg.statePath}/gitlab_shell_secret
776+
777+ ${if cfg.databasePasswordFile != null then ''
778+ export db_password="$(<'${cfg.databasePasswordFile}')"
779+
780+ if [[ -z "$db_password" ]]; then
781+ >&2 echo "Database password was an empty string!"
782+ exit 1
783+ fi
784+
785+ ${pkgs.jq}/bin/jq <${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)} \
786+ '.production.password = $ENV.db_password' \
787+ >'${cfg.statePath}/config/database.yml'
788+ ''
789+ else ''
790+ ${pkgs.jq}/bin/jq <${pkgs.writeText "database.yml" (builtins.toJSON databaseConfig)} \
791+ >'${cfg.statePath}/config/database.yml'
792+ ''
793+ }
794+
795+ ${utils.genJqSecretsReplacementSnippet
796+ gitlabConfig
797+ "${cfg.statePath}/config/gitlab.yml"
798+ }
799+
800+ if [[ -h '${cfg.statePath}/config/secrets.yml' ]]; then
801+ rm '${cfg.statePath}/config/secrets.yml'
645802 fi
646803
647− ${pkgs.sudo}/bin/sudo -u ${cfg.user} -H ${gitlab-rake}/bin/gitlab-rake db:schema:load
648−
649− ${pkgs.sudo}/bin/sudo -u ${cfg.user} touch "${cfg.statePath}/db-created"
650− fi
651−
652− # Always do the db migrations just to be sure the database is up-to-date
653− ${pkgs.sudo}/bin/sudo -u ${cfg.user} -H ${gitlab-rake}/bin/gitlab-rake db:migrate
654−
655− if ! test -e "${cfg.statePath}/db-seeded"; then
656− ${pkgs.sudo}/bin/sudo -u ${cfg.user} ${gitlab-rake}/bin/gitlab-rake db:seed_fu \
657− GITLAB_ROOT_PASSWORD='${cfg.initialRootPassword}' GITLAB_ROOT_EMAIL='${cfg.initialRootEmail}'
658− ${pkgs.sudo}/bin/sudo -u ${cfg.user} touch "${cfg.statePath}/db-seeded"
659− fi
804+ export secret="$(<'${cfg.secrets.secretFile}')"
805+ export db="$(<'${cfg.secrets.dbFile}')"
806+ export otp="$(<'${cfg.secrets.otpFile}')"
807+ export jws="$(<'${cfg.secrets.jwsFile}')"
808+ ${pkgs.jq}/bin/jq -n '{production: {secret_key_base: $ENV.secret,
809+ otp_key_base: $ENV.db,
810+ db_key_base: $ENV.otp,
811+ openid_connect_signing_key: $ENV.jws}}' \
812+ > '${cfg.statePath}/config/secrets.yml'
813+ )
814+
815+ initial_root_password="$(<'${cfg.initialRootPasswordFile}')"
816+ ${gitlab-rake}/bin/gitlab-rake gitlab:db:configure GITLAB_ROOT_PASSWORD="$initial_root_password" \
817+ GITLAB_ROOT_EMAIL='${cfg.initialRootEmail}'
660818
661819 # We remove potentially broken links to old gitlab-shell versions
662820 rm -Rf ${cfg.statePath}/repositories/**/*.git/hooks
663821
664− ${pkgs.sudo}/bin/sudo -u ${cfg.user} -H ${pkgs.git}/bin/git config --global core.autocrlf "input"
822+ ${pkgs.git}/bin/git config --global core.autocrlf "input"
665823 '';
666824
667825 serviceConfig = {
668− PermissionsStartOnly = true; # preStart must be run as root
669826 Type = "simple";
670827 User = cfg.user;
671828 Group = cfg.group;