@ -5,6 +5,9 @@ with lib;
let
cfg = config . services . logrotate ;
# deprecated legacy compat settings
# these options will be removed before 22.11 in the following PR:
# https://github.com/NixOS/nixpkgs/pull/164169
pathOpts = { name , . . . }: {
options = {
enable = mkOption {
@ -86,27 +89,77 @@ let
config . name = name ;
} ;
mkConf = pathOpts : ''
# generated by NixOS using the `services.logrotate.paths.${pathOpts.name}` attribute set
$ { concatMapStringsSep " " ( path : '' " ${ path } " '' ) ( toList pathOpts . path ) } {
$ { optionalString ( pathOpts . user != null || pathOpts . group != null ) " s u ${ pathOpts . user } ${ pathOpts . group } " }
$ { pathOpts . frequency }
rotate $ { toString pathOpts . keep }
$ { pathOpts . extraConfig }
}
'' ;
paths = sortProperties ( attrValues ( filterAttrs ( _ : pathOpts : pathOpts . enable ) cfg . paths ) ) ;
configText = concatStringsSep " \n " (
[ " m i s s i n g o k " " n o t i f e m p t y " cfg . extraConfig ] ++ ( map mkConf paths )
generateLine = n : v :
if builtins . elem n [ " f i l e s " " p r i o r i t y " " e n a b l e " " g l o b a l " ] || v == null then null
else if builtins . elem n [ " e x t r a C o n f i g " " f r e q u e n c y " ] then " ${ v } \n "
else if builtins . elem n [ " f i r s t a c t i o n " " l a s t a c t i o n " " p r e r o t a t e " " p o s t r o t a t e " " p r e r e m o v e " ]
then " ${ n } \n ${ v } \n e n d s c r i p t \n "
else if isInt v then " ${ n } ${ toString v } \n "
else if v == true then " ${ n } \n "
else if v == false then " n o ${ n } \n "
else " ${ n } ${ v } \n " ;
generateSection = indent : settings : concatStringsSep ( fixedWidthString indent " " " " ) (
filter ( x : x != null ) ( mapAttrsToList generateLine settings )
) ;
# generateSection includes a final newline hence weird closing brace
mkConf = settings :
if settings . global or false then generateSection 0 settings
else ''
$ { concatMapStringsSep " \n " ( files : '' " ${ files } " '' ) ( toList settings . files ) } {
$ { generateSection 2 settings } }
'' ;
# below two mapPaths are compat functions
mapPathOptToSetting = n : v :
if n == " k e e p " then nameValuePair " r o t a t e " v
else if n == " p a t h " then nameValuePair " f i l e s " v
else nameValuePair n v ;
mapPathsToSettings = path : pathOpts :
nameValuePair path (
filterAttrs ( n : v : ! builtins . elem n [ " u s e r " " g r o u p " " n a m e " ] && v != " " ) (
( mapAttrs' mapPathOptToSetting pathOpts ) //
{
su =
if pathOpts . user != null
then " ${ pathOpts . user } ${ pathOpts . group } "
else null ;
}
)
) ;
settings = sortProperties ( attrValues ( filterAttrs ( _ : settings : settings . enable ) (
foldAttrs recursiveUpdate { } [
{
header = {
enable = true ;
missingok = true ;
notifempty = true ;
frequency = " w e e k l y " ;
rotate = 4 ;
} ;
# compat section
extraConfig = {
enable = ( cfg . extraConfig != " " ) ;
global = true ;
extraConfig = cfg . extraConfig ;
priority = 101 ;
} ;
}
( mapAttrs' mapPathsToSettings cfg . paths )
cfg . settings
{ header = { global = true ; priority = 100 ; } ; }
]
) ) ) ;
configFile = pkgs . writeText " l o g r o t a t e . c o n f " (
concatStringsSep " \n " (
map mkConf settings
)
) ;
configFile = pkgs . writeText " l o g r o t a t e . c o n f " configText ;
mailOption =
# add mail option to service if a mail is requested in config
# this ugly match will be replaced by cleaner attribute check in
# the near future
if builtins . match " ( . * [ [ : s p a c e : ] ] ) ? m a i l [ [ : s p a c e : ] ] . * " configText != null
if foldr ( n : a : a || n ? mail ) false ( attrValues cfg . settings )
then " - - m a i l = ${ pkgs . mailutils } / b i n / m a i l "
else " " ;
in
@ -118,8 +171,68 @@ in
options = {
services . logrotate = {
enable = mkEnableOption " t h e l o g r o t a t e s y s t e m d s e r v i c e " // {
default = foldr ( n : a : a || n . enable ) false ( attrValues cfg . paths ) ;
defaultText = literalExpression " c f g . p a t h s ! = { } " ;
default = foldr ( n : a : a || n . enable ) false ( attrValues cfg . settings ) ;
defaultText = literalExpression " c f g . s e t t i n g s ! = { } " ;
} ;
settings = mkOption {
default = { } ;
description = ''
logrotate freeform settings : each attribute here will define its own section ,
ordered by priority , which can either define files to rotate with their settings
or settings common to all further files settings .
Refer to < link xlink:href= " h t t p s : / / l i n u x . d i e . n e t / m a n / 8 / l o g r o t a t e " / > for details .
'' ;
type = types . attrsOf ( types . submodule ( { name , . . . }: {
freeformType = with types ; attrsOf ( nullOr ( oneOf [ int bool str ] ) ) ;
options = {
enable = mkEnableOption " s e t t i n g i n d i v i d u a l k i l l s w i t c h " // {
default = true ;
} ;
global = mkOption {
type = types . bool ;
default = false ;
description = ''
Whether this setting is a global option or not : set to have these
settings apply to all files settings with a higher priority .
'' ;
} ;
files = mkOption {
type = with types ; either str ( listOf str ) ;
default = name ;
defaultText = ''
The attrset name if not specified
'' ;
description = ''
Single or list of files for which rules are defined .
The files are quoted with double-quotes in logrotate configuration ,
so globs and spaces are supported .
Note this setting is ignored if globals is true .
'' ;
} ;
frequency = mkOption {
type = types . nullOr types . str ;
default = null ;
description = ''
How often to rotate the logs . Defaults to previously set global setting ,
which itself defauts to weekly .
'' ;
} ;
priority = mkOption {
type = types . int ;
default = 1000 ;
description = ''
Order of this logrotate block in relation to the others . The semantics are
the same as with ` lib . mkOrder ` . Smaller values are inserted first .
'' ;
} ;
} ;
} ) ) ;
} ;
configFile = mkOption {
@ -130,7 +243,7 @@ in
'' ;
description = ''
Override the configuration file used by MySQL . By default ,
NixOS generates one automatically from <option> services . logrotate . settings < /option > .
NixOS generates one automatically from < xref linkend = " o p t - s e r v i c e s . l o g r o t a t e . s e t t i n g s " / > .
'' ;
example = literalExpression ''
pkgs . writeText " l o g r o t a t e . c o n f " '' '
@ -143,6 +256,7 @@ in
'' ;
} ;
# deprecated legacy compat settings
paths = mkOption {
type = with types ; attrsOf ( submodule pathOpts ) ;
default = { } ;
@ -150,6 +264,7 @@ in
Attribute set of paths to rotate . The order each block appears in the generated configuration file
can be controlled by the < link linkend = " o p t - s e r v i c e s . l o g r o t a t e . p a t h s . _ n a m e _ . p r i o r i t y " > priority < /link > option
using the same semantics as ` lib . mkOrder ` . Smaller values have a greater priority .
This setting has been deprecated in favor of < link linkend = " o p t - s e r v i c e s . l o g r o t a t e . s e t t i n g s " > logrotate settings < /link > .
'' ;
example = literalExpression ''
{
@ -178,22 +293,37 @@ in
description = ''
Extra contents to append to the logrotate configuration file . Refer to
< link xlink:href= " h t t p s : / / l i n u x . d i e . n e t / m a n / 8 / l o g r o t a t e " / > for details .
This setting has been deprecated in favor of
< link linkend = " o p t - s e r v i c e s . l o g r o t a t e . s e t t i n g s " > logrotate settings < /link > .
'' ;
} ;
} ;
} ;
config = mkIf cfg . enable {
assertions = mapAttrsToList
( name : pathOpts :
{
assertion = ( pathOpts . user != null ) == ( pathOpts . group != null ) ;
message = ''
If either of ` services . logrotate . paths . ${ name } . user ` or ` services . logrotate . paths . ${ name } . group ` are specified then * both * must be specified .
'' ;
}
)
cfg . paths ;
assertions =
mapAttrsToList
( name : pathOpts :
{
assertion = ( pathOpts . user != null ) == ( pathOpts . group != null ) ;
message = ''
If either of ` services . logrotate . paths . ${ name } . user ` or ` services . logrotate . paths . ${ name } . group ` are specified then * both * must be specified .
'' ;
} )
cfg . paths ;
warnings =
( mapAttrsToList
( name : pathOpts : ''
Using config . services . logrotate . paths . ${ name } is deprecated and will become unsupported in a future release .
Please use services . logrotate . settings instead .
'' )
cfg . paths
) ++
( optional ( cfg . extraConfig != " " ) ''
Using config . services . logrotate . extraConfig is deprecated and will become unsupported in a future release .
Please use services . logrotate . settings with globals = true instead .
'' ) ;
systemd . services . logrotate = {
description = " L o g r o t a t e S e r v i c e " ;