Jump to content

Garage: Difference between revisions

From Official NixOS Wiki
Nicof2000 (talk | contribs)
m setup/finalCode: change order to match text
Nicof2000 (talk | contribs)
m add test instructions
Line 52: Line 52:
       rpc_bind_addr = "[::]:3901";
       rpc_bind_addr = "[::]:3901";
       replication_factor = 1;
       replication_factor = 1;
       s3_api.s3_region = "garage";
       s3_api = {
        s3_region = "garage";
        api_bind_addr = "[::]:3900";
      };
       rpc_secret_file = "/run/credentials/garage.service/rpc_secret";
       rpc_secret_file = "/run/credentials/garage.service/rpc_secret";
       allow_world_readable_secrets = true;
       allow_world_readable_secrets = true;
Line 60: Line 63:
   systemd.services.garage.serviceConfig.LoadCredential =
   systemd.services.garage.serviceConfig.LoadCredential =
     "rpc_secret:${config.sops.secrets."garage/rpc_secret".path}";
     "rpc_secret:${config.sops.secrets."garage/rpc_secret".path}";
}</syntaxhighlight>
}</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
sudo garage key create garage-test
sudo garage bucket allow --read --write --owner garage-test --key garage-test
 
</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/
aws --endpoint-url http://192.0.2.10:3900 s3 ls s3://garage-test/
aws --endpoint-url http://192.0.2.10:3900 s3 cp s3://garage-test/ test.txt
cat test.txt
aws --endpoint-url http://192.0.2.10:3900 s3 rm s3://garage-test/test.txt
 
</syntaxhighlight>Be aware that this uses http, which transmits the content as cleartext and is insecure.
 
You want to configure a reverse proxy (e.g. nginx) which handels tls termination.
[[Category:Server]]
[[Category:Server]]
[[Category:NixOS Manual]]
[[Category:NixOS Manual]]

Revision as of 01:21, 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 = "[::]: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}";
}

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
sudo garage key create garage-test
sudo garage bucket allow --read --write --owner garage-test --key garage-test

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/
aws --endpoint-url http://192.0.2.10:3900 s3 ls s3://garage-test/
aws --endpoint-url http://192.0.2.10:3900 s3 cp s3://garage-test/ test.txt
cat test.txt
aws --endpoint-url http://192.0.2.10:3900 s3 rm s3://garage-test/test.txt

Be aware that this uses http, which transmits the content as cleartext and is insecure.

You want to configure a reverse proxy (e.g. nginx) which handels tls termination.