Как добавить кастомные сервисы в Nixos

с помощью nixops можно легко настроить такие службы, как:

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

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

но я хочу продлить services. например, используя override, как это сделано для pkgs ниже:

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

вопрос

как это сделать для services?


person qknight    schedule 28.07.2016    source источник
comment
Я не понимаю вопроса. Вы можете привести конкретный пример?   -  person iElectric    schedule 28.07.2016


Ответы (2)


Для добавления службы необходимо сначала написать для нее определение службы. То есть nix-файл, в котором объявляются параметры вашей службы и предоставляется реализация.

Допустим, наша служба называется foo, затем мы пишем для нее определение службы и сохраняем ее как файл 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
    };
  };

Например, для Гидры этот файл можно найти здесь: https://github.com/NixOS/hydra/blob/dd32033657fc7d6a755c2feae1714148ee43fc7e/hydra-module.nix.

После написания определения службы мы можем использовать его в нашей основной конфигурации следующим образом:

{
  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!";
    };
  };

}

Отказ от ответственности: в этом могут быть некоторые опечатки, я не тестировал.

person bennofs    schedule 28.07.2016
comment
именно то, что нам нужно! - person qknight; 28.07.2016

согласно aszlig, мы можем сделать это:

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...";
    };
  };
}

оцените это

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



по сравнению с отключенными модулями:

 $ 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)
person qknight    schedule 25.09.2017