Skip to content

nixos/crowdsec: refactor sprint - #535319

Merged
leona-ya merged 3 commits into
NixOS:masterfrom
TornaxO7:saltsprint
Oct 19, 2026
Merged

leona-ya merged 3 commits into
NixOS:masterfrom
TornaxO7:saltsprint

Conversation

@TornaxO7

@TornaxO7 TornaxO7 commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Continues #446307

This PR is created in order to have some smaller commits to understand the changes easier... I recommend to read commit-by-commit to understand the changes although I have to admit that some changes could be splitted to even smaller commits.

Compared to the previous PR, this fully uses systemd.tmpfiles for the file management which fixes the case that when the user sets some different paths that the module doesn't break...

A follow-up PR could be done to do the same for the crowdsec-firewall-bouncer.

Also note: If everything's fine, I'd need to add an entry for the 26.11 changelog. Due to merge-conflicts I'd like to do that at the end.

Things done

@nixpkgs-ci
nixpkgs-ci Bot requested review from 06kellyjac, M0ustach3 and nicomem June 25, 2026 16:14
@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Jun 25, 2026

The main usecase for this is to allow reading group readable log files.
'';
defaultText = lib.literalExpression ''[ "systemd-journal" ]'';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This likely isn't what you want - providing a defaultText without a real default. ;)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'm wondering: I don't think extraGroups is a good idea - we don't want the service to run with the privileges of those services.

I'd rather recommend providing an inverse helper that uses setfacl to add the crowdsec user (and/or group) to the relevant log files.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'm wondering: I don't think extraGroups is a good idea - we don't want the service to run with the privileges of those services.

I'd rather recommend providing an inverse helper that uses setfacl to add the crowdsec user (and/or group) to the relevant log files.

That sounds like a good idea. Hm... I'm wondering if I should do that in this PR or in a follow up PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This likely isn't what you want - providing a defaultText without a real default. ;)

Regarding this: The default value is automatically added at the bottom of the file.

Settings for the main CrowdSec configuration file.

See <https://docs.crowdsec.net/docs/data_sources/intro> for details.
Refer to the defaults at <https://github.com/crowdsecurity/crowdsec/blob/master/config/config.yaml>.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do they have stable versioned references we could generate based on the specific package version we use?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I posted a question for this in the crowdsec discord server and I'm waiting for a response of them.

default = [ ];
description = ''
A list of stage s00-raw specifications. Most of the time, those are already included in the hub, but are presented here anyway.

See <https://docs.crowdsec.net/docs/parsers/intro> for details.
See <https://docs.crowdsec.net/docs/next/log_processor/parsers/intro/#s00-raw> for details.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't a consistent URL pattern sadly for the version and according to the discord server they only keep about the latest 3 versions of the docs around.

description = ''
The Console Token file to use.

Normally you would do `cscli enroll <token>`, but you can put the token in a file instead and pass the path of that file to this option.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: double space

@TornaxO7 TornaxO7 Jun 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing a double space 👀 (also I this description has been reformatted in a later commit, so I think we can close this? 👀 )

}) localNotificationsMap
);
};
systemd.tmpfiles.settings."10-crowdsec" =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the helpers look interesting and might be placed in a utility or at least earlier in this file to support the readability

@skowalak skowalak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the crowdsec.nix module only for now. Nitpick, I don't know if it's required but you could squash/rebase your refactor commits into one, that makes it a little cleaner here.

@TornaxO7

Copy link
Copy Markdown
Contributor Author

Nitpick, I don't know if it's required but you could squash/rebase your refactor commits into one, that makes it a little cleaner here.

@skowalak yes, but I'd like to keep those commits for the time being in order to simplify the reviewing process:
Instead of reviewing one big commit you can read each commit step by step and see what kind of changes have been made. However, I can squash them at the end if it's wanted after it has been fully approved.

@TornaxO7 TornaxO7 mentioned this pull request Jun 26, 2026
2 of 13 tasks
@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/7137

@ofborg ofborg Bot added ofborg-internal-error Ofborg encountered an error and removed ofborg-internal-error Ofborg encountered an error labels Jun 27, 2026
@eccgecko

Copy link
Copy Markdown

Is there a regression on state permanence since switching to this new #535319 from #446307 ?

This refactor now seems to route every managed directory through the systemd.tmpfiles rule ([uppercase] D type), including the runtime data directory data_dir (/var/lib/crowdsec/data).

Seems that D directories have their contents removed whenever systemd-tmpfiles is run with the --remove option, which NixOS does on every boot and every new activation/switch.

So because the LAPI sqlite database (${data_dir}/crowdsec.db) is in that directory (and is not itself a declarative tmpfile entry), it seems that on this PR it ends up getting deleted.

If I understand correctly, this means bouncer registration, registered machines, CAPI engine identity, and local decisions all get destroyed on every reboot/nixos switch, whioch appears to be a regression from the previous PR #446307, which persisted
state via StateDirectory= and therefore survived reboots.

nixos/tests/crowdsec/simple.nix and full.nix only wait_for_unit("multi-user.target") and
assert cscli bouncers list is non-empty on the first boot. Neither tests reboots and re-checks, so
the data loss is outside current test coverage.

I think switching to using tmpfiles d [lowercase] is the fix for this, but I am not certain so this needs checking. For e.g., with d, changing the data_dir later leaves the old path behind (no automatic cleanup), but to me this seems preferable to deleting a live database.

@TornaxO7

TornaxO7 commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor Author

which NixOS does on every boot and every new activation/switch.

hold up. I thought that this would only happen if the user calls this manually

In that case: Yeah, I think d would be better then.

EDIT: Done 👍

@eccgecko

eccgecko commented Jun 29, 2026 •

Copy link
Copy Markdown

which NixOS does on every boot and every new activation/switch.

hold up. I thought that this would only happen if the user calls this manually

In that case: Yeah, I think d would be better then.

Yeah I am not certain either so think this needs confirming.

But going off the systemd-tmpfiles man page:

--remove
If this command is passed, the contents of directories marked with D or R, and files or directories themselves marked with r or R are removed unless an exclusive or shared BSD lock is taken on them (see flock(2)).

And ❯ systemctl cat systemd-tmpfiles-setup.service which runs on every boot:

...
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=systemd-tmpfiles --create --remove --boot --exclude-prefix=/dev
...

@eccgecko

Copy link
Copy Markdown

Potentially you could also use lowercase d on just data_dir and hub_dir while keeping value.D for declaratively-regenerated config directories under config_dir (whose every entry is a tmpfiles f/L+/C+ rule, so removing + recreating them is safe and intentional)?

@nicomem nicomem left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With these changes, the services run on my machine

@TornaxO7

Copy link
Copy Markdown
Contributor Author

Potentially you could also use lowercase d on just data_dir and hub_dir while keeping value.D for declaratively-regenerated config directories under config_dir (whose every entry is a tmpfiles f/L+/C+ rule, so removing + recreating them is safe and intentional)?

hm... I'd say: Let's keep it simple and stick with d. That's fine I think.

@liamthexpl0rer

Copy link
Copy Markdown
Contributor

Hi @TornaxO7 and thanks for your work,
have you changed anything in the options from the former PR or are they still identical so I can use the same NixOS configuration?

@TornaxO7

TornaxO7 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

(...) or are they still identical so I can use the same NixOS configuration?

they are almost identical. If I remember correctly, only the openFirewall option has been removed. So if you already run the "previous" version, it should be plug-and-play.


online_client.credentials_path = lib.mkOption {
type = lib.types.nullOr secret_path;
default = null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

settings.console.enrollKeyFile require this to be not null otherwise crowdsec-setup.service will fail on enrollment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask how it can fail? There's a check that it's not null for the setup script?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t remember details, as it been a while already, but if i had to guess this is because enroll called without registration first.

@poperigby

Copy link
Copy Markdown
Contributor

I hit what I think is a startup race with autoUpdateService = true on a fresh install (so no /var directories).

On the first activation, crowdsec-setup and crowdsec-update-hub started at the same time. Both use DynamicUser=true and share StateDirectory=crowdsec, and they failed with:

Error: while setting up trace directory: mkdir /var/lib/crowdsec/data/trace: permission denied
mkdir: cannot create directory '/var/lib/crowdsec/data': Permission denied

Making crowdsec-update-hub run after/require setup fixed the race locally. There might also be a problem with multiple dynamic user units writing to the same state directory, because making an explicit user and having them all use it fixed the permissions.

If you've tested this on a completely clean install I'm very confused why this would only be happening to me, but it's been a consistent issue.

@TornaxO7

Copy link
Copy Markdown
Contributor Author

I hit what I think is a startup race with autoUpdateService = true on a fresh install (so no /var directories).

On the first activation, crowdsec-setup and crowdsec-update-hub started at the same time. Both use DynamicUser=true and share StateDirectory=crowdsec, and they failed with:

Error: while setting up trace directory: mkdir /var/lib/crowdsec/data/trace: permission denied
mkdir: cannot create directory '/var/lib/crowdsec/data': Permission denied

Making crowdsec-update-hub run after/require setup fixed the race locally. There might also be a problem with multiple dynamic user units writing to the same state directory, because making an explicit user and having them all use it fixed the permissions.

If you've tested this on a completely clean install I'm very confused why this would only be happening to me, but it's been a consistent issue.

Thank you for the information!
I added crowdsec-setup.service to after in crowdsec-update-hub. Regarding the multiple dynamic users:
It shouldn't be a problem since all of them should be the same user.

@eccgecko

Copy link
Copy Markdown

What are the chances that this refactor will make it into 26.11? Seems like progress has kind of stalled a little?

@TornaxO7

Copy link
Copy Markdown
Contributor Author

What are the chances that this refactor will make it into 26.11?

I... don't know. My plan is, that I'll contact the people (which is usually announced some months before afaik) who will organize the 26.11 release and ask them, what else is needed for this PR to get merged.

Seems like progress has kind of stalled a little?

I'm waiting for some more reviews.
The failing checks are just due to the commits which I didn't squash yet, because it's a bit easier to read the changes commit-by-commit instead of a huge one that's why I'd like to squash them at the end.

So from my side, it's not stalled. I just need some feedback xD

@leona-ya

Copy link
Copy Markdown
Member

I reordered the commits now. You @TornaxO7 can have a look and tell me if you don't like something. Generally speaking: A fixup to a previous commit should be squashed. Also, when the commit is for example about introducing documentation, it should contain no module changes.


From my side this PR works. I also don't understand the above comment. Please @LarsArtmann write something like this without AI. This just reads like word bingo and is not understandable. IMHO this is also about respect of other peoples time.

@TornaxO7

Copy link
Copy Markdown
Contributor Author

I reordered the commits now. You @TornaxO7 can have a look and tell me if you don't like something.

Looks fine to me 👍

Generally speaking: A fixup to a previous commit should be squashed. Also, when the commit is for example about introducing documentation, it should contain no module changes.

Noted I'll try to not let this happen again 👍

@gaelj

gaelj commented Oct 18, 2026

Copy link
Copy Markdown
Member

@leona-ya keeping the commit history during development proved really useful to allow reviewers to keep track of what's going on (versus constant force-push of a single commit), at least on big changes like this one. Of course I fully agree that a final squash and rebase on master is mandatory, once there is a consensus that the PR is ready to merge.

Anyhow, thanks for helping this get merged. And thanks to TornaxO7 for the perseverance !

@leona-ya leona-ya left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is now good, we have breaking feature freeze for NixOS 26.11 in less than a week and I think we're quite sure this is an improvement over the current merged module.

Eventually remaining bugs or more features can be done afterwards.

Thanks to @TornaxO7 for the patience with this huge PR. :)

@leona-ya
leona-ya added this pull request to the merge queue Oct 19, 2026
Merged via the queue into NixOS:master with commit 8e29a34 Oct 19, 2026
28 checks passed
@leona-ya

Copy link
Copy Markdown
Member

@TornaxO7 could you write another small PR with a release note in nixos/doc for this? Thanks

gaelj pushed a commit to gaelj/nixpkgs that referenced this pull request Oct 19, 2026
@TornaxO7

Copy link
Copy Markdown
Contributor Author

thank you for reviewing and merging it!

Eventually remaining bugs or more features can be done afterwards.

@TornaxO7 could you write another small PR with a release note in nixos/doc for this? Thanks

Aye!

@TornaxO7 TornaxO7 mentioned this pull request Oct 19, 2026
1 of 13 tasks
@krokosik

Copy link
Copy Markdown

Great job @TornaxO7 and @leona-ya 🥳
Looking forward to 26.11

gaelj pushed a commit to gaelj/nixpkgs that referenced this pull request Oct 19, 2026
gaelj pushed a commit to gaelj/nixpkgs that referenced this pull request Oct 19, 2026
@eccgecko

Copy link
Copy Markdown

Congratulations and good job, @TornaxO7

fosskar added a commit to fosskar/nixfiles that referenced this pull request Oct 22, 2026
NixOS/nixpkgs#535319 rewrote services.crowdsec: openFirewall is gone,
settings.general is now settings.config, localConfig.* moved to
settings.*, and the lapi/capi credentialsFile options moved under
settings.config.api. the module now links /etc/crowdsec/config.yaml
itself, so drop the environment.etc workaround and the `cscli -c`
flags in the bouncer register scripts.

pin data_dir to /var/lib/crowdsec/state and keep use_wal, the old
module defaults, so the existing db, hub and credentials stay in
place. upstream now sets Restart=always, which conflicts with our
Restart=on-failure, so drop ours.

Assisted-by: LLM
@eljojo

eljojo commented Oct 24, 2026

Copy link
Copy Markdown

thanks for this work @TornaxO7 and @leona-ya , been watching this for a while!!

fosskar added a commit to fosskar/nixfiles that referenced this pull request Oct 25, 2026
NixOS/nixpkgs#535319 rewrote services.crowdsec: openFirewall is gone,
settings.general is now settings.config, localConfig.* moved to
settings.*, and the lapi/capi credentialsFile options moved under
settings.config.api. the module now links /etc/crowdsec/config.yaml
itself, so drop the environment.etc workaround and the `cscli -c`
flags in the bouncer register scripts.

pin data_dir to /var/lib/crowdsec/state and keep use_wal, the old
module defaults, so the existing db, hub and credentials stay in
place. upstream now sets Restart=always, which conflicts with our
Restart=on-failure, so drop ours.

Assisted-by: LLM
gaelj pushed a commit to gaelj/nixpkgs that referenced this pull request Oct 25, 2026
fosskar added a commit to fosskar/nixfiles that referenced this pull request Oct 26, 2026
NixOS/nixpkgs#535319 rewrote services.crowdsec: openFirewall is gone,
settings.general is now settings.config, localConfig.* moved to
settings.*, and the lapi/capi credentialsFile options moved under
settings.config.api. the module now links /etc/crowdsec/config.yaml
itself, so drop the environment.etc workaround and the `cscli -c`
flags in the bouncer register scripts.

pin data_dir to /var/lib/crowdsec/state and keep use_wal, the old
module defaults, so the existing db, hub and credentials stay in
place. upstream now sets Restart=always, which conflicts with our
Restart=on-failure, so drop ours.

Assisted-by: LLM
fosskar added a commit to fosskar/nixfiles that referenced this pull request Oct 27, 2026
NixOS/nixpkgs#535319 rewrote services.crowdsec: openFirewall is gone,
settings.general is now settings.config, localConfig.* moved to
settings.*, and the lapi/capi credentialsFile options moved under
settings.config.api. the module now links /etc/crowdsec/config.yaml
itself, so drop the environment.etc workaround and the `cscli -c`
flags in the bouncer register scripts.

pin data_dir to /var/lib/crowdsec/state and keep use_wal, the old
module defaults, so the existing db, hub and credentials stay in
place. upstream now sets Restart=always, which conflicts with our
Restart=on-failure, so drop ours.

Assisted-by: LLM
fosskar Bot added a commit to fosskar/nixfiles that referenced this pull request Oct 27, 2026
* flake: update nixpkgs

Diff: NixOS/nixpkgs@fe13242...3ec6363

* crowdsec: migrate to rewritten nixpkgs module

NixOS/nixpkgs#535319 rewrote services.crowdsec: openFirewall is gone,
settings.general is now settings.config, localConfig.* moved to
settings.*, and the lapi/capi credentialsFile options moved under
settings.config.api. the module now links /etc/crowdsec/config.yaml
itself, so drop the environment.etc workaround and the `cscli -c`
flags in the bouncer register scripts.

pin data_dir to /var/lib/crowdsec/state and keep use_wal, the old
module defaults, so the existing db, hub and credentials stay in
place. upstream now sets Restart=always, which conflicts with our
Restart=on-failure, so drop ours.

Assisted-by: LLM

* overlays: skip broken urllib3-future and niquests tests

urllib3-future 2.25.902 socks tests time out against python-socks v3,
run ~1.6 h and fail on hydra. caldav pulls it in for calendar-mcp and
for home-assistant on ha-green, so the override moves from
calendar-mcp into a global overlay. niquests
test_use_proxy_from_environment also fails once urllib3-future builds.
drop once https://redirect.github.com/NixOS/nixpkgs/pull/575392 reaches
nixos-unstable.

Assisted-by: LLM

* calendar-mcp, laya-mcp: port to mcp 2 MCPServer

nixpkgs ships mcp 2.2.0, which removed mcp.server.fastmcp: FastMCP is
now mcp.server.mcpserver.MCPServer and call_tool returns a
CallToolResult instead of a tuple.

Assisted-by: LLM

* flake: update hermes-agent

the npm hook of hermes-agent 7ac2c6dc segfaults on the new nixpkgs;
the current revision builds against it.

Diff: NousResearch/hermes-agent@7ac2c6d...085f562

Assisted-by: LLM

* caddy: update desec plugin hash for new nixpkgs

the vendored go modules of caddy.withPlugins changed with the
nixpkgs bump, so the fixed-output hash no longer matches.

Assisted-by: LLM

---------

Co-authored-by: fosskar[bot] <300917551+fosskar[bot]@users.noreply.github.com>
Co-authored-by: fosskar <[email protected]>
gaelj pushed a commit to gaelj/nixpkgs that referenced this pull request Oct 27, 2026
gaelj pushed a commit to gaelj/nixpkgs that referenced this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.