Files
homelab-config/homelab-services/minio-service/flake.nix
T

209 lines
6.8 KiB
Nix
Raw Normal View History

{
description = "minio service for storing images with api access";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
};
outputs = { self, nixpkgs, ... }: {
nixosModules.minio-service = { config, lib, pkgs, ... }:
let
opts = config.services.minio-service;
in {
options.services.minio-service = {
enable = lib.mkEnableOption "MinIO object storage server";
dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/minio";
description = "Directory to store MinIO data.";
};
dataPort = lib.mkOption {
type = lib.types.port;
default = 9000;
description = "MinIO S3 API port.";
};
consolePort = lib.mkOption {
type = lib.types.port;
default = 9001;
description = "MinIO Admin Console port.";
};
2025-06-12 19:01:51 -05:00
credentialsFile = lib.mkOption {
type = lib.types.path;
2025-06-12 19:01:51 -05:00
description = "File containing MinIO credentials.";
};
2025-08-16 21:11:24 -05:00
bootstrap-minio = {
enable = lib.mkEnableOption "Enable bootstrapping minio creds";
environments = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = "list of environments to bootstrap (dev, prod, stage, etc)";
};
};
default-nginx = {
enable = lib.mkEnableOption "Enable nginx reverse proxy for MinIO";
hostname = lib.mkOption {
2025-08-16 21:16:25 -05:00
type = lib.types.str;
default = "localhost";
description = "Hostname for nginx reverse proxy.";
};
};
};
config = lib.mkIf opts.enable {
users.groups.minio = {};
users.users.minio = {
isSystemUser = true;
2025-06-12 22:15:29 -05:00
createHome = true;
2025-06-12 19:32:34 -05:00
home = "${opts.dataDir}";
2025-06-12 22:15:29 -05:00
group = "minio";
};
systemd.services.minio = {
description = "MinIO S3-compatible object storage";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
2025-06-20 23:54:53 -05:00
ExecStart = ''
${pkgs.minio}/bin/minio server ${opts.dataDir} \
--address ":${toString opts.dataPort}" \
--console-address ":${toString opts.consolePort}"
2025-06-20 22:29:38 -05:00
'';
2025-06-20 23:54:53 -05:00
User = "minio";
Group = "minio";
2025-08-16 22:40:23 -05:00
Environment = "MINIO_BROWSER_REDIRECT_URL=https://${opts.default-nginx.hostname}/console";
2025-06-20 23:54:53 -05:00
EnvironmentFile = "${opts.credentialsFile}";
Restart = "always";
};
};
2025-08-16 21:11:24 -05:00
systemd.services.bootstrap-minio = lib.mkIf opts.bootstrap-minio.enable {
description = "Minio bootstrap users, policies, buckets";
after = [ "minio.service" ];
requires = [ "minio.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
ExecStart = pkgs.writeShellScript "bootstrap-minio" ''
2025-08-16 21:37:07 -05:00
2025-08-16 21:11:24 -05:00
set -euo pipefail
2025-08-16 21:37:07 -05:00
alias_name="local"
mc_bin=${pkgs.minio-client}/bin/mc
2025-08-16 22:46:30 -05:00
# Delete alias if it already exists
mc alias rm local || true
2025-08-16 21:37:07 -05:00
# Point mc at the local minio instance
2025-08-16 21:45:04 -05:00
$mc_bin alias set "$alias_name" http://localhost:${toString opts.dataPort} \
2025-08-16 21:37:07 -05:00
"$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD"
# Loop through requested environments
for env in ${lib.escapeShellArgs opts.bootstrap-minio.environments}; do
2025-08-16 21:54:27 -05:00
bucket="$env"
2025-08-16 21:37:07 -05:00
user_var="MINIO_''${env^^}_USER"
pass_var="MINIO_''${env^^}_PASSWORD"
# expand env var names dynamically
user_val=$(eval "echo \''${$user_var:-}")
pass_val=$(eval "echo \''${$pass_var:-}")
if [ -z "$user_val" ] || [ -z "$pass_val" ]; then
echo "Missing credentials for $env ($user_var / $pass_var)" >&2
exit 1
fi
policy="policy-$env"
echo "Bootstrapping environment: $env"
# Create bucket if not exists
$mc_bin mb --ignore-existing "$alias_name/$bucket"
2025-08-16 22:28:13 -05:00
# Set bucket limit in future
# $mc_bin bucket quota is deprecated
2025-08-16 21:37:07 -05:00
# Create policy JSON (full access to that bucket)
policy_file=$(mktemp)
cat > "$policy_file" <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:*"],
"Resource": [
"arn:aws:s3:::$bucket",
"arn:aws:s3:::$bucket/*"
]
}
]
}
EOF
# Apply policy
2025-08-16 21:54:27 -05:00
$mc_bin admin policy create "$alias_name" "$policy" "$policy_file"
2025-08-16 21:37:07 -05:00
# Create or update user
if ! $mc_bin admin user info "$alias_name" "$user_val" >/dev/null 2>&1; then
$mc_bin admin user add "$alias_name" "$user_val" "$pass_val"
echo "Created user $user_val"
fi
# Attach policy
2025-08-16 22:02:21 -05:00
$mc_bin admin policy attach "$alias_name" "$policy" --user "''${user_val}"
2025-08-16 21:37:07 -05:00
rm -f "$policy_file"
done
2025-08-16 21:11:24 -05:00
'';
User = "minio";
Group = "minio";
2025-08-16 21:45:04 -05:00
EnvironmentFile = "${opts.credentialsFile}";
2025-08-16 21:11:24 -05:00
};
};
services.nginx = lib.mkIf opts.default-nginx.enable {
enable = true;
virtualHosts.${opts.default-nginx.hostname} = {
forceSSL = true;
enableACME = true;
2025-06-12 22:29:53 -05:00
acmeRoot = null;
2025-08-16 21:37:07 -05:00
2025-07-02 17:07:20 -05:00
locations."/console" = {
2025-08-16 22:50:43 -05:00
proxyPass = "http://localhost:${toString opts.consolePort}";
};
2025-08-16 21:37:07 -05:00
2025-07-03 10:43:54 -05:00
# Add directive for s3like queries, which only accept root path allegedly
2025-07-03 10:44:54 -05:00
locations."/" = {
2025-07-03 10:43:54 -05:00
proxyPass = "http://localhost:${toString opts.dataPort}";
2025-07-03 15:36:34 -05:00
extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 0;
proxy_buffering off;
'';
2025-07-03 10:43:54 -05:00
};
};
};
networking.firewall.allowedTCPPorts = lib.mkMerge [
2025-06-12 22:29:53 -05:00
(lib.mkIf opts.enable [ opts.dataPort opts.consolePort ])
(lib.mkIf opts.default-nginx.enable [ 80 443 ])
];
};
};
};
}
2025-07-02 17:07:20 -05:00