2022-09-07 22:04:27 +02:00
{ config , lib , pkgs , . . . }:
with lib ;
let
cfg = config . services . garage ;
2023-10-17 14:52:38 +02:00
toml = pkgs . formats . toml { } ;
2022-09-07 22:04:27 +02:00
configFile = toml . generate " g a r a g e . t o m l " cfg . settings ;
in
{
2022-10-15 22:40:34 +02:00
meta = {
2023-01-25 00:33:40 +01:00
doc = ./garage.md ;
2022-10-15 22:40:34 +02:00
maintainers = with pkgs . lib . maintainers ; [ raitobezarius ] ;
} ;
2022-09-07 22:04:27 +02:00
options . services . garage = {
enable = mkEnableOption ( lib . mdDoc " G a r a g e O b j e c t S t o r a g e ( S 3 c o m p a t i b l e ) " ) ;
extraEnvironment = mkOption {
type = types . attrsOf types . str ;
description = lib . mdDoc " E x t r a e n v i r o n m e n t v a r i a b l e s t o p a s s t o t h e G a r a g e s e r v e r . " ;
2023-10-17 14:52:38 +02:00
default = { } ;
example = { RUST_BACKTRACE = " y e s " ; } ;
2022-09-07 22:04:27 +02:00
} ;
2023-09-24 10:04:24 +02:00
environmentFile = mkOption {
type = types . nullOr types . path ;
description = lib . mdDoc " F i l e c o n t a i n i n g e n v i r o n m e n t v a r i a b l e s t o b e p a s s e d t o t h e G a r a g e s e r v e r . " ;
default = null ;
} ;
2022-09-07 22:04:27 +02:00
logLevel = mkOption {
2024-02-13 20:26:29 +01:00
type = types . enum ( [ " e r r o r " " w a r n " " i n f o " " d e b u g " " t r a c e " ] ) ;
2022-09-07 22:04:27 +02:00
default = " i n f o " ;
example = " d e b u g " ;
description = lib . mdDoc " G a r a g e l o g l e v e l , s e e < h t t p s : / / g a r a g e h q . d e u x f l e u r s . f r / d o c u m e n t a t i o n / q u i c k - s t a r t / # l a u n c h i n g - t h e - g a r a g e - s e r v e r > f o r e x a m p l e s . " ;
} ;
settings = mkOption {
type = types . submodule {
freeformType = toml . type ;
options = {
metadata_dir = mkOption {
default = " / v a r / l i b / g a r a g e / m e t a " ;
type = types . path ;
description = lib . mdDoc " T h e m e t a d a t a d i r e c t o r y , p u t t h i s o n a f a s t d i s k ( e . g . S S D ) i f p o s s i b l e . " ;
} ;
data_dir = mkOption {
default = " / v a r / l i b / g a r a g e / d a t a " ;
type = types . path ;
description = lib . mdDoc " T h e m a i n d a t a s t o r a g e , p u t t h i s o n y o u r l a r g e s t o r a g e ( e . g . h i g h c a p a c i t y H D D ) " ;
} ;
replication_mode = mkOption {
default = " n o n e " ;
2023-06-01 17:03:42 +02:00
type = types . enum ( [ " n o n e " " 1 " " 2 " " 3 " " 2 - d a n g e r o u s " " 3 - d a n g e r o u s " " 3 - d e g r a d e d " 1 2 3 ] ) ;
2022-09-07 22:04:27 +02:00
apply = v : toString v ;
2023-04-06 11:40:38 +02:00
description = lib . mdDoc " G a r a g e r e p l i c a t i o n m o d e , d e f a u l t s t o n o n e , s e e : < h t t p s : / / g a r a g e h q . d e u x f l e u r s . f r / d o c u m e n t a t i o n / r e f e r e n c e - m a n u a l / c o n f i g u r a t i o n / # r e p l i c a t i o n - m o d e > f o r r e f e r e n c e . " ;
2022-09-07 22:04:27 +02:00
} ;
} ;
} ;
2023-04-06 11:40:38 +02:00
description = lib . mdDoc " G a r a g e c o n f i g u r a t i o n , s e e < h t t p s : / / g a r a g e h q . d e u x f l e u r s . f r / d o c u m e n t a t i o n / r e f e r e n c e - m a n u a l / c o n f i g u r a t i o n / > f o r r e f e r e n c e . " ;
2022-09-07 22:04:27 +02:00
} ;
package = mkOption {
type = types . package ;
2023-10-17 14:52:48 +02:00
description = lib . mdDoc " G a r a g e p a c k a g e t o u s e , n e e d s t o b e s e t e x p l i c i t l y . I f y o u a r e u p g r a d i n g f r o m a m a j o r v e r s i o n , p l e a s e r e a d N i x O S a n d G a r a g e r e l e a s e n o t e s f o r u p g r a d e i n s t r u c t i o n s . " ;
2022-09-07 22:04:27 +02:00
} ;
} ;
config = mkIf cfg . enable {
environment . etc . " g a r a g e . t o m l " = {
source = configFile ;
} ;
2024-03-09 18:35:53 +01:00
# For administration
environment . systemPackages = [
( pkgs . writeScriptBin " g a r a g e " ''
# make it so all future variables set are automatically exported as environment variables
set - a
# source the set environmentFile (since systemd EnvironmentFile is supposed to be a minor subset of posix sh parsing) (with shell arg escaping to avoid quoting issues)
[ - f $ { lib . escapeShellArg cfg . environmentFile } ] && . $ { lib . escapeShellArg cfg . environmentFile }
# exec the program with quoted args (also with shell arg escaping for the program path to avoid quoting issues there)
exec $ { lib . escapeShellArg ( lib . getExe cfg . package ) } " $ @ "
'' )
] ;
2022-09-07 22:04:27 +02:00
systemd . services . garage = {
description = " G a r a g e O b j e c t S t o r a g e ( S 3 c o m p a t i b l e ) " ;
after = [ " n e t w o r k . t a r g e t " " n e t w o r k - o n l i n e . t a r g e t " ] ;
wants = [ " n e t w o r k . t a r g e t " " n e t w o r k - o n l i n e . t a r g e t " ] ;
wantedBy = [ " m u l t i - u s e r . t a r g e t " ] ;
2023-09-24 10:04:24 +02:00
restartTriggers = [ configFile ] ++ ( lib . optional ( cfg . environmentFile != null ) cfg . environmentFile ) ;
2022-09-07 22:04:27 +02:00
serviceConfig = {
ExecStart = " ${ cfg . package } / b i n / g a r a g e s e r v e r " ;
2023-10-24 23:58:13 +02:00
StateDirectory = mkIf ( hasPrefix " / v a r / l i b / g a r a g e " cfg . settings . data_dir || hasPrefix " / v a r / l i b / g a r a g e " cfg . settings . metadata_dir ) " g a r a g e " ;
2022-09-07 22:04:27 +02:00
DynamicUser = lib . mkDefault true ;
ProtectHome = true ;
NoNewPrivileges = true ;
2023-09-24 10:04:24 +02:00
EnvironmentFile = lib . optional ( cfg . environmentFile != null ) cfg . environmentFile ;
2022-09-07 22:04:27 +02:00
} ;
environment = {
RUST_LOG = lib . mkDefault " g a r a g e = ${ cfg . logLevel } " ;
} // cfg . extraEnvironment ;
} ;
} ;
}