Search code examples
nixnixos

How to install systemd service on nixos


If I do this:

#!/usr/bin/env bash

set -e;

cd "$(dirname "$BASH_SOURCE")"

ln -sf "$(pwd)/interos-es-mdb.service" '/etc/systemd/system/interos-es-mdb.service'

systemctl enable interos-es-mdb.service
systemctl start interos-es-mdb.service

then I get this error:

ln: failed to create symbolic link '/etc/systemd/system/interos-es-mdb.service': Read-only file system

anyone know the right way to install a service on nixos machine? (I am the root user)...here is the service for reference:

[Unit]
Description=Interos MongoDB+ES log capture
After=network.target

[Service]
Environment=interos_emit_only_json=yes
EnvironmentFile=/root/interos/env/es-service.env
StartLimitIntervalSec=0
Type=simple
Restart=always
RestartSec=1
ExecStart=/root/interos/repos/elastic-search-app/syslog-exec.sh

[Install]
WantedBy=multi-user.target

update: perhaps what I am looking for is "per-user" service, not something run as root etcetera.


Solution

  • An appropriate entry in your /etc/nixos/configuration.nix might look like:

    let
      # assumes you build a derivation for your software and put it in
      # /etc/nixos/pkgs/interosEsMdb/default.nix
      interosEsMdb = import ./pkgs/interosEsMdb {};
    in config.systemd.services.interosEsMdb = {
      description = "Interos MongoDB+ES log capture";
      after = ["network.target"];
      wantedBy = ["multi-user.target"];
    
      serviceConfig = {
        # change this to refer to your actual derivation
        ExecStart = "${interosEsMdb}/bin/syslog-exec.sh";
        EnvironmentFile = "${interosEsMdb}/lib/es-service.env";
        Restart = "always";
        RestartSec = 1;
      }
    }
    

    ...assuming you actually build a derivation for interosEsMdb (which is the only sane and proper way to package software on NixOS).