@ -9,13 +9,6 @@ let
prometheusYmlOut = " ${ workingDir } / p r o m e t h e u s - s u b s t i t u t e d . y a m l " ;
writeConfig = pkgs . writeShellScriptBin " w r i t e - p r o m e t h e u s - c o n f i g " ''
PATH = " ${ makeBinPath ( with pkgs ; [ coreutils envsubst ] ) } "
touch ' $ { prometheusYmlOut } '
chmod 600 ' $ { prometheusYmlOut } '
envsubst - o ' $ { prometheusYmlOut } ' - i ' $ { prometheusYml } '
'' ;
triggerReload = pkgs . writeShellScriptBin " t r i g g e r - r e l o a d - p r o m e t h e u s " ''
PATH = " ${ makeBinPath ( with pkgs ; [ systemd ] ) } "
if systemctl - q is-active prometheus . service ; then
@ -76,8 +69,8 @@ let
" - - s t o r a g e . t s d b . p a t h = ${ workingDir } / d a t a / "
" - - c o n f i g . f i l e = ${
if cfg . enableReload
then prometheusYmlOut
else " / r u n / p r o m e t h e u s / p r o m e t h e u s - s u b s t i t u t e d . y a m l "
then " / e t c / p r o m e t h e u s / p r o m e t h e u s . y a m l "
else prometheusYml
} "
" - - w e b . l i s t e n - a d d r e s s = ${ cfg . listenAddress } : ${ builtins . toString cfg . port } "
" - - a l e r t m a n a g e r . n o t i f i c a t i o n - q u e u e - c a p a c i t y = ${ toString cfg . alertmanagerNotificationQueueCapacity } "
@ -1625,51 +1618,6 @@ in
( <literal> switch-to-configuration < /literal > ) that changes the prometheus
configuration only finishes successully when prometheus has finished
loading the new configuration .
Note that prometheus will also get reloaded when the location of the
<option> environmentFile < /option > changes but not when its contents
changes . So when you change it contents make sure to reload prometheus
manually or include the hash of <option> environmentFile < /option > in its
name .
'' ;
} ;
environmentFile = mkOption {
type = types . nullOr types . path ;
default = null ;
example = " / r o o t / p r o m e t h e u s . e n v " ;
description = ''
Environment file as defined in <citerefentry>
<refentrytitle> systemd . exec < /refentrytitle > <manvolnum> 5 < /manvolnum >
< /citerefentry > .
Secrets may be passed to the service without adding them to the
world-readable Nix store , by specifying placeholder variables as
the option value in Nix and setting these variables accordingly in the
environment file .
Environment variables from this file will be interpolated into the
config file using envsubst with this syntax :
<literal> $ ENVIRONMENT '' ${ VARIABLE } < / l i t e r a l >
<programlisting>
# Example scrape config entry handling an OAuth bearer token
{
job_name = " h o m e _ a s s i s t a n t " ;
metrics_path = " / a p i / p r o m e t h e u s " ;
scheme = " h t t p s " ;
bearer_token = " \' ' ${ HOME_ASSISTANT_BEARER_TOKEN } " ;
[ . . . ]
}
< /programlisting >
<programlisting>
# Content of the environment file
HOME_ASSISTANT_BEARER_TOKEN = someoauthbearertoken
< /programlisting >
Note that this file needs to be available on the host on which
<literal> Prometheus < /literal > is running .
'' ;
} ;
@ -1830,13 +1778,12 @@ in
uid = config . ids . uids . prometheus ;
group = " p r o m e t h e u s " ;
} ;
environment . etc . " p r o m e t h e u s / p r o m e t h e u s . y a m l " = mkIf cfg . enableReload {
source = prometheusYml ;
} ;
systemd . services . prometheus = {
wantedBy = [ " m u l t i - u s e r . t a r g e t " ] ;
after = [ " n e t w o r k . t a r g e t " ] ;
preStart = mkIf ( ! cfg . enableReload ) ''
$ { lib . getBin pkgs . envsubst } /bin/envsubst - o " / r u n / p r o m e t h e u s / p r o m e t h e u s - s u b s t i t u t e d . y a m l " \
- i " ${ prometheusYml } "
'' ;
serviceConfig = {
ExecStart = " ${ cfg . package } / b i n / p r o m e t h e u s " +
optionalString ( length cmdlineArgs != 0 ) ( " \\ \n " +
@ -1844,7 +1791,6 @@ in
ExecReload = mkIf cfg . enableReload " + ${ reload } / b i n / r e l o a d - p r o m e t h e u s " ;
User = " p r o m e t h e u s " ;
Restart = " a l w a y s " ;
EnvironmentFile = mkIf ( cfg . environmentFile != null && ! cfg . enableReload ) [ cfg . environmentFile ] ;
RuntimeDirectory = " p r o m e t h e u s " ;
RuntimeDirectoryMode = " 0 7 0 0 " ;
WorkingDirectory = workingDir ;
@ -1852,18 +1798,6 @@ in
StateDirectoryMode = " 0 7 0 0 " ;
} ;
} ;
systemd . services . prometheus-config-write = mkIf cfg . enableReload {
wantedBy = [ " p r o m e t h e u s . s e r v i c e " ] ;
before = [ " p r o m e t h e u s . s e r v i c e " ] ;
serviceConfig = {
Type = " o n e s h o t " ;
User = " p r o m e t h e u s " ;
StateDirectory = cfg . stateDir ;
StateDirectoryMode = " 0 7 0 0 " ;
EnvironmentFile = mkIf ( cfg . environmentFile != null ) [ cfg . environmentFile ] ;
ExecStart = " ${ writeConfig } / b i n / w r i t e - p r o m e t h e u s - c o n f i g " ;
} ;
} ;
# prometheus-config-reload will activate after prometheus. However, what we
# don't want is that on startup it immediately reloads prometheus because
# prometheus itself might have just started.
@ -1873,26 +1807,19 @@ in
# harmless message and then stay active (RemainAfterExit).
#
# Then, when the config file has changed, switch-to-configuration notices
# that this service has changed and needs to be reloaded
# (reloadIfChanged). The reload command then actually writes the new config
# and reloads prometheus.
# that this service has changed (restartTriggers) and needs to be reloaded
# (reloadIfChanged). The reload command then reloads prometheus.
systemd . services . prometheus-config-reload = mkIf cfg . enableReload {
wantedBy = [ " p r o m e t h e u s . s e r v i c e " ] ;
after = [ " p r o m e t h e u s . s e r v i c e " ] ;
reloadIfChanged = true ;
restartTriggers = [ prometheusYml ] ;
serviceConfig = {
Type = " o n e s h o t " ;
User = " p r o m e t h e u s " ;
StateDirectory = cfg . stateDir ;
StateDirectoryMode = " 0 7 0 0 " ;
EnvironmentFile = mkIf ( cfg . environmentFile != null ) [ cfg . environmentFile ] ;
RemainAfterExit = true ;
TimeoutSec = 60 ;
ExecStart = " ${ pkgs . logger } / b i n / l o g g e r ' p r o m e t h e u s - c o n f i g - r e l o a d w i l l o n l y r e l o a d p r o m e t h e u s w h e n r e l o a d e d i t s e l f . ' " ;
ExecReload = [
" ${ writeConfig } / b i n / w r i t e - p r o m e t h e u s - c o n f i g "
" + ${ triggerReload } / b i n / t r i g g e r - r e l o a d - p r o m e t h e u s "
] ;
ExecReload = [ " ${ triggerReload } / b i n / t r i g g e r - r e l o a d - p r o m e t h e u s " ] ;
} ;
} ;
} ;