Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
395cc85
sourcehut: make script work with non-python modules
christoph-heiss Mar 21, 2023
d703173
sourcehut: make /query endpoint config common to all services
christoph-heiss Apr 19, 2023
8d56959
sourcehut.srht: 0.69.0 -> 0.69.15
christoph-heiss Mar 21, 2023
869781c
sourcehut.buildsrht: 0.83.0 -> 0.86.10
christoph-heiss Apr 19, 2023
c123a37
sourcehut.gitsrht: 0.78.20 -> 0.84.2
christoph-heiss Apr 19, 2023
a973057
sourcehut.hgsrht: 0.31.3 -> 0.32.4
christoph-heiss Mar 21, 2023
1a7c5a8
sourcehut.hubsrht: 0.14.14 -> 0.17.2
christoph-heiss Mar 21, 2023
b0fb4dc
sourcehut.listssrht: 0.51.11 -> 0.57.8
christoph-heiss Mar 21, 2023
3005409
sourcehut.mansrht: 0.15.26 -> 0.16.1
christoph-heiss Mar 21, 2023
558767f
sourcehut.metasrht: 0.61.3 -> 0.64.8
christoph-heiss Mar 21, 2023
b916bbd
sourcehut.pagessrht: 0.7.4 -> 0.13.0
christoph-heiss Mar 21, 2023
8d573f2
sourcehut.pastesrht: 0.13.8 -> 0.15.1
christoph-heiss Apr 2, 2023
f53540a
sourcehut.scmsrht: 0.22.22 -> 0.22.23
christoph-heiss Apr 2, 2023
c0c7355
sourcehut.todosrht: 0.72.2 -> 0.74.6
christoph-heiss Apr 2, 2023
9006648
sourcehut: default gqlgenVersion 0.17.2 -> 0.17.20
christoph-heiss Mar 29, 2023
12fe05f
sourcehut: add override for SQLAlchemy 1.x
christoph-heiss Apr 3, 2023
6e51802
sourcehut: fix `repos` path by using actual settings value
christoph-heiss Apr 19, 2023
6648488
sourcehut: fix logging of git/hg ssh commands
christoph-heiss Apr 20, 2023
c39ba7f
sourcehut: remove `set -x` from ssh commands
christoph-heiss Apr 20, 2023
5841d27
sourcehut: explicitly disallow openssh to socket-active
christoph-heiss May 2, 2023
6b25e09
sourcehut: fix up some more bin paths
christoph-heiss Jul 24, 2023
78cc278
sourcehut: drop obsolete `services` array in favor of indivdual `enab…
christoph-heiss Aug 7, 2023
589b75b
sourcehut: disable IPv6 completely for tests
christoph-heiss Aug 7, 2023
fc6addb
sourcehut: reword `api-origin` option description
christoph-heiss Nov 11, 2023
641e54b
sourcehut: create logs directory unconditionally
christoph-heiss Nov 11, 2023
79dc7c3
sourcehut: add overrides for `flask-sqlalchemy` and `factory-boy`
christoph-heiss Nov 11, 2023
acd21da
sourcehut: use systemd.tmpfiles instead of manually creating logfiles
christoph-heiss Nov 11, 2023
66b86f8
sourcehut: de-duplicate nginx `add_header` directives
christoph-heiss Nov 11, 2023
88a3d2a
sourcehut: fix postgresql database permission for postgresql >= 15
christoph-heiss Nov 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 68 additions & 90 deletions nixos/modules/services/misc/sourcehut/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let
|| head srvMatch == srv # Include sections for the service being configured
then v
# Enable Web links and integrations between services.
else if tail srvMatch == [ null ] && elem (head srvMatch) cfg.services
else if tail srvMatch == [ null ] && cfg.${head srvMatch}.enable
then {
inherit (v) origin;
# mansrht crashes without it
Expand All @@ -38,9 +38,9 @@ let
# for services needing access to them.
"builds.sr.ht::worker".buildlogs = "/var/log/sourcehut/buildsrht-worker";
"git.sr.ht".post-update-script = "/usr/bin/gitsrht-update-hook";
"git.sr.ht".repos = "/var/lib/sourcehut/gitsrht/repos";
"git.sr.ht".repos = cfg.settings."git.sr.ht".repos;
"hg.sr.ht".changegroup-script = "/usr/bin/hgsrht-hook-changegroup";
"hg.sr.ht".repos = "/var/lib/sourcehut/hgsrht/repos";
"hg.sr.ht".repos = cfg.settings."hg.sr.ht".repos;
# Making this a per service option despite being in a global section,
# so that it uses the redis-server used by the service.
"sr.ht".redis-host = cfg.${srv}.redis.host;
Expand Down Expand Up @@ -77,6 +77,14 @@ let
type = types.path;
apply = s: "<" + toString s;
};
api-origin = mkOption {
description = lib.mdDoc "Origin URL for the API";
type = types.str;
default = "http://${cfg.listenAddress}:${toString (cfg.${srv}.port + 100)}";
defaultText = lib.literalMD ''
`"http://''${`[](#opt-services.sourcehut.listenAddress)`}:''${toString (`[](#opt-services.sourcehut.${srv}.port)` + 100)}"`
'';
};
};

# Specialized python containing all the modules
Expand Down Expand Up @@ -112,15 +120,6 @@ in
and account management services
'');

services = mkOption {
type = with types; listOf (enum
[ "builds" "git" "hg" "hub" "lists" "man" "meta" "pages" "paste" "todo" ]);
defaultText = "locally enabled services";
description = lib.mdDoc ''
Services that may be displayed as links in the title bar of the Web interface.
'';
};

listenAddress = mkOption {
type = types.str;
default = "localhost";
Expand Down Expand Up @@ -400,8 +399,8 @@ in
This setting is propagated to newer and existing repositories.
'';
type = types.str;
default = "${cfg.python}/bin/hgsrht-hook-changegroup";
defaultText = "\${cfg.python}/bin/hgsrht-hook-changegroup";
default = "${pkgs.sourcehut.hgsrht}/bin/hgsrht-hook-changegroup";
defaultText = "\${pkgs.sourcehut.hgsrht}/bin/hgsrht-hook-changegroup";
};
repos = mkOption {
description = lib.mdDoc ''
Expand Down Expand Up @@ -501,12 +500,6 @@ in
options."meta.sr.ht" =
removeAttrs (commonServiceSettings "meta")
["oauth-client-id" "oauth-client-secret"] // {
api-origin = mkOption {
description = lib.mdDoc "Origin URL for API, 100 more than web.";
type = types.str;
default = "http://${cfg.listenAddress}:${toString (cfg.meta.port + 100)}";
defaultText = lib.literalMD ''`"http://''${`[](#opt-services.sourcehut.listenAddress)`}:''${toString (`[](#opt-services.sourcehut.meta.port)` + 100)}"`'';
};
webhooks = mkOption {
description = lib.mdDoc "The Redis connection used for the webhooks worker.";
type = types.str;
Expand Down Expand Up @@ -784,6 +777,7 @@ in
extraConfig = ''
PermitUserEnvironment SRHT_*
'';
startWhenNeeded = false;
};
environment.etc."ssh/sourcehut/config.ini".source =
settingsFormat.generate "sourcehut-dispatch-config.ini"
Expand All @@ -792,15 +786,28 @@ in
environment.etc."ssh/sourcehut/subdir/srht-dispatch" = {
# sshd_config(5): The program must be owned by root, not writable by group or others
mode = "0755";
source = pkgs.writeShellScript "srht-dispatch" ''
source = pkgs.writeShellScript "srht-dispatch-wrapper" ''
set -e
set -x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you still need mean to set -x here?

cd /etc/ssh/sourcehut/subdir
${cfg.python}/bin/gitsrht-dispatch "$@"
${pkgs.sourcehut.gitsrht}/bin/gitsrht-dispatch "$@"
'';
};
systemd.tmpfiles.settings."10-sourcehut-gitsrht" = mkIf cfg.git.enable (
builtins.listToAttrs (map (name: {
name = "/var/log/sourcehut/gitsrht-${name}";
value.f = {
inherit (cfg.git) user group;
mode = "0644";
};
}) [ "keys" "shell" "update-hook" ])
);
systemd.services.sshd = {
#path = optional cfg.git.enable [ cfg.git.package ];
preStart = mkIf cfg.hg.enable ''
chown ${cfg.hg.user}:${cfg.hg.group} /var/log/sourcehut/hgsrht-keys
'';
serviceConfig = {
LogsDirectory = "sourcehut";
BindReadOnlyPaths =
# Note that those /usr/bin/* paths are hardcoded in multiple places in *.sr.ht,
# for instance to get the user from the [git.sr.ht::dispatch] settings.
Expand All @@ -813,7 +820,6 @@ in
"${pkgs.writeShellScript "buildsrht-keys-wrapper" ''
set -e
cd /run/sourcehut/buildsrht/subdir
set -x
exec -a "$0" ${pkgs.sourcehut.buildsrht}/bin/buildsrht-keys "$@"
''}:/usr/bin/buildsrht-keys"
"${pkgs.sourcehut.buildsrht}/bin/master-shell:/usr/bin/master-shell"
Expand All @@ -825,31 +831,26 @@ in
"${pkgs.writeShellScript "gitsrht-keys-wrapper" ''
set -e
cd /run/sourcehut/gitsrht/subdir
set -x
exec -a "$0" ${pkgs.sourcehut.gitsrht}/bin/gitsrht-keys "$@"
''}:/usr/bin/gitsrht-keys"
"${pkgs.writeShellScript "gitsrht-shell-wrapper" ''
set -e
cd /run/sourcehut/gitsrht/subdir
set -x
export PATH="${cfg.git.package}/bin:$PATH"
export SRHT_CONFIG=/run/sourcehut/gitsrht/config.ini
exec -a "$0" ${pkgs.sourcehut.gitsrht}/bin/gitsrht-shell "$@"
''}:/usr/bin/gitsrht-shell"
"${pkgs.writeShellScript "gitsrht-update-hook" ''
set -e
test -e "''${PWD%/*}"/config.ini ||
# Git hooks are run relative to their repository's directory,
# but gitsrht-update-hook looks up ../config.ini
ln -s /run/sourcehut/gitsrht/config.ini "''${PWD%/*}"/config.ini
export SRHT_CONFIG=/run/sourcehut/gitsrht/config.ini
# hooks/post-update calls /usr/bin/gitsrht-update-hook as hooks/stage-3
# but this wrapper being a bash script, it overrides $0 with /usr/bin/gitsrht-update-hook
# hence this hack to put hooks/stage-3 back into gitsrht-update-hook's $0
if test "''${STAGE3:+set}"
then
set -x
exec -a hooks/stage-3 ${pkgs.sourcehut.gitsrht}/bin/gitsrht-update-hook "$@"
else
export STAGE3=set
set -x
exec -a "$0" ${pkgs.sourcehut.gitsrht}/bin/gitsrht-update-hook "$@"
fi
''}:/usr/bin/gitsrht-update-hook"
Expand All @@ -860,13 +861,11 @@ in
"${pkgs.writeShellScript "hgsrht-keys-wrapper" ''
set -e
cd /run/sourcehut/hgsrht/subdir
set -x
exec -a "$0" ${pkgs.sourcehut.hgsrht}/bin/hgsrht-keys "$@"
''}:/usr/bin/hgsrht-keys"
"${pkgs.writeShellScript "hgsrht-shell-wrapper" ''
set -e
cd /run/sourcehut/hgsrht/subdir
set -x
exec -a "$0" ${pkgs.sourcehut.hgsrht}/bin/hgsrht-shell "$@"
''}:/usr/bin/hgsrht-shell"
# Mercurial's changegroup hooks are run relative to their repository's directory,
Expand All @@ -875,8 +874,7 @@ in
set -e
test -e "''$PWD"/config.ini ||
ln -s /run/sourcehut/hgsrht/config.ini "''$PWD"/config.ini
set -x
exec -a "$0" ${cfg.python}/bin/hgsrht-hook-changegroup "$@"
exec -a "$0" ${pkgs.sourcehut.hgsrht}/bin/hgsrht-hook-changegroup "$@"
''}:/usr/bin/hgsrht-hook-changegroup"
];
};
Expand Down Expand Up @@ -1066,10 +1064,11 @@ in
};
})
];
extraServices.gitsrht-api = {
serviceConfig.Restart = "always";
serviceConfig.RestartSec = "5s";
serviceConfig.ExecStart = "${pkgs.sourcehut.gitsrht}/bin/gitsrht-api -b ${cfg.listenAddress}:${toString (cfg.git.port + 100)}";
extraServices.gitsrht-api.serviceConfig = {
Restart = "always";
RestartSec = "5s";
ExecStart = "${pkgs.sourcehut.gitsrht}/bin/gitsrht-api -b ${cfg.listenAddress}:${toString (cfg.git.port + 100)}";
BindPaths = [ "${cfg.settings."git.sr.ht".repos}:/var/lib/sourcehut/gitsrht/repos" ];
};
extraServices.gitsrht-fcgiwrap = mkIf cfg.nginx.enable {
serviceConfig = {
Expand Down Expand Up @@ -1188,7 +1187,7 @@ in
extraServices.listssrht-lmtp = {
wants = [ "postfix.service" ];
unitConfig.JoinsNamespaceOf = optional cfg.postfix.enable "postfix.service";
serviceConfig.ExecStart = "${cfg.python}/bin/listssrht-lmtp";
serviceConfig.ExecStart = "${pkgs.sourcehut.listssrht}/bin/listssrht-lmtp";
# Avoid crashing: os.chown(sock, os.getuid(), sock_gid)
serviceConfig.PrivateUsers = mkForce false;
};
Expand Down Expand Up @@ -1252,55 +1251,30 @@ in
) cfg.settings));
serviceConfig.ExecStart = "${pkgs.sourcehut.metasrht}/bin/metasrht-api -b ${cfg.listenAddress}:${toString (cfg.meta.port + 100)}";
};
extraConfig = mkMerge [
{
assertions = [
{ assertion = let s = cfg.settings."meta.sr.ht::billing"; in
s.enabled == "yes" -> (s.stripe-public-key != null && s.stripe-secret-key != null);
message = "If meta.sr.ht::billing is enabled, the keys must be defined.";
}
];
environment.systemPackages = optional cfg.meta.enable
(pkgs.writeShellScriptBin "metasrht-manageuser" ''
set -eux
if test "$(${pkgs.coreutils}/bin/id -n -u)" != '${cfg.meta.user}'
then exec sudo -u '${cfg.meta.user}' "$0" "$@"
else
# In order to load config.ini
if cd /run/sourcehut/metasrht
then exec ${cfg.python}/bin/metasrht-manageuser "$@"
else cat <<EOF
Please run: sudo systemctl start metasrht
EOF
exit 1
fi
extraConfig = {
assertions = [
{ assertion = let s = cfg.settings."meta.sr.ht::billing"; in
s.enabled == "yes" -> (s.stripe-public-key != null && s.stripe-secret-key != null);
message = "If meta.sr.ht::billing is enabled, the keys must be defined.";
}
];
environment.systemPackages = optional cfg.meta.enable
(pkgs.writeShellScriptBin "metasrht-manageuser" ''
set -eux
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another one set -x that could potentially be dropped

if test "$(${pkgs.coreutils}/bin/id -n -u)" != '${cfg.meta.user}'
then exec sudo -u '${cfg.meta.user}' "$0" "$@"
else
# In order to load config.ini
if cd /run/sourcehut/metasrht
then exec ${pkgs.sourcehut.metasrht}/bin/metasrht-manageuser "$@"
else cat <<EOF
Please run: sudo systemctl start metasrht
EOF
exit 1
fi
'');
}
(mkIf cfg.nginx.enable {
services.nginx.virtualHosts."meta.${domain}" = {
locations."/query" = {
proxyPass = cfg.settings."meta.sr.ht".api-origin;
extraConfig = ''
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
'';
};
};
})
];
fi
'');
};
})

(import ./service.nix "pages" {
Expand Down Expand Up @@ -1356,7 +1330,7 @@ in
extraServices.todosrht-lmtp = {
wants = [ "postfix.service" ];
unitConfig.JoinsNamespaceOf = optional cfg.postfix.enable "postfix.service";
serviceConfig.ExecStart = "${cfg.python}/bin/todosrht-lmtp";
serviceConfig.ExecStart = "${pkgs.sourcehut.todosrht}/bin/todosrht-lmtp";
# Avoid crashing: os.chown(sock, os.getuid(), sock_gid)
serviceConfig.PrivateUsers = mkForce false;
};
Expand Down Expand Up @@ -1388,6 +1362,10 @@ in
dispatch is deprecated. See https://sourcehut.org/blog/2022-08-01-dispatch-deprecation-plans/
for more information.
'')

(mkRemovedOptionModule [ "services" "sourcehut" "services"] ''
This option was removed in favor of individual <service>.enable flags.
'')
];

meta.doc = ./default.md;
Expand Down
36 changes: 32 additions & 4 deletions nixos/modules/services/misc/sourcehut/service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ let
#SocketBindDeny = "any";
SystemCallFilter = [
"@system-service"
"~@aio" "~@keyring" "~@memlock" "~@privileged" "~@resources" "~@timer"
"~@aio" "~@keyring" "~@memlock" "~@privileged" "~@timer"
"@chown" "@setuid"
];
SystemCallArchitectures = "native";
Expand Down Expand Up @@ -222,6 +222,23 @@ in
expires 30d;
'';
};
locations."/query" = mkIf (cfg.settings.${iniKey} ? api-origin) {
proxyPass = cfg.settings.${iniKey}.api-origin;
extraConfig = ''
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';

if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}

add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
'';
};
} cfg.nginx.virtualHost ];
};

Expand All @@ -236,9 +253,6 @@ in
}) [srvCfg.user];
};

services.sourcehut.services = mkDefault (filter (s: cfg.${s}.enable)
[ "builds" "dispatch" "git" "hg" "hub" "lists" "man" "meta" "pages" "paste" "todo" ]);

services.sourcehut.settings = mkMerge [
{
"${srv}.sr.ht".origin = mkDefault "https://${srv}.${cfg.settings."sr.ht".global-domain}";
Expand Down Expand Up @@ -363,6 +377,20 @@ in
}
extraService
])) extraServices)

# Work around 'pq: permission denied for schema public' with postgres v15, until a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the note here

# solution for `services.postgresql.ensureUsers` is found.
# See https://github.com/NixOS/nixpkgs/issues/216989
# Workaround taken from nixos/forgejo: https://github.com/NixOS/nixpkgs/pull/262741
(lib.mkIf (
cfg.postgresql.enable
&& lib.strings.versionAtLeast config.services.postgresql.package.version "15.0"
) {
postgresql.postStart = (lib.mkAfter ''
$PSQL -tAc 'ALTER DATABASE "${srvCfg.postgresql.database}" OWNER TO "${srvCfg.user}";'
'');
}
)
];

systemd.timers = mapAttrs (timerName: timer:
Expand Down
6 changes: 1 addition & 5 deletions nixos/tests/sourcehut.nix
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ in
virtualisation.diskSize = 4 * 1024;
virtualisation.memorySize = 2 * 1024;
networking.domain = domain;
networking.enableIPv6 = false;
networking.extraHosts = ''
${config.networking.primaryIPAddress} builds.${domain}
${config.networking.primaryIPAddress} git.${domain}
Expand All @@ -134,11 +135,6 @@ in

services.sourcehut = {
enable = true;
services = [
"builds"
"git"
"meta"
];
nginx.enable = true;
nginx.virtualHost = {
forceSSL = true;
Expand Down
Loading