Skip to content

nixos/crowdsec: ExecStartPre hub update fails in hermetic VMs, cascades to permanent EACCES via bouncer-register StateDirectory #520206

Open

Description

@SMC17

Describe the bug

The services.crowdsec NixOS module's ExecStartPre runs a crowdsec-setup script that unconditionally calls cscli hub update on every service start. In any environment without DNS at boot (hermetic nixos-test VMs, air-gapped deployments, first-boot before network-online.target is reached, networks with strict egress), this call fails. Because the setup script uses set -euo pipefail, the failure propagates and aborts service startup.

Once the bouncer-register service has run in this state, the failure becomes permanent via a DynamicUser + StateDirectory migration cascade. After that, the system cannot recover the service without manual filesystem cleanup.

This blocks pkgs.testers.nixosTest-based integration testing of the module and is observable on air-gapped or DNS-flaky-at-boot deployments.

Cascade

  1. crowdsec.service's ExecStartPre runs crowdsec-setup, which calls cscli hub update unconditionally (around lines 553–557 of nixos/modules/services/security/crowdsec.nix).
  2. In a hermetic environment, DNS fails → cscli hub update exits non-zero → set -euo pipefail aborts the script → crowdsec.service start fails.
  3. crowdsec-firewall-bouncer-register.service is configured with DynamicUser=true + StateDirectory=crowdsec. systemd's StateDirectory semantics see the pre-existing /var/lib/crowdsec (created by the upstream package) and migrate it to /var/lib/private/crowdsec under the dynamic user.
  4. Every subsequent invocation of crowdsec-setup now gets EACCES on /var/lib/crowdsec (the path the script still references — it was not aware of the DynamicUser migration).
  5. The service is stuck in a fail loop until manual intervention.

Steps to reproduce

Minimal repro with pkgs.testers.nixosTest:

{ pkgs ? import <nixpkgs> {} }:

pkgs.testers.nixosTest {
  name = "crowdsec-hermetic-repro";
  nodes.machine = { ... }: {
    services.crowdsec.enable = true;
    services.crowdsec.allowLocalJournalAccess = true;
    # No network in nixos-test VM by default — that's the trigger.
  };
  testScript = ''
    machine.wait_for_unit("multi-user.target")
    # crowdsec.service fails on ExecStartPre. Bouncer-register has run by now,
    # so /var/lib/crowdsec is already migrated to /var/lib/private/crowdsec.
    machine.succeed("systemctl status crowdsec.service || true")
  '';
}

The test driver hits its 900s timeout with crowdsec.service in activating or failed state.

Mitigation tried — does not help

Setting services.crowdsec.autoUpdateService = false was tried and is insufficient. That option only governs a separate daily timer (crowdsec-update-hub.service), not the unconditional hub update in crowdsec-setup's ExecStartPre. The fix has to address the ExecStartPre directly.

Proposed fixes (either or both)

(a) Make crowdsec-setup tolerant of offline boot. Wrap the cscli hub update call so that it logs a warning and continues on failure, rather than aborting. Hub updates that fail at boot can be retried by the daily crowdsec-update-hub.service once the network is available. The first-boot semantics should not be "fail closed" for an optional metadata refresh.

# Suggested shape — log on failure, continue
${cscli}/bin/cscli hub update || echo "crowdsec-setup: cscli hub update failed (will retry later)" >&2

(b) Stop crowdsec-firewall-bouncer-register.service from claiming crowdsec as a StateDirectory. The bouncer-register's DynamicUser + StateDirectory=crowdsec migrates /var/lib/crowdsec away from where the rest of the module expects to find it. Either the bouncer-register should use its own state dir (e.g., crowdsec-firewall-bouncer) or drop DynamicUser on this service so the path stays canonical.

Fix (a) addresses the immediate hermetic-VM and air-gapped-boot scenarios. Fix (b) prevents the failure from being permanent once it has occurred.

Why this matters beyond hermetic tests

  • Air-gapped deployments (industrial, healthcare, military) cannot use services.crowdsec today without manual workarounds.
  • First-boot-before-network deployments with DHCP delays hit this cascade.
  • Networks with strict egress filtering (corporate, cloud VPCs without NAT) hit this cascade.
  • pkgs.testers.nixosTest integration testing of any stack that includes CrowdSec is blocked.

Discovered via

Building a nixos-test for a Cloudflare-shaped self-hosted edge stack (Caddy + Coraza WAF + Knot DNS + nftables + CrowdSec). The other four components passed nixosTest integration validation; CrowdSec was the one that revealed the cascade. Full diagnosis and reproducer in SMC17/sovereign-edge PR #4.

Related (but not duplicate) issues

Notify maintainers

@n0emis @aldoborrero (cf. nixos/modules/services/security/crowdsec.nix maintainers field — please update mention if maintainership has changed)

Nixpkgs version

Reproduced on current nixos-unstable channel as of 2026-05-14 via flake-pinned input.

Activity

  1. SMC17 commented on May 19, 2026

    @SMC17
    Author

    PR up at #521985 — adds services.crowdsec.hub.updateOnStart (default true, preserves existing behaviour) so operators can disable the unconditional cscli hub update call in ExecStartPre for hermetic / air-gapped / pre-network-online deployments. Minimal-diff fix; 2 hunks; no breaking change. The secondary half of the cascade (DynamicUser + StateDirectory migration on crowdsec-firewall-bouncer-register) is a separate concern and out of scope for this PR.

  2. TornaxO7 commented on May 19, 2026

    @TornaxO7
    Contributor

    There has been a rewrite of the crowdsec module: #446307

    Could you please try that one out?

    @n0emis @aldoborrero (cf. nixos/modules/services/security/crowdsec.nix maintainers field — please update mention if maintainership has changed)

    Also note: Please ping the maintainers before ;)

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions