首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Nixos中添加自定义服务

如何在Nixos中添加自定义服务
EN

Stack Overflow用户
提问于 2016-07-28 10:50:55
回答 2查看 3.2K关注 0票数 0

使用nixops可以轻松地配置以下服务:

代码语言:javascript
复制
{
  network.description = "Web server";
  webserver = { config, pkgs, ... }:

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

但我想扩展services。例如,通过使用override,如下面对pkgs所做的那样:

代码语言:javascript
复制
  let
    myfoo = callPackage ...
  in
  pkgs = pkgs.override {
    overrides = self: super: {
      myfoo-core = myfoo;
    };
  }

问题

如何为services做到这一点

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-25 14:40:39

根据aszlig的说法,我们可以这样做:

configuration.nix

代码语言:javascript
复制
{ 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...";
    };
  };
}

评估它

代码语言:javascript
复制
 $ nix-instantiate --eval '<nixpkgs/nixos>' --arg configuration ./test-disable.nix -A config.systemd.services.nagios.description
"my own shiny nagios service..."



相对于没有disabledModules的情况:

代码语言:javascript
复制
 $ 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)
票数 0
EN

Stack Overflow用户

发布于 2016-07-28 11:40:04

添加服务需要首先为服务编写服务定义。也就是说,一个nix文件,它声明服务的选项并提供一个实现。

假设我们的服务名为foo,然后为它编写一个服务定义--将其保存为文件foo.nix

代码语言:javascript
复制
{ 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

在编写了服务定义之后,我们可以使用它--我们的主要配置如下:

代码语言:javascript
复制
{
  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!";
    };
  };

}

免责声明:可能有一些排版在这,我还没有测试它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38634229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档