Pour ajouter un service, vous devez d'abord écrire une définition de service pour votre service. C'est-à-dire, un fichier nix qui déclare les options de votre service et fournit une implémentation.
Disons que notre service est appelé foo
, nous écrivons une définition de service pour lui une sauvegarde comme fichier 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
};
};
Par exemple, pour Hydra, ce fichier se trouve ici: https://github.com/NixOS/hydra/blob/dd32033657fc7d6a755c2feae1714148ee43fc7e/hydra-module.nix.
Après avoir écrit la définition du service, nous pouvons l'utiliser notre configuration principale comme ceci:
{
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!";
};
};
}
Avertissement: il pourrait y avoir des fautes de frappe dans ce domaine, je ne l'ai pas testé.
Je ne comprendre la question. Pouvez-vous donner un exemple concret? – iElectric