Installing on NixOS

Installing on NixOS

In the following example configuration, all options except for the enable option are just for illustrational purpose, as they have the shown values by default.
For lots of additional configuration options you can visit nixos.org or take a look at the man configuration.nix on your system.

To run grafana on your NixOS installation, simply add this snippet to your system configuration:

...

services.grafana = {
  enable   = true;
  port     = 3000;
  domain   = "localhost";
  protocol = "http";
  dataDir  = "/var/lib/grafana";
};

...

Now rebuild your system by using nixos-rebuild:

nixos-rebuild switch

And you’re done!

Module details

  • All configuration is handled by the NixOS module.
  • Non-exposed options can be passed to the service by using the module’s extraOptions option.
  • Starts a new systemd service grafana-server.service
  • Logs will be placed in /var/lib/grafana/log/ by default
  • The default configuration specifies an sqlite3 db at /var/lib/grafana/data/grafana.db
  • grafana-cli and grafana-server are added to your environment.systemPackages by the module

Controlling the server (via systemd)

When the grafana module is enabled, the systemd service will start automatically.

To stop/start/restart the service using systemd’s systemctl:

systemctl stop grafana.service
systemctl start grafana.service
systemctl restart grafana-server

The service’s logs in the systemd-journal can be accessed using journalctl:

journalctl -efu grafana.service

Database

The default configuration specifies a sqlite3 database located at
/var/lib/grafana/data/grafana.db. Please backup this database before
upgrades. You can also use MySQL or Postgres as the Grafana database, as detailed on the configuration page.

Configuration

All configuration is handled by the NixOS module.
For a list of available configuration options, take a look at the available options on nixos.org.
Additional options can be passed by using the module’s extraOptions option.

Adding data sources

A few tips from my install of Grafana on NixOS:

Context

I have a dedicated NixOS PC on a local network for serving InfluxDB & Grafana.

Network Interfaces

Accessing from Grafana from other LAN PCs

Set addr = ""; to set Grafana to bind to all network ports. If you leave the default, you cannot access the web interface (from other network PCs) with http://ipAddress:3000, only http://127.0.0.1:3000.

Start Grafana service after IP Addresses Assigned

I had issues with Grafana starting before all network interfaces started and obtained IP addresses, so I added the following lines to try to force the Grafana service to bind to the IP addresses that the network interfaces obtain.

systemd.services.grafana = {
  after = [ "network-interfaces.target" ];
  wants = [ "network-interfaces.target" ];
};  

Firewall Ports

I recommend opening network firewall ports to let Grafana serve data on the network.

networking.firewall.allowedTCPPorts = [ grafanaPort ];	# ports for influx API, grafana, resp.

You can provision data sources into Grafana from your NixOS config. An example complete config:

let
  grafanaPort = 3000;
  influxApiPort = 8086;
  ipAddress = "127.0.0.1";
  mkGrafanaInfluxSource = db: {
    name = "My Influx-${db} DB";
    type = "influxdb";
    database = db;
    editable = false; # Force editing in this file.
    access = "proxy";
    # user = "grafana"; # fill in Grafana InfluxDB user, if enabled
    # password = "grafana";
    url = ("http://${ipAddress}:${toString influxApiPort}");
  };
in
  services.grafana = {
	addr = ""; # listen (bind) to all network interfaces (i.e. 127.0.0.1, and ipAddress)
	enable = true;
	port = grafanaPort;
	domain = "localhost";
	protocol = "http";
	dataDir = "/var/lib/grafana";
  };
  systemd.services.grafana = {
	# wait until all network interfaces initialize before starting Grafana
	after = [ "network-interfaces.target" ];
	wants = [ "network-interfaces.target" ];
  };  
  services.grafana.provision = {
	enable = true;
	datasources = map mkGrafanaInfluxSource
	  ["my-influx-database-name" "database-name-2"];
  };

Hope this helps!