Skip to content

Commit

Permalink
nixos/garage: add user-given path to ReadWritePaths
Browse files Browse the repository at this point in the history
If the user has specified a custom data_dir or meta_dir, this results in
garage service failing with read-only filesystem error since the service
runs with DynamicUser by default.
  • Loading branch information
cything committed Jan 15, 2025
1 parent 514ec34 commit 0fe4739
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions nixos/modules/services/web-servers/garage.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ let
cfg = config.services.garage;
toml = pkgs.formats.toml { };
configFile = toml.generate "garage.toml" cfg.settings;

anyHasPrefix =
prefix: strOrList:
if isString strOrList then
hasPrefix prefix strOrList
else
any ({ path, ... }: hasPrefix prefix path) strOrList;
in
{
meta = {
Expand All @@ -44,13 +37,13 @@ in
};

logLevel = mkOption {
type = types.enum ([
type = types.enum [
"error"
"warn"
"info"
"debug"
"trace"
]);
];
default = "info";
example = "debug";
description = "Garage log level, see <https://garagehq.deuxfleurs.fr/documentation/quick-start/#launching-the-garage-server> for examples.";
Expand Down Expand Up @@ -125,18 +118,32 @@ in
restartTriggers = [
configFile
] ++ (lib.optional (cfg.environmentFile != null) cfg.environmentFile);
serviceConfig = {
ExecStart = "${cfg.package}/bin/garage server";

StateDirectory = mkIf (
anyHasPrefix "/var/lib/garage" cfg.settings.data_dir
|| hasPrefix "/var/lib/garage" cfg.settings.metadata_dir
) "garage";
DynamicUser = lib.mkDefault true;
ProtectHome = true;
NoNewPrivileges = true;
EnvironmentFile = lib.optional (cfg.environmentFile != null) cfg.environmentFile;
};
serviceConfig =
let
paths = lib.flatten (
with cfg.settings;
[
metadata_dir
]
# data_dir can either be a string or a list of attrs
# if data_dir is a list, the actual path will in in the `path` attribute of each item
# see https://garagehq.deuxfleurs.fr/documentation/reference-manual/configuration/#data_dir
++ lib.optional (lib.isList data_dir) (map (item: item.path) data_dir)
++ lib.optional (lib.isString data_dir) [ data_dir ]
);
isDefault = lib.hasPrefix "/var/lib/garage";
isDefaultStateDirectory = lib.any isDefault paths;
in
{
ExecStart = "${cfg.package}/bin/garage server";

StateDirectory = lib.mkIf isDefaultStateDirectory "garage";
DynamicUser = lib.mkDefault true;
ProtectHome = true;
NoNewPrivileges = true;
EnvironmentFile = lib.optional (cfg.environmentFile != null) cfg.environmentFile;
ReadWritePaths = lib.filter (x: !(isDefault x)) (lib.flatten [ paths ]);
};
environment = {
RUST_LOG = lib.mkDefault "garage=${cfg.logLevel}";
} // cfg.extraEnvironment;
Expand Down

0 comments on commit 0fe4739

Please sign in to comment.