Repository navigation
Description
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
- I assert that this is a bug and not a support request.
- I assert that this is not a duplicate of an existing issue.
- I assert that I have read the NixOS Code of Conduct and agree to abide by it.
Is this issue important to you?
Add a 👍 reaction to issues you find important.
Activity
SebastianStork commentedon Sep 24, 2025
Especially
api.server.enable = lib.mkDefault falseseems 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/M0ustach3 commentedon Sep 25, 2025
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,
SebastianStork commentedon Sep 25, 2025
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?
M0ustach3 commentedon Sep 25, 2025
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,
TornaxO7 commentedon Sep 26, 2025
@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.yamlas the defaultcredentialsFilePath?nicomem commentedon Sep 26, 2025
@TornaxO7
/etc/crowdseccannot be used as it is read-only on NixOS and we need a location where thecrowdsecservice 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/crowdsecseems appropriate.TornaxO7 commentedon Sep 26, 2025
I just tried to execute
crowdsecin 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 bycrowdsecitself (or I'm remembering it wrong). I can try that out again later. But first, it's lunchtime :DTornaxO7 commentedon Sep 26, 2025
Indeed, I'm getting
after just executing
crowdsec.nicomem commentedon Sep 26, 2025
I think you're confusing the config file with the lapi credentials file:
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)config.yamlfile, ifapi.client.credentials_pathis set (to a path somewhere), it seems thatcrowdseceither reads the file's contents (if it exists) or creates it (with a generated password)/etc/crowdsec/local_api_credentials.yamlon NixOS), thecrowdsecservice fails because it cannot write the file/etcbecause in most distros this path is not read-onlyTo resume the flow of what I suggested:
services.crowdsec.settings.lapi.credentialsFileto somewhere the service has write permissions (and will not be deleted when the service is stopped)api.client.credentials_pathin the generated config filecrowdsecservice starts and reads the generated config file (specified by the-cargument)crowdsecis running ✅...crowdsecis running ✅TornaxO7 commentedon Sep 26, 2025
on my system, if I test it,
/etc/crowdsec/local_api_credentials.yaml) isn't read only:as you can see from the screenshot, it could successfully create the default file.
SebastianStork commentedon Sep 26, 2025
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
/etcand 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.nicomem commentedon Sep 27, 2025
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/libinstead of adding specific rules for each service or adding rhe entirity of/etcwhich is composed mostly of generated files, which have no utility of being persisted.nixos-discourse commentedon Dec 19, 2025
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/environment-etc-vs-systemd-tmpfiles/73450/1
nixos-discourse commentedon Dec 20, 2025
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/environment-etc-vs-systemd-tmpfiles/73450/4
nixos-discourse commentedon Dec 21, 2025
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/environment-etc-vs-systemd-tmpfiles/73450/11
jarppiko commentedon Dec 26, 2025
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/crowdsecwas created with too loose permissionsroot:root 0755. I could not figure out where the dir is actually created in thecrowdsec.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 registerand stored the url, login and password tosops-nix.3. Unable to enroll to console
cscli console enrollfailed since the config cannot be written to a read-onlyconsole.yaml. I setservices.crowdsec.settings.general.api.server.console_path = "/var/lib/crowdsec/state/console.yaml";in my config.4.
crowdsec.servicehasDynamicUser=trueShould the
DynamicUser=truebe 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
crowdsecshould download it, but it has not.At this point I decided to take a break...EDIT. I added an example acquisition and it started working 🚀
TornaxO7 commentedon Dec 26, 2025
Howdy @jarppiko !
Am I understanding your sentence correctly here that you suggest to set it to
0750? I could try to do that.Hm... so are you suggesting it would be better if the module tries to register automatically to the central api?
Oh damn, that's a fair pain point. I'll change the default path.
This has already removed in #446307 (would be nice if you could try this out :))
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!
TornaxO7 commentedon Dec 26, 2025
Alright:
crowdsecand0750for the StateDirectory nowjarppiko commentedon Dec 27, 2025
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/crowdsecpermissionsUnless the
crowdsecprocess has to write to the/var/lib/crowdsec, I would keep the permissions as0750androot:crowdsec. Ifcrowdsecuser has to write the directory, then thecrowdsec:crowdsecis correct.I think you are correct. It should not register automatically, but maybe a warning/info message to the stdout/stderr during
nixos-rebuildwould be good to guide the user?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?
Should there be an option to restart
crowdsec-firewall-bouncer.servicewhenever firewall service is being restarted? This requiresPartOf=nftables.serviceunder[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 (
catnot found in PATH). I am still learning Nixos. How can I test the PR?Btw. I got Crowdsec working 🚀
TornaxO7 commentedon Dec 27, 2025
I changed it to
crowdsec:crowdsec 0750👍Fair enough, that's doable. I'll do that.
There should be already a warning
TornaxO7 commentedon Dec 27, 2025
@jarppiko
crowdsec.nixsomewhere to your dotfiles.