Jump to content

Garage: Difference between revisions

From Official NixOS Wiki
Klinger (talk | contribs)
first version with link to nixos manual
 
Nicof2000 (talk | contribs)
mNo edit summary
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
   
   
[https://garagehq.deuxfleurs.fr/Garage] is an open-source, self-hostable S3 store, simpler than MinIO, for geodistributed stores.
[https://garagehq.deuxfleurs.fr Garage] is an open-source, self-hostable S3 store, for geodistributed stores.


It is documented in the [https://nixos.org/manual/nixos/stable/#module-services-garage NixOS manual].
It is documented in the [https://nixos.org/manual/nixos/stable/#module-services-garage NixOS manual].


==== Setup ====
You have to explicitly set the [https://search.nixos.org/options?query=services.garage&type=options#show=option%253Aservices.garage.package package option]. You can choose between the [https://search.nixos.org/packages?query=garage_ garage_1 and garage_2] packages.
For a minimal setup you need to configure rpc_bind_addr, s3_api.s3_region, replication_factor and rpc_secret_file.
The Garage [https://github.com/NixOS/nixpkgs/blob/0dc498bfe49baeb18b0f9891e800b53bb2603193/nixos/modules/services/web-servers/garage.nix#L145 systemd service has DynamicUser configured], which prevents you from setting the owner property of the secret.
Adding the secret without it like this:<syntaxhighlight lang="nixos">
{ config, ... }:
{
  sops.secrets."garage/rpc_secret" = { };
  services.garage.settings.rpc_secret_file = config.sops.secrets."garage/rpc_secret".path;
}
</syntaxhighlight>Results in: Error: Failed to read secret file /run/secrets/garage/rpc_secret: Permission denied (os error 13)
You can use [https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#Credentials systemd LoadCredential] to make it accessible for the dynamic user running the service:<syntaxhighlight lang="nixos">
{ config, ... }:
{
  sops.secrets."garage/rpc_secret" = { };
  services.garage.settings.rpc_secret_file = "/run/credentials/garage.service/rpc_secret";
  systemd.services.garage.serviceConfig.LoadCredential =
    "rpc_secret:${config.sops.secrets."garage/rpc_secret".path}";
}
</syntaxhighlight>However this results in another error in the Garage service:<syntaxhighlight lang="bash">
2026-11-24T00:20:43.071157Z  INFO garage::server: Loading configuration from /etc/garage.toml...
Error: File /run/credentials/garage.service/rpc_secret is world-readable! (mode: 0100440, expected 0600)
Refusing to start until this is fixed, or environment variable GARAGE_ALLOW_WORLD_READABLE_SECRETS is set to true.
</syntaxhighlight>This is because garage expects the file to have a umask of 600, not 440. To accept this umask (which is secure, as you can see in the following codeblock) you can set the allow_world_readable_secrets setting.<syntaxhighlight lang="bash"># systemctl start garage; getfacl /run/credentials/garage.service/rpc_secret
getfacl: Removing leading '/' from absolute path names
# file: run/credentials/garage.service/rpc_secret
# owner: root
# group: root
user::r--
user:garage:r--
group::---
mask::r--
other::---</syntaxhighlight>The final configuration looks like this:<syntaxhighlight lang="nixos" line="1">{ config, pkgs, ... }:
{
  sops.secrets."garage/rpc_secret" = { };
  services.garage = {
    enable = true;
    package = pkgs.garage_2;
    settings = {
      rpc_bind_addr = "[::1]:3901";
      replication_factor = 1;
      s3_api = {
        s3_region = "garage";
        api_bind_addr = "[::]:3900";
      };
      rpc_secret_file = "/run/credentials/garage.service/rpc_secret";
      allow_world_readable_secrets = true;
    };
  };
  systemd.services.garage.serviceConfig.LoadCredential =
    "rpc_secret:${config.sops.secrets."garage/rpc_secret".path}";
  networking.firewall.allowedTCPPorts = [ 3900 ];
}</syntaxhighlight>You can check the status of the garage server <syntaxhighlight lang="bash">
# sudo garage status
==== HEALTHY NODES ====
ID                Hostname  Address        Tags  Zone  Capacity          DataAvail  ResyncQueue  Version
8b03eb0fff755598  app      192.0.2.10:3901              NO ROLE ASSIGNED            0            cargo:2.5.0
</syntaxhighlight>Next you can create a layout and s3 bucket:<syntaxhighlight lang="bash">
sudo garage layout assign -z dc1 -c 1G 8b03eb0fff755598
sudo garage layout show
sudo garage layout apply --version 1
sudo garage bucket create garage-test-one
sudo garage key create garage-test-one
sudo garage bucket allow --read --write --owner garage-test-one --key garage-test-one
</syntaxhighlight>And test it for example using aws cli:<syntaxhighlight lang="bash">
export AWS_ACCESS_KEY_ID='...'
export AWS_SECRET_ACCESS_KEY='...'
export AWS_DEFAULT_REGION='garage'
aws --endpoint-url http://192.0.2.10:3900 s3 ls
echo "Test" > /tmp/test.txt
aws --endpoint-url http://192.0.2.10:3900 s3 cp /tmp/test.txt s3://garage-test-one/
aws --endpoint-url http://192.0.2.10:3900 s3 ls s3://garage-test-one/
aws --endpoint-url http://192.0.2.10:3900 s3 cp s3://garage-test-one/ test.txt
cat test.txt
aws --endpoint-url http://192.0.2.10:3900 s3 rm s3://garage-test-one/test.txt
</syntaxhighlight>Please note that this uses HTTP, which transmits data in clear text and is insecure.
You want to configure a reverse proxy (e.g. nginx) which handels tls termination:<syntaxhighlight lang="nixos">
{
  services = {
    garage.settings.s3_api.api_bind_addr = "[::1]:3900";
    nginx = {
      enable = true;
      virtualHosts."s3.example.com" = {
        locations."/".proxyPass = "http://[::1]:3900";
        serverName = "s3.example.com";
        forceSSL = true;
        enableACME = true; # use ACME HTTP-01 challenge
      };
    };
  };
  security.acme.acceptTerms = true;
  networking.firewall.allowedTCPPorts = [ 80 443 ];
}
</syntaxhighlight>
[[Category:Server]]
[[Category:Server]]
[[Category:NixOS Manual]]
[[Category:NixOS Manual]]

Latest revision as of 01:48, 24 November 2026

Garage is an open-source, self-hostable S3 store, for geodistributed stores.

It is documented in the NixOS manual.

Setup

You have to explicitly set the package option. You can choose between the garage_1 and garage_2 packages.

For a minimal setup you need to configure rpc_bind_addr, s3_api.s3_region, replication_factor and rpc_secret_file.

The Garage systemd service has DynamicUser configured, which prevents you from setting the owner property of the secret.

Adding the secret without it like this:

{ config, ... }:
{
  sops.secrets."garage/rpc_secret" = { };
  services.garage.settings.rpc_secret_file = config.sops.secrets."garage/rpc_secret".path;
}

Results in: Error: Failed to read secret file /run/secrets/garage/rpc_secret: Permission denied (os error 13) You can use systemd LoadCredential to make it accessible for the dynamic user running the service:

{ config, ... }:
{
  sops.secrets."garage/rpc_secret" = { };
  services.garage.settings.rpc_secret_file = "/run/credentials/garage.service/rpc_secret";
  systemd.services.garage.serviceConfig.LoadCredential =
    "rpc_secret:${config.sops.secrets."garage/rpc_secret".path}";
}

However this results in another error in the Garage service:

2026-11-24T00:20:43.071157Z  INFO garage::server: Loading configuration from /etc/garage.toml...
Error: File /run/credentials/garage.service/rpc_secret is world-readable! (mode: 0100440, expected 0600)
Refusing to start until this is fixed, or environment variable GARAGE_ALLOW_WORLD_READABLE_SECRETS is set to true.

This is because garage expects the file to have a umask of 600, not 440. To accept this umask (which is secure, as you can see in the following codeblock) you can set the allow_world_readable_secrets setting.

# systemctl start garage; getfacl /run/credentials/garage.service/rpc_secret
getfacl: Removing leading '/' from absolute path names
# file: run/credentials/garage.service/rpc_secret
# owner: root
# group: root
user::r--
user:garage:r--
group::---
mask::r--
other::---

The final configuration looks like this:

{ config, pkgs, ... }:
{
  sops.secrets."garage/rpc_secret" = { };

  services.garage = {
    enable = true;
    package = pkgs.garage_2;
    settings = {
      rpc_bind_addr = "[::1]:3901";
      replication_factor = 1;
      s3_api = {
        s3_region = "garage";
        api_bind_addr = "[::]:3900";
      };
      rpc_secret_file = "/run/credentials/garage.service/rpc_secret";
      allow_world_readable_secrets = true;
    };
  };

  systemd.services.garage.serviceConfig.LoadCredential =
    "rpc_secret:${config.sops.secrets."garage/rpc_secret".path}";

  networking.firewall.allowedTCPPorts = [ 3900 ];
}

You can check the status of the garage server

# sudo garage status
==== HEALTHY NODES ====
ID                Hostname  Address         Tags  Zone  Capacity          DataAvail  ResyncQueue  Version
8b03eb0fff755598  app       192.0.2.10:3901              NO ROLE ASSIGNED             0            cargo:2.5.0

Next you can create a layout and s3 bucket:

sudo garage layout assign -z dc1 -c 1G 8b03eb0fff755598
sudo garage layout show
sudo garage layout apply --version 1

sudo garage bucket create garage-test-one
sudo garage key create garage-test-one
sudo garage bucket allow --read --write --owner garage-test-one --key garage-test-one

And test it for example using aws cli:

export AWS_ACCESS_KEY_ID='...'
export AWS_SECRET_ACCESS_KEY='...'
export AWS_DEFAULT_REGION='garage'

aws --endpoint-url http://192.0.2.10:3900 s3 ls
echo "Test" > /tmp/test.txt
aws --endpoint-url http://192.0.2.10:3900 s3 cp /tmp/test.txt s3://garage-test-one/
aws --endpoint-url http://192.0.2.10:3900 s3 ls s3://garage-test-one/
aws --endpoint-url http://192.0.2.10:3900 s3 cp s3://garage-test-one/ test.txt
cat test.txt
aws --endpoint-url http://192.0.2.10:3900 s3 rm s3://garage-test-one/test.txt

Please note that this uses HTTP, which transmits data in clear text and is insecure. You want to configure a reverse proxy (e.g. nginx) which handels tls termination:

{
  services = {
    garage.settings.s3_api.api_bind_addr = "[::1]:3900";
    nginx = {
      enable = true;
      virtualHosts."s3.example.com" = {
        locations."/".proxyPass = "http://[::1]:3900";
        serverName = "s3.example.com";
        forceSSL = true;
        enableACME = true; # use ACME HTTP-01 challenge
      };
    };
  };
  security.acme.acceptTerms = true;
  networking.firewall.allowedTCPPorts = [ 80 443 ];
}