在nixpkgs中覆盖nixops部署的服务

时间:2016-07-28 10:50:56

标签: nixos nixops

使用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如何做到这一点?

2 个答案:

答案 0 :(得分:1)

添加服务要求您首先为服务编写服务定义。也就是说,这是一个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
    };
  };

例如,对于Hydra,可以在此处找到此文件: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!";
    };
  };

}

免责声明:此处可能存在一些拼写错误,我还没有测试过。

答案 1 :(得分:0)

根据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..."



与没有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)