qknight
qknight

Reputation: 924

How to add custom services in Nixos

using nixops one can easily configure services like:

{
  network.description = "Web server";
  webserver = { config, pkgs, ... }:

    {
     services.mysql = {
      enable = true;
      package = pkgs.mysql51;
    };

but i want to extend services. for example by using override as done for pkgs below:

  let
    myfoo = callPackage ...
  in
  pkgs = pkgs.override {
    overrides = self: super: {
      myfoo-core = myfoo;
    };
  }

question

how to do that for services?

Upvotes: 1

Views: 6836

Answers (2)

qknight
qknight

Reputation: 924

according to aszlig, we can do this:

configuration.nix

{ config, lib, ... }:

{
  disabledModules = [ "services/monitoring/nagios.nix" ];

  options.services.nagios.enable = lib.mkOption {
    # Make sure that this option type conflicts with the one in
    # the original NixOS module for illustration purposes.
    type = lib.types.str;
    default = "of course";
    description = "Really enable nagios?";
  };

  config = lib.mkIf (config.services.nagios.enable == "of course") {
    systemd.services.nagios = {
      description = "my own shiny nagios service...";
    };
  };
}

evaluate it

 $ nix-instantiate --eval '<nixpkgs/nixos>' --arg configuration ./test-disable.nix -A config.systemd.services.nagios.description
"my own shiny nagios service..."



versus without disabledModules:

 $ nix-instantiate --eval '<nixpkgs/nixos>' --arg configuration ./test-disable.nix -A config.systemd.services.nagios.description
error: The option `services.nagios.enable' in `/home/aszlig/test-disable.nix' is already declared in `/nix/var/nix/profiles/per-user/root/channels/vuizvui/nixpkgs/nixos/modules/services/monitoring/nagios.nix'.
(use '--show-trace' to show detailed location information)

Upvotes: 2

bennofs
bennofs

Reputation: 11963

Adding a service requires that you first write a service definition for your service. That is, a nix file that declares the options of your service and provides an implementation.

Let's say our service is called foo, then we write a service definition for it an save it as the file foo.nix:

{ config, lib, pkgs, ... }:

with lib;  # use the functions from lib, such as mkIf

let
  # the values of the options set for the service by the user of the service
  foocfg = config.services.foo;
in {
  ##### interface. here we define the options that users of our service can specify
  options = {
    # the options for our service will be located under services.foo
    services.foo = { 
      enable = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to enable foo.
        '';
      };

      barOption = {
        type = types.str;
        default = "qux";
        description = ''
          The bar option for foo.
        '';
      };
    };
  };

  ##### implementation
  config = mkIf foocfg.enable { # only apply the following settings if enabled
    # here all options that can be specified in configuration.nix may be used
    # configure systemd services
    # add system users
    # write config files, just as an example here:
    environment.etc."foo-bar" = {
      text = foocfg.bar; # we can use values of options for this service here
    };
  };

For example, for Hydra, this file can be found here: https://github.com/NixOS/hydra/blob/dd32033657fc7d6a755c2feae1714148ee43fc7e/hydra-module.nix.

After having written the service definition, we can use it our main configuration like this:

{
  network.description = "Web server";
  webserver = { config, pkgs, ... }: {
    imports = [ ./foo.nix ]; # import our service
    services.mysql = {
      enable = true;
      package = pkgs.mysql51;
    };
    services.foo = {
      enable = true;
      bar = "hello nixos modules!";
    };
  };

}

Disclaimer: there might be some typos in this, I have not tested it.

Upvotes: 2

Related Questions