@@ -45,7 +45,7 @@ let
4545
4646 cmdlineArgs = cfg.extraFlags ++ [
4747 "--storage.tsdb.path=${workingDir}/data/"
48− "--config.file=${prometheusYml}"
48+ "--config.file=/run/prometheus/prometheus-substituted.yaml"
4949 "--web.listen-address=${cfg.listenAddress}:${builtins.toString cfg.port}"
5050 "--alertmanager.notification-queue-capacity=${toString cfg.alertmanagerNotificationQueueCapacity}"
5151 "--alertmanager.timeout=${toString cfg.alertmanagerTimeout}s"
@@ -522,6 +522,45 @@ in {
522522 '';
523523 };
524524
525+ environmentFile = mkOption {
526+ type = types.nullOr types.path;
527+ default = null;
528+ example = "/root/prometheus.env";
529+ description = ''
530+ Environment file as defined in <citerefentry>
531+ <refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum>
532+ </citerefentry>.
533+
534+ Secrets may be passed to the service without adding them to the
535+ world-readable Nix store, by specifying placeholder variables as
536+ the option value in Nix and setting these variables accordingly in the
537+ environment file.
538+
539+ Environment variables from this file will be interpolated into the
540+ config file using envsubst with this syntax:
541+ <literal>$ENVIRONMENT ''${VARIABLE}</literal>
542+
543+ <programlisting>
544+ # Example scrape config entry handling an OAuth bearer token
545+ {
546+ job_name = "home_assistant";
547+ metrics_path = "/api/prometheus";
548+ scheme = "https";
549+ bearer_token = "\''${HOME_ASSISTANT_BEARER_TOKEN}";
550+ [...]
551+ }
552+ </programlisting>
553+
554+ <programlisting>
555+ # Content of the environment file
556+ HOME_ASSISTANT_BEARER_TOKEN=someoauthbearertoken
557+ </programlisting>
558+
559+ Note that this file needs to be available on the host on which
560+ <literal>Prometheus</literal> is running.
561+ '';
562+ };
563+
525564 configText = mkOption {
526565 type = types.nullOr types.lines;
527566 default = null;
@@ -662,12 +701,19 @@ in {
662701 systemd.services.prometheus = {
663702 wantedBy = [ "multi-user.target" ];
664703 after = [ "network.target" ];
704+ preStart = ''
705+ ${lib.getBin pkgs.envsubst}/bin/envsubst -o "/run/prometheus/prometheus-substituted.yaml" \
706+ -i "${prometheusYml}"
707+ '';
665708 serviceConfig = {
666709 ExecStart = "${cfg.package}/bin/prometheus" +
667710 optionalString (length cmdlineArgs != 0) (" \\\n " +
668711 concatStringsSep " \\\n " cmdlineArgs);
669712 User = "prometheus";
670713 Restart = "always";
714+ EnvironmentFile = mkIf (cfg.environmentFile != null) [ cfg.environmentFile ];
715+ RuntimeDirectory = "prometheus";
716+ RuntimeDirectoryMode = "0700";
671717 WorkingDirectory = workingDir;
672718 StateDirectory = cfg.stateDir;
673719 };