Search code examples
nixos

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?


Solution

  • 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)