anchor.diff:  with lib;
   
   let
  -
  +  dataDir = "/var/lib/searx";
     cfg = config.services.searx;
   
  -  configFile = cfg.configFile;
  +  hasEngines =
  +    builtins.hasAttr "engines" cfg.settings &&
  +    cfg.settings.engines != { };
  +
  +  # Script to merge NixOS settings with
  +  # the default settings.yml bundled in searx.
  +  mergeConfig = ''
  +    cd ${dataDir}
  +    # find the default settings.yml
  +    default=$(find '${cfg.package}/' -name settings.yml)
  +
  +    # write NixOS settings as JSON
  +    cat <<'EOF' > settings.json
  +      ${builtins.toJSON cfg.settings}
  +    EOF
  +
  +    ${optionalString hasEngines ''
  +      # extract and convert the default engines array to an object
  +      ${pkgs.yq-go}/bin/yq r "$default" engines -j | \
  +      ${pkgs.jq}/bin/jq 'reduce .[] as $e ({}; .[$e.name] = $e)' \
  +        > engines.json
  +
  +      # merge and update the NixOS engines with the newly created object
  +      cp settings.json temp.json
  +      ${pkgs.jq}/bin/jq -s '. as [$s, $e] | $s | .engines |=
  +        ($e * . | to_entries | map (.value))' \
  +        temp.json engines.json > settings.json
  +
  +      # clean up temporary files
  +      rm {engines,temp}.json
  +    ''}
  +
  +    # merge the default and NixOS settings
  +    ${pkgs.yq-go}/bin/yq m -P settings.json "$default" > settings.yml
  +    rm settings.json
  +
  +    # substitute environment variables
  +    env -0 | while IFS='=' read -r -d ''' n v; do
  +      sed "s#@$n@#$v#g" -i settings.yml
  +    done
  +  '';
   
   in
   
   {
  +
  +  imports = [
  +    (mkRenamedOptionModule
  +      [ "services" "searx" "configFile" ]
  +      [ "services" "searx" "settingsFile" ])
  +  ];
   
     ###### interface
   
  
positive.code: 
      services.searx = {
  
        enable = mkEnableOption
          "the searx server. See https://github.com/asciimoo/searx";
  
        configFile = mkOption {
          type = types.nullOr types.path;
          default = null;
          description = "
            The path of the Searx server configuration file. If no file
            is specified, a default file is used (default config file has
            debug mode enabled).
          ";
        };
  
        package = mkOption {
  
negative.code:   ###### implementation
  
    config = mkIf config.services.searx.enable {
      systemd.services.searx = {
        description = "Searx server, the meta search engine.";
        after = [ "network.target" ];
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          User = "searx";
          DynamicUser = true;
          ExecStart = "${cfg.package}/bin/searx-run";
          StateDirectory = "searx";
        } // optionalAttrs (cfg.environmentFile != null)
          { EnvironmentFile = builtins.toPath cfg.environmentFile; };
        environment.SEARX_SETTINGS_PATH = cfg.settingsFile;
        preStart = mergeConfig;
      };
  
      environment.systemPackages = [ cfg.package ];
  
  