Skip to content

nixos/crowdsec: Use sensible defaults so that the module does not fail when simply enabling it #445342

Open

Description

@nicomem

Nixpkgs version

  • Unstable (25.11)

Describe the bug

I wanted to setup crowdsec on my machine.

By reading the module options and file, I had the following config to start:

services.crowdsec = {
    enable = true;

    localConfig = {
      acquisitions = [
        # Some acquisitions
      ];
    };

    hub = {
      collections = [
        # Some collections
        ...
      ];
    };
  };

However, with that configuration, the systemd service fails with the following fatal error: loading api client: no API client section in configuration.

I managed to get a working service (at least it does not fail and parses my logs) by adding the following to my configuration:

  • services.crowdsec.settings.general.api.server.enable = true
    • To enable the local API since it seems to fail because it is disabled by default in the module
  • services.crowdsec.settings.general.lapi.credentialsFile = "/var/lib/crowdsec/lapi-credentials.yaml"
    • Needed so that crowdsec generates the credentials file
  • systemd.tmpfiles.rules = [ "d /var/lib/crowdsec 0755 ${cfg.user} ${cfg.group}" ];
    • Needed so that crowdsec has the rights to create the credentials file

I am only a begginner at crowdsec so maybe these should not be the defaults for everyone, but as a begginner, I spent quite some time trying to find out why it failed and what to tweak to try to fix it.
-> The modules defaults should generate a working crowdsec service so that begginner crowdsec users can begin using it, and after gaining more knowlege, configure it as they please

Steps to reproduce

  • Enable the module, define some acquisitions and collections
  • The crowdsec service fails

Expected behaviour

The crowdsec module can be simply enabled and generates a service that does not fail at startup

Screenshots

No response

Relevant log output

Additional context

No response

System metadata

  • system: "x86_64-linux"
  • host os: Linux 6.12.44, NixOS, 25.11 (Xantusia), 25.11.20250918.0147c2f
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.28.5
  • nixpkgs: /nix/store/wn3wblni1cm7plb1dvakqcmr31szqdgn-source

Notify maintainers

@M0ustach3
@TornaxO7
@06kellyjac


Note for maintainers: Please tag this issue in your pull request description. (i.e. Resolves #ISSUE.)

I assert that this issue is relevant for Nixpkgs

Is this issue important to you?

Add a 👍 reaction to issues you find important.

Activity

  1. added
    0.kind: bug
    6.topic: nixos
    on Sep 22, 2025
  2. SebastianStork commented on Sep 24, 2025

    @SebastianStork
    Contributor

    Especially api.server.enable = lib.mkDefault false seems like a mistake as it's enabled by default upstream and the local API seems to be required for bouncers to work: https://docs.crowdsec.net/docs/local_api/intro/

  3. M0ustach3 commented on Sep 25, 2025

    @M0ustach3
    Contributor

    Hi @SebastianStork , jumping in the conversation :)

    From what I recall, when I created the module, I put this default value because I deployed a distributed architecture. As the module was initially made only for me and my company, I did not thought about changing the default value for this option. I think this is why we ended up with these default values !

    This module was my first module ever anyway and a lot has changed since then, but I wanted to clarify on why some values might seem strange at first glance !

    Cheers,

  4. SebastianStork commented on Sep 25, 2025

    @SebastianStork
    Contributor

    First of all, thanks for your effort!

    I currently still use https://codeberg.org/kampka/nix-flake-crowdsec but am not very happy with it and have some ideas on how to make it better. I might try to contribute those ideas to this new (and from the looks of it) better module.

    Are pull requests welcome for improved defaults and features?

  5. M0ustach3 commented on Sep 25, 2025

    @M0ustach3
    Contributor

    First of all, thanks for your effort!

    I currently still use https://codeberg.org/kampka/nix-flake-crowdsec but am not very happy with it and have some ideas on how to make it better. I might try to contribute those ideas to this new (and from the looks of it) better module.

    Are pull requests welcome for improved defaults and features?

    I think there was a misunderstanding ! I was the original maintainer for the nixpkgs/crowdsec module, not the Kampka one ! I’m not sure he has time to accept PRs on his repo, but IMO you can of course suggest changes here ! I don’t have much time to maintain this, that’s why I’m mainly here as a spectator for now, but the other mentioned maintainers sure are active !

    Cheers,

  6. TornaxO7 commented on Sep 26, 2025

    @TornaxO7
    Contributor

    services.crowdsec.settings.general.lapi.credentialsFile = "/var/lib/crowdsec/lapi-credentials.yaml"

    @nicomem may I ask why you choose this path? According to the official default linux config it's added to /etc/crowdsec. Would you agree if I use /etc/crowdsec/local_api_credentials.yaml as the default credentialsFilePath?

  7. nicomem commented on Sep 26, 2025

    @nicomem
    ContributorAuthor

    @TornaxO7 /etc/crowdsec cannot be used as it is read-only on NixOS and we need a location where the crowdsec service will be able to write the file. It should also be a location that is not cleaned when the service stops, so somewhere inside /var/lib/crowdsec seems appropriate.

  8. TornaxO7 commented on Sep 26, 2025

    @TornaxO7
    Contributor

    (...) and we need a location where the crowdsec service will be able to write the file.

    I just tried to execute crowdsec in my terminal but it was complaining that it didn't exist. So it looks like as if the default config file doesn't even get created by crowdsec itself (or I'm remembering it wrong). I can try that out again later. But first, it's lunchtime :D

  9. TornaxO7 commented on Sep 26, 2025

    @TornaxO7
    Contributor

    Indeed, I'm getting

    FATA[2025-09-26T16:06:39+02:00] while loading configuration file: while reading yaml file: open /etc/crowdsec/config.yaml: no such file or directory

    after just executing crowdsec.

  10. nicomem commented on Sep 26, 2025

    @nicomem
    ContributorAuthor

    Indeed, I'm getting

    FATA[2025-09-26T16:06:39+02:00] while loading configuration file: while reading yaml file: open /etc/crowdsec/config.yaml: no such file or directory

    after just executing crowdsec.

    I think you're confusing the config file with the lapi credentials file:

    • When executing crowdsec, it needs an existing configuration file, either at the default location (/etc/crowdsec/config.yaml), or by specifying where it is (crowdsec -c ./my-config-file.yaml)
    • In the config.yaml file, if api.client.credentials_path is set (to a path somewhere), it seems that crowdsec either reads the file's contents (if it exists) or creates it (with a generated password)
      • If the path points to a read-only location (like /etc/crowdsec/local_api_credentials.yaml on NixOS), the crowdsec service fails because it cannot write the file
        • The upstream default location can point to inside /etc because in most distros this path is not read-only

    To resume the flow of what I suggested:

    • We set services.crowdsec.settings.lapi.credentialsFile to somewhere the service has write permissions (and will not be deleted when the service is stopped)
      • This will set the api.client.credentials_path in the generated config file
    • The crowdsec service starts and reads the generated config file (specified by the -c argument)
    • It find no file at the credentials path location so it creates one
    • crowdsec is running ✅
    • ...
    • We restart the service
    • It now finds a file at the credentials path, it reads its contents
    • crowdsec is running ✅
  11. TornaxO7 commented on Sep 26, 2025

    @TornaxO7
    Contributor

    (like /etc/crowdsec/local_api_credentials.yaml on NixOS)

    on my system, if I test it, /etc/crowdsec/local_api_credentials.yaml) isn't read only:

    Image

    as you can see from the screenshot, it could successfully create the default file.

  12. SebastianStork commented on Sep 26, 2025

    @SebastianStork
    Contributor

    Coming back to the question of defaults: I think it's fine to follow upstream on this but it could also be a good idea to follow the lead of other modules. I have observed that usually only things declaratively managed by nix get put in /etc and everything else (essentially "state") gets put in /var/lib. This would also make the lives easier for people who want to backup or persist the state.

  13. nicomem commented on Sep 27, 2025

    @nicomem
    ContributorAuthor

    I am also on the side of following what other modules do and keep state in /var/lib. Apart from not "suprising" users by doing things differently from other modules, this simplifies life for those who want to do backups or use the impermanence module (like I am), allowing to simply persist/backup /var/lib instead of adding specific rules for each service or adding rhe entirity of /etc which is composed mostly of generated files, which have no utility of being persisted.

  14. nixos-discourse commented on Dec 19, 2025

    @nixos-discourse
  15. nixos-discourse commented on Dec 20, 2025

    @nixos-discourse
  16. nixos-discourse commented on Dec 21, 2025

    @nixos-discourse
  17. jarppiko commented on Dec 26, 2025

    @jarppiko

    Hello @nicomem

    Thank you for the initiative. I got here after searching "crowdsec loading api client: no API client section in configuration" :-)

    List of issues I stumbled on (I used 25.11 5900a0a)

    1. Bit loose permissions for /var/lib/crowdsec?

    /var/lib/crowdsec was created with too loose permissions root:root 0755 . I could not figure out where the dir is actually created in the crowdsec.nix, but maybe there is a good reason for the permissions.

    2. The basic config does not register the engine with Central API, I ran manually cscli capi register and stored the url, login and password to sops-nix.

    3. Unable to enroll to console

    cscli console enroll failed since the config cannot be written to a read-only console.yaml. I set services.crowdsec.settings.general.api.server.console_path = "/var/lib/crowdsec/state/console.yaml"; in my config.

    4. crowdsec.service has DynamicUser=true

    Should the DynamicUser=true be set if the service is dependent on state written on the disk? I think no, but I can be wrong here.

    5. Default config fails since no data source has been defined

    I have no idea how the GeoLite2 db should end up the to the desired location. Maybe crowdsec should download it, but it has not. At this point I decided to take a break...

    Dec 26 16:37:53 hostname crowdsec-setup[141348]: Nothing to do, the hub index is up to date.
    Dec 26 16:37:54 hostname crowdsec[141377]: time="2025-12-26T16:37:54+02:00" level=error msg="unable to open GeoLite2-City.mmdb : open /var/lib/crowdsec/state/GeoLite2-City.mmdb: no such file or directory"
    Dec 26 16:37:55 hostname crowdsec[141377]: time="2025-12-26T16:37:55+02:00" level=fatal msg="crowdsec init: while loading acquisition config: no datasource enabled"
    Dec 26 16:37:55 hostname systemd[1]: crowdsec.service: Control process exited, code=exited, status=1/FAILURE
    

    EDIT. I added an example acquisition and it started working 🚀

    services.crowdsec. localConfig.acquisitions = [
          {
            journalctl_filter = [ "_SYSTEMD_UNIT=sshd.service" ];
            labels.type = "syslog";
            source = "journalctl";
          }
        ];
  18. TornaxO7 commented on Dec 26, 2025

    @TornaxO7
    Contributor

    Howdy @jarppiko !

    /var/lib/crowdsec was created with too loose permissions root:root 0755

    Am I understanding your sentence correctly here that you suggest to set it to 0750? I could try to do that.

    1. The basic config does not register the engine with Central API, I ran manually cscli capi register and stored the url, login and password to sops-nix.

    Hm... so are you suggesting it would be better if the module tries to register automatically to the central api?

    cscli console enroll failed since the config cannot be written to a read-only console.yaml. I set services.crowdsec.settings.general.api.server.console_path = "/var/lib/crowdsec/state/console.yaml"; in my config.

    Oh damn, that's a fair pain point. I'll change the default path.

    Should the DynamicUser=true be set if the service is dependent on state written on the disk? I think no, but I can be wrong here.

    This has already removed in #446307 (would be nice if you could try this out :))

    I have no idea how the GeoLite2 db should end up the to the desired location. Maybe crowdsec should download it, but it has not. At this point I decided to take a break...

    Tbh. I couldn't really find a solution for this but I probably didn't look enough into it.
    I just found this package https://app.crowdsec.net/hub/author/crowdsecurity/configurations/geoip-enrich maybe this is going to fix the issue? I'll see and try.

    Thank you for the detailed information!

  19. TornaxO7 commented on Dec 26, 2025

    @TornaxO7
    Contributor

    Alright:

    • The default permissions and owner are set to crowdsec and 0750 for the StateDirectory now
    • The default path for the console path has been moved to the StateDirectory.
  20. jarppiko commented on Dec 27, 2025

    @jarppiko

    Hola @TornaxO7 ,

    First of all massive thanks for developing and maintaining the NixOS module for Crowdsec! It's quite a complex service and not very well compatible with NixOS' IaaC / declarative nature not to mention impermeability.

    • /var/lib/crowdsec permissions

    Unless the crowdsec process has to write to the /var/lib/crowdsec, I would keep the permissions as 0750 and root:crowdsec. If crowdsec user has to write the directory, then the crowdsec:crowdsec is correct.

    • registering with the central api

    I think you are correct. It should not register automatically, but maybe a warning/info message to the stdout/stderr during nixos-rebuild would be good to guide the user?

    • GeoLite2 db issue

    This disappeared once I added a single acquisition. So maybe there should be a safe default acquisition so a user does not end up with this kind of odd error?

    • Restarting Firewall bouncer when firewall is restarted?

    Should there be an option to restart crowdsec-firewall-bouncer.service whenever firewall service is being restarted? This requires PartOf=nftables.service under [Unit] section to restart the bouncer whenever firewall is restarted. This should work with other firewalls than Nftables too. A nice to have feature.

    I made few comments to the PR. I forgot to mention an error I came by (cat not found in PATH). I am still learning Nixos. How can I test the PR?

    Btw. I got Crowdsec working 🚀

  21. TornaxO7 commented on Dec 27, 2025

    @TornaxO7
    Contributor

    Unless the crowdsec process has to write to the /var/lib/crowdsec, I would keep the permissions as 0750 and root:crowdsec. If crowdsec user has to write the directory, then the crowdsec:crowdsec is correct.

    I changed it to crowdsec:crowdsec 0750 👍

    It should not register automatically, but maybe a warning/info message to the stdout/stderr during nixos-rebuild would be good to guide the user?

    Fair enough, that's doable. I'll do that.

    This disappeared once I added a single acquisition. So maybe there should be a safe default acquisition so a user does not end up with this kind of odd error?

    There should be already a warning

  22. TornaxO7 commented on Dec 27, 2025

    @TornaxO7
    Contributor

    How can I test the PR?

    @jarppiko

    1. Copy the new crowdsec.nix somewhere to your dotfiles.
    2. Add the following to your NixOS module:
    {...}: {
        disabledModules = [ "services/security/crowdsec.nix" ];
    
      imports = [
        "./crowdsec.nix" # assuming you copy+pasted the new `crowdsec.nix` in the given directory
      ];
    
      config = {
        # it's show time!
        services.crowdsec.enable = true;
      };
    }
    1. Simply rebuild your config :)
  23. added
    2.status: stale
    on Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bug2.status: stale6.topic: nixos

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions