2024-07-10 23:31:57 +03:00
|
|
|
{ config, depot, lib, pkgs, ... }:
|
2023-09-03 22:21:31 +03:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.garage;
|
|
|
|
|
2023-09-04 03:50:09 +03:00
|
|
|
garageShellLibrary = pkgs.writeText "garage-shell-library.sh" ''
|
2023-09-03 22:21:31 +03:00
|
|
|
getNodeId() {
|
|
|
|
nodeId=""
|
|
|
|
while [[ -z "$nodeId" ]]; do
|
|
|
|
nodeId="$(garage status | grep 'NO ROLE ASSIGNED' | grep -wm1 "$1" | cut -d' ' -f1)"
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
echo "Waiting for node $1 to appear..." 2>/dev/null
|
|
|
|
sleep 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "$nodeId"
|
|
|
|
}
|
|
|
|
waitForGarage() {
|
|
|
|
while ! garage status >/dev/null 2>/dev/null; do
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
}
|
2023-09-04 03:50:09 +03:00
|
|
|
waitForGarageOperational() {
|
|
|
|
waitForGarage
|
|
|
|
while garage layout show | grep -qwm1 '^Current cluster layout version: 0'; do
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
}
|
2023-09-03 22:21:31 +03:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2023-09-04 03:50:09 +03:00
|
|
|
options.services.garage = with lib; {
|
|
|
|
layout.initial = mkOption {
|
|
|
|
default = {};
|
|
|
|
type = with types; attrsOf (submodule {
|
|
|
|
options = {
|
|
|
|
zone = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
capacity = mkOption {
|
|
|
|
type = types.ints.positive;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
keys = mkOption {
|
|
|
|
type = with types; attrsOf (submodule {
|
2024-07-10 23:31:57 +03:00
|
|
|
options = {
|
|
|
|
allow = {
|
|
|
|
createBucket = mkOption {
|
|
|
|
description = "Allow the key to create new buckets.";
|
|
|
|
type = bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
locksmith = {
|
|
|
|
nodes = mkOption {
|
|
|
|
description = "Nodes that this key will be made available to via Locksmith.";
|
|
|
|
type = listOf str;
|
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
format = mkOption {
|
|
|
|
description = "Locksmith secret format.";
|
2024-08-03 03:45:19 +03:00
|
|
|
type = enum [ "files" "aws" "envFile" "s3ql" ];
|
2024-07-10 23:31:57 +03:00
|
|
|
default = "files";
|
|
|
|
};
|
|
|
|
owner = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "root";
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "root";
|
|
|
|
};
|
|
|
|
mode = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "0400";
|
|
|
|
};
|
2023-09-04 03:50:09 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2023-09-03 22:21:31 +03:00
|
|
|
default = {};
|
2023-09-04 03:50:09 +03:00
|
|
|
};
|
|
|
|
buckets = mkOption {
|
|
|
|
type = with types; attrsOf (submodule {
|
2023-09-03 22:21:31 +03:00
|
|
|
options = {
|
2023-09-04 03:50:09 +03:00
|
|
|
allow = mkOption {
|
|
|
|
description = "List of permissions to grant on this bucket, grouped by key name.";
|
|
|
|
type = attrsOf (listOf (enum [ "read" "write" "owner" ]));
|
|
|
|
default = {};
|
2023-09-03 22:21:31 +03:00
|
|
|
};
|
2023-09-04 03:50:09 +03:00
|
|
|
quotas = {
|
|
|
|
maxObjects = mkOption {
|
|
|
|
description = "Maximum number of objects in this bucket. Null for unlimited.";
|
|
|
|
type = nullOr ints.positive;
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
maxSize = mkOption {
|
|
|
|
description = "Maximum size of this bucket in bytes. Null for unlimited.";
|
|
|
|
type = nullOr ints.positive;
|
|
|
|
default = null;
|
|
|
|
};
|
2023-09-03 22:21:31 +03:00
|
|
|
};
|
2024-07-04 23:06:25 +03:00
|
|
|
web.enable = mkEnableOption "website access for this bucket";
|
2023-09-03 22:21:31 +03:00
|
|
|
};
|
|
|
|
});
|
2023-09-04 03:50:09 +03:00
|
|
|
default = {};
|
2023-09-03 22:21:31 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
system.extraIncantations = {
|
2023-11-02 20:53:07 +02:00
|
|
|
runGarage = i: script: i.execShellWith [ config.services.garage.package pkgs.gnugrep ] ''
|
2023-09-04 03:50:09 +03:00
|
|
|
source ${garageShellLibrary}
|
2023-09-03 22:21:31 +03:00
|
|
|
waitForGarage
|
|
|
|
${script}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-09-04 03:50:09 +03:00
|
|
|
systemd.services = {
|
|
|
|
garage-layout-init = {
|
|
|
|
distributed.enable = true;
|
|
|
|
wantedBy = [ "garage.service" "multi-user.target" ];
|
|
|
|
wants = [ "garage.service" ];
|
|
|
|
after = [ "garage.service" ];
|
|
|
|
path = [ config.services.garage.package ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
TimeoutStartSec = "1800s";
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "10s";
|
|
|
|
};
|
|
|
|
script = ''
|
|
|
|
source ${garageShellLibrary}
|
|
|
|
waitForGarage
|
|
|
|
|
|
|
|
if [[ "$(garage layout show | grep -m1 '^Current cluster layout version:' | cut -d: -f2 | tr -d ' ')" != "0" ]]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
2023-09-03 22:21:31 +03:00
|
|
|
|
2023-09-04 03:50:09 +03:00
|
|
|
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: layout: ''
|
|
|
|
garage layout assign -z '${layout.zone}' -c '${toString layout.capacity}' "$(getNodeId '${name}')"
|
|
|
|
'') cfg.layout.initial)}
|
|
|
|
|
|
|
|
garage layout apply --version 1
|
|
|
|
'';
|
2023-09-03 22:21:31 +03:00
|
|
|
};
|
2024-08-05 00:44:02 +03:00
|
|
|
garage-ready = {
|
2023-09-04 03:50:09 +03:00
|
|
|
wants = [ "garage.service" ];
|
|
|
|
after = [ "garage.service" "garage-layout-init.service" ];
|
|
|
|
path = [ config.services.garage.package ];
|
2023-09-03 22:21:31 +03:00
|
|
|
|
2023-09-04 03:50:09 +03:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
TimeoutStartSec = "1800s";
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "10s";
|
|
|
|
};
|
|
|
|
script = ''
|
|
|
|
source ${garageShellLibrary}
|
|
|
|
waitForGarageOperational
|
2024-08-05 00:44:02 +03:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2023-09-03 22:21:31 +03:00
|
|
|
|
2024-08-05 00:44:02 +03:00
|
|
|
services.incandescence.providers.garage = {
|
|
|
|
locksmith = true;
|
|
|
|
wantedBy = [ "garage.service" "multi-user.target" ];
|
|
|
|
partOf = [ "garage.service" ];
|
|
|
|
wants = [ "garage-ready.service" ];
|
|
|
|
after = [ "garage-ready.service" ];
|
|
|
|
|
|
|
|
packages = [
|
|
|
|
config.services.garage.package
|
|
|
|
];
|
|
|
|
formulae = {
|
|
|
|
key = {
|
|
|
|
destroyAfterDays = 0;
|
|
|
|
create = key: ''
|
|
|
|
# don't print secret key
|
|
|
|
garage key new --name ${lib.escapeShellArg key} >/dev/null
|
|
|
|
echo Key ${lib.escapeShellArg key} was created.
|
|
|
|
'';
|
|
|
|
destroy = ''
|
|
|
|
garage key delete --yes "$OBJECT"
|
|
|
|
'';
|
|
|
|
change = key: let
|
|
|
|
kCfg = cfg.keys.${key};
|
|
|
|
in ''
|
|
|
|
garage key ${if kCfg.allow.createBucket then "allow" else "deny"} ${lib.escapeShellArg key} --create-bucket >/dev/null
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
bucket = {
|
|
|
|
deps = [ "key" ];
|
|
|
|
destroyAfterDays = 30;
|
|
|
|
create = bucket: ''
|
|
|
|
garage bucket create ${lib.escapeShellArg bucket}
|
|
|
|
'';
|
|
|
|
destroy = ''
|
|
|
|
garage bucket delete --yes "$OBJECT"
|
|
|
|
'';
|
|
|
|
change = bucket: let
|
|
|
|
bCfg = cfg.buckets.${bucket};
|
|
|
|
in ''
|
|
|
|
# permissions
|
|
|
|
${lib.concatStringsSep "\n" (lib.flatten (
|
2023-09-04 03:50:09 +03:00
|
|
|
lib.mapAttrsToList (key: perms: ''
|
2024-08-05 00:44:02 +03:00
|
|
|
garage bucket allow ${lib.escapeShellArg bucket} --key ${lib.escapeShellArg key} ${lib.escapeShellArgs (map (x: "--${x}") perms)}
|
|
|
|
garage bucket deny ${lib.escapeShellArg bucket} --key ${lib.escapeShellArg key} ${lib.escapeShellArgs (map (x: "--${x}") (lib.subtractLists perms [ "read" "write" "owner" ]))}
|
2023-09-04 03:50:09 +03:00
|
|
|
'') bCfg.allow
|
2024-08-05 00:44:02 +03:00
|
|
|
))}
|
2023-09-04 03:50:09 +03:00
|
|
|
|
2024-08-05 00:44:02 +03:00
|
|
|
# quotas
|
|
|
|
garage bucket set-quotas ${lib.escapeShellArg bucket} \
|
|
|
|
--max-objects '${if bCfg.quotas.maxObjects == null then "none" else toString bCfg.quotas.maxObjects}' \
|
|
|
|
--max-size '${if bCfg.quotas.maxSize == null then "none" else toString bCfg.quotas.maxSize}'
|
2024-07-04 23:06:25 +03:00
|
|
|
|
2024-08-05 00:44:02 +03:00
|
|
|
# website access
|
|
|
|
garage bucket website ${if bCfg.web.enable then "--allow" else "--deny"} ${lib.escapeShellArg bucket}
|
|
|
|
'';
|
|
|
|
};
|
2023-09-04 03:50:09 +03:00
|
|
|
};
|
2023-09-03 22:21:31 +03:00
|
|
|
};
|
2024-07-10 23:31:57 +03:00
|
|
|
|
|
|
|
services.locksmith.providers.garage = {
|
|
|
|
secrets = lib.mkMerge (lib.mapAttrsToList (key: kCfg: let
|
|
|
|
common = {
|
|
|
|
inherit (kCfg.locksmith) mode owner group nodes;
|
|
|
|
};
|
|
|
|
getKeyID = "${cfg.package}/bin/garage key info ${lib.escapeShellArg key} | grep -m1 'Key ID:' | cut -d ' ' -f3";
|
|
|
|
getSecretKey = "${cfg.package}/bin/garage key info ${lib.escapeShellArg key} | grep -m1 'Secret key:' | cut -d ' ' -f3";
|
|
|
|
in if kCfg.locksmith.format == "files" then {
|
|
|
|
"${key}-id" = common // {
|
|
|
|
command = getKeyID;
|
|
|
|
};
|
|
|
|
"${key}-secret" = common // {
|
|
|
|
command = getSecretKey;
|
|
|
|
};
|
|
|
|
} else let
|
|
|
|
template = pkgs.writeText "garage-key-template" {
|
|
|
|
aws = ''
|
|
|
|
[default]
|
|
|
|
aws_access_key_id=@@GARAGE_KEY_ID@@
|
|
|
|
aws_secret_access_key=@@GARAGE_SECRET_KEY@@
|
|
|
|
'';
|
|
|
|
envFile = ''
|
|
|
|
AWS_ACCESS_KEY_ID=@@GARAGE_KEY_ID@@
|
|
|
|
AWS_SECRET_ACCESS_KEY=@@GARAGE_SECRET_KEY@@
|
|
|
|
'';
|
2024-08-03 03:45:19 +03:00
|
|
|
s3ql = ''
|
|
|
|
[s3c]
|
|
|
|
storage-url: s3c4://
|
|
|
|
backend-login: @@GARAGE_KEY_ID@@
|
|
|
|
backend-password: @@GARAGE_SECRET_KEY@@
|
|
|
|
'';
|
2024-07-10 23:31:57 +03:00
|
|
|
}.${kCfg.locksmith.format};
|
|
|
|
in {
|
|
|
|
${key} = common // {
|
|
|
|
command = pkgs.writeShellScript "garage-render-key-template" ''
|
|
|
|
tmpFile="$(mktemp -ut garageKeyTemplate-XXXXXXXXXXXXXXXX)"
|
|
|
|
cp ${template} "$tmpFile"
|
|
|
|
trap "rm -f $tmpFile" EXIT
|
|
|
|
chmod 600 "$tmpFile"
|
|
|
|
${getKeyID} | ${pkgs.replace-secret}/bin/replace-secret '@@GARAGE_KEY_ID@@' /dev/stdin "$tmpFile"
|
|
|
|
${getSecretKey} | ${pkgs.replace-secret}/bin/replace-secret '@@GARAGE_SECRET_KEY@@' /dev/stdin "$tmpFile"
|
|
|
|
cat "$tmpFile"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}) cfg.keys);
|
|
|
|
};
|
2023-09-03 22:21:31 +03:00
|
|
|
};
|
|
|
|
}
|