Skip to content

Commit 5b4d777

Browse files
committed
feat: add GitLab Runner entrypoint and non-root user support
Add entrypoint script as Nix derivation using pkgs.writeShellScript: - Handles custom CA certificate injection for Nix containers - Combines custom CA with system cacert bundle - Sets SSL_CERT_FILE and NIX_SSL_CERT_FILE environment variables - Creates /etc/gitlab-runner directory - Launches gitlab-runner with all passed arguments Configure container to run as non-root user: - Set User to gitlab-runner:gitlab-runner (UID/GID 999) - Create /etc/passwd and /etc/group files - Create necessary directories (/etc/gitlab-runner, /home/gitlab-runner, /tmp) - Set world-writable permissions on /tmp
1 parent e59b3b4 commit 5b4d777

1 file changed

Lines changed: 58 additions & 5 deletions

File tree

flake.nix

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,44 @@
3434
else
3535
"v0.0.0-dev";
3636

37+
# Entrypoint script for GitLab Runner container
38+
entrypoint = pkgs.writeShellScript "entrypoint" ''
39+
# gitlab-runner data directory
40+
DATA_DIR="/etc/gitlab-runner"
41+
CONFIG_FILE=''${CONFIG_FILE:-$DATA_DIR/config.toml}
42+
43+
# custom certificate authority path
44+
CA_CERTIFICATES_PATH=''${CA_CERTIFICATES_PATH:-$DATA_DIR/certs/ca.crt}
45+
LOCAL_CA_PATH="/etc/ssl/certs/ca-certificates.crt"
46+
47+
update_ca() {
48+
echo "Updating CA certificates..."
49+
# In Nix containers, we combine custom CA with system CA bundle
50+
if [ -f "''${CA_CERTIFICATES_PATH}" ]; then
51+
# Create temporary combined certificate bundle
52+
cat ${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt "''${CA_CERTIFICATES_PATH}" > /tmp/ca-bundle-combined.crt
53+
export SSL_CERT_FILE=/tmp/ca-bundle-combined.crt
54+
export NIX_SSL_CERT_FILE=/tmp/ca-bundle-combined.crt
55+
echo "Custom CA certificate added to bundle"
56+
fi
57+
}
58+
59+
# Check if custom CA certificate exists and update if needed
60+
if [ -f "''${CA_CERTIFICATES_PATH}" ]; then
61+
update_ca
62+
else
63+
# Use default Nix CA bundle
64+
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
65+
export NIX_SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
66+
fi
67+
68+
# Ensure data directory exists
69+
mkdir -p "''${DATA_DIR}"
70+
71+
# Launch gitlab-runner passing all arguments
72+
exec ${pkgs.gitlab-runner}/bin/gitlab-runner "$@"
73+
'';
74+
3775
in
3876
{
3977
# Package outputs
@@ -91,10 +129,13 @@
91129
];
92130

93131
config = {
94-
Cmd = [ "/bin/gitlab-runner-kubevirt" ];
132+
Entrypoint = [ "${entrypoint}" ];
133+
Cmd = [ "run" ];
134+
User = "gitlab-runner:gitlab-runner";
95135
Env = [
96136
"PATH=/bin"
97137
"SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
138+
"HOME=/home/gitlab-runner"
98139
];
99140
Labels = {
100141
"org.opencontainers.image.source" = "https://github.com/thpham/gitlab-runner-kubevirt";
@@ -104,12 +145,24 @@
104145
};
105146

106147
# Set the architecture for the image
107-
architecture = if system == "x86_64-linux" then "amd64"
108-
else if system == "aarch64-linux" then "arm64"
109-
else pkgs.stdenv.hostPlatform.linuxArch;
148+
architecture =
149+
if system == "x86_64-linux" then
150+
"amd64"
151+
else if system == "aarch64-linux" then
152+
"arm64"
153+
else
154+
pkgs.stdenv.hostPlatform.linuxArch;
110155

111156
extraCommands = ''
112-
mkdir -p bin
157+
# Create gitlab-runner user and group
158+
echo "gitlab-runner:x:999:999:GitLab Runner:/home/gitlab-runner:/bin/bash" > etc/passwd
159+
echo "gitlab-runner:x:999:" > etc/group
160+
161+
# Create necessary directories
162+
mkdir -p etc/gitlab-runner/certs
163+
mkdir -p home/gitlab-runner
164+
mkdir -p tmp
165+
chmod 1777 tmp
113166
'';
114167
};
115168

0 commit comments

Comments
 (0)