Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions docs/architecture/nixos-passthrough-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Rule to implement first (**done 2026-10-31**, `tendcf_systemd_units_writable` in
container into this layout and proves the refusal): the systemd adapter is supported only where
`/etc/systemd/system` is a real writable directory (not a symlink into a store),
and a present `systemd` entry elsewhere is refused by name, with a message that
says the host's unit writer is `nixos`. Negative test: the same fixture on a
says the host's supervisor is `nixos`. A tombstone there is reported
(`tombstone not actuated`) and never actuated (D3; check 7b). Negative test: the same fixture on a
host where `/etc/systemd/system` is a read-only symlink (a CI container can
bind-mount one) must run no `systemctl` and write nothing.

Expand Down Expand Up @@ -73,8 +74,10 @@ host until stage C.
read-only store symlink, `/var/lib/cfengine-tendcf` exists from tmpfiles, `cf-agent` is
`3.29.0a.b8abf2840`. With `deploy/linux/passthrough/host_specific.json` as CMDB data,
`cf-promises` accepts the policy and `cf-agent -n -K -I` actuates nothing; with the
`systemd-present` fixture it refuses the entry by name with the `nixos` unit-writer
message (PR #20's guard), writes nothing, runs no `systemctl`. One finding: CFEngine's OS
`systemd-present` fixture it refuses the entry by name with the `nixos` refusal
message (PR #20's guard; worded `supervisor nixos` since
https://github.com/frdminc/tendcf/pull/26), writes nothing, runs no `systemctl`. One
finding: CFEngine's OS
detection does not know NixOS (`sys.os_name_human` "Unknown" warning; `sys.flavour` is
`nixos_26`, so classes work): filed as [djbclark/core#56](https://github.com/djbclark/core/issues/56). get keeps the package and the work
directory; nothing is scheduled there. Original text: `nixos-rebuild test` (activates
Expand Down
26 changes: 21 additions & 5 deletions policy/tendcf_services.cf
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,26 @@ bundle agent tendcf_services
# The adapter writes /etc/systemd/system. On NixOS that path is a
# symlink into the read-only store (/etc/static/systemd/system): a
# unit exists there only if the Nix configuration declares it, and
# the host's unit writer is `nixos`, never cfengine
# the host's supervisor is `nixos`, never cfengine
# (linux-targets.md section 5, nixos-passthrough-plan.md section 2).
# Refuse by policy rather than half-fail on the write: a real,
# non-symlink directory is required.
"tendcf_systemd_units_writable"
expression => "tendcf_systemd_units_dir.!tendcf_systemd_units_link";

#
# Operand order is load-bearing (same reason as the header): a class
# expression naming a class not yet defined reads it as false, and a
# class once set is never re-evaluated. With `_writable` above its
# operands, pass 1 set `_elsewhere` on every normal systemd host and
# every present entry was reported "refused" while it was rendered in
# pass 2 (seen in CI, 2026-11-01; present since the guard landed).
"tendcf_systemd_units_dir"
expression => isdir("/etc/systemd/system");

"tendcf_systemd_units_link"
expression => islink("/etc/systemd/system");

"tendcf_systemd_units_writable"
expression => "tendcf_systemd_units_dir.!tendcf_systemd_units_link";

"tendcf_systemd_units_elsewhere"
expression => "linux.tendcf_systemd_booted.!tendcf_systemd_units_writable";

Expand All @@ -199,6 +206,12 @@ bundle agent tendcf_services
"service_$(service_canon[$(service_ids)])_refused"
expression => "service_$(service_canon[$(service_ids)])_present.!service_$(service_canon[$(service_ids)])_supported";

# A tombstone has no unit, so it has no flavour and `_refused` (which
# needs `_present`) cannot see it. Where the units are the system
# configuration's, a tombstone is reported and never actuated (D3).
"service_$(service_canon[$(service_ids)])_tombstone_elsewhere"
expression => "tendcf_systemd_units_elsewhere.!service_$(service_canon[$(service_ids)])_present";

# systemd actuation gate: the entry's own interlock bundle is met.
"service_$(service_canon[$(service_ids)])_systemd_go"
expression => "service_$(service_canon[$(service_ids)])_systemd_supported.!interlock_blocked_$(service_bundle_canon[$(service_ids)])";
Expand Down Expand Up @@ -315,7 +328,10 @@ bundle agent tendcf_services
"tendcf: service '$(service_ids)' refused: unit flavour '$(unit_flavour[$(service_ids)])' is not implemented on this host ($(sys.flavour); adapters: launchd on darwin, systemd on a Linux host booted by systemd)";

"service_$(service_canon[$(service_ids)])_refused.service_$(service_canon[$(service_ids)])_systemd.tendcf_systemd_units_elsewhere"::
"tendcf: service '$(service_ids)' refused: /etc/systemd/system is not a writable directory on this host ($(sys.flavour)); its units are written by the system configuration (unit writer nixos), not by cfengine";
"tendcf: service '$(service_ids)' refused: /etc/systemd/system is not a writable directory on this host ($(sys.flavour)); its units are written by the system configuration (supervisor nixos), not by cfengine";

"service_$(service_canon[$(service_ids)])_tombstone_elsewhere"::
"tendcf: service '$(service_ids)' tombstone not actuated: /etc/systemd/system is not a writable directory on this host ($(sys.flavour)); its units are the system configuration's (supervisor nixos)";
}

body contain tendcf_interlock_contain(timeout)
Expand Down
40 changes: 36 additions & 4 deletions tests/policy/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ if [ "$(uname)" = Linux ] && [ -d /run/systemd/system ]; then
main_pid() { systemctl show -p MainPID --value "$unit"; }

sysd_run systemd-present.json "$work/sysd1.out"
check "no refusal reported on a normal systemd host" \
bash -c "! grep -q \"'$unit' refused\" '$work/sysd1.out'"
check "unit rendered" \
grep -q 'ExecStart="/usr/bin/sleep" "3600"' "$unit_path"
check "Restart and WantedBy rendered from the entry" \
Expand Down Expand Up @@ -220,6 +222,8 @@ if [ "$(uname)" = Linux ] && [ -d /run/systemd/system ]; then
check "enabled:false removed the boot link" bash -c "! systemctl is-enabled --quiet $unit"

sysd_run systemd-absent.json "$work/sysd4.out"
check "tombstone_elsewhere report absent on a normal systemd host" \
bash -c "! grep -q 'tombstone not actuated' '$work/sysd4.out'"
check "tombstone removed the unit file" bash -c "[ ! -e '$unit_path' ]"
check "tombstone stopped the unit" bash -c "! systemctl is-active --quiet $unit"
check "tombstone reloaded the manager (unit not found)" \
Expand All @@ -238,11 +242,20 @@ else
grep -q "tendcf: service '$unit' refused: unit flavour 'systemd' is not implemented on this host" "$out"
check "nothing rendered under /etc/systemd/system" bash -c "[ ! -e '$unit_path' ]"
check "no systemctl invocation" no_systemctl "$out"

# A tombstone on a host that is not booted by systemd (Darwin, a
# container) is not the NixOS layout: no report, nothing run.
cp "$repo/tests/policy/fixtures/systemd-absent.json" "$work/data/host_specific.json"
out="$work/sysd-host-tomb.out"
"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true
check "tombstone_elsewhere report absent on a host not booted by systemd" \
bash -c "! grep -q 'tombstone not actuated' '$out'"
check "no systemctl invocation for the tombstone" no_systemctl "$out"
fi

# 7. NixOS shape: booted by systemd but /etc/systemd/system is a symlink into a
# read-only tree (nixos-passthrough-plan.md section 1). The adapter must refuse
# by policy, naming the unit writer, and run no systemctl. Reshapes the host,
# by policy, naming the supervisor, and run no systemctl. Reshapes the host,
# so only on request and as root (the debian-13 container in CI); restored on exit.
if [ "${TENDCF_TEST_NIXOS_SHAPE:-}" = 1 ]; then
echo "# 7. NixOS shape: /etc/systemd/system a read-only store symlink"
Expand All @@ -264,19 +277,38 @@ if [ "${TENDCF_TEST_NIXOS_SHAPE:-}" = 1 ]; then
cp "$repo/tests/policy/fixtures/systemd-present.json" "$work/data/host_specific.json"
out="$work/nixos-shape.out"
"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true
check "systemd entry refused naming the nixos unit writer" \
grep -q "tendcf: service '$unit' refused: /etc/systemd/system is not a writable directory on this host .*unit writer nixos" "$out"
check "systemd entry refused naming the nixos supervisor" \
grep -q "tendcf: service '$unit' refused: /etc/systemd/system is not a writable directory on this host .*supervisor nixos" "$out"
check "generic flavour refusal not also reported" \
bash -c "! grep -q \"'$unit' refused: unit flavour\" '$out'"
check "nothing written into the store-like unit directory" \
bash -c "[ -z \"\$(ls -A '$shape/static/systemd/system')\" ]"
check "no systemctl invocation" no_systemctl "$out"

# 7b. A tombstone has no unit and no flavour, so the refusal above
# cannot see it: it must be reported and never actuated (D3). A unit
# file stands in the store-like tree first, as if the Nix configuration
# declared it, so a teardown that wrongly ran would have something to
# remove (root ignores the a-w bits; the symlink is what the policy reads).
echo "# 7b. NixOS shape: tombstone reported, not actuated"
printf '[Unit]\nDescription=declared by the system configuration\n' \
>"$shape/static/systemd/system/$unit"
cp "$repo/tests/policy/fixtures/systemd-absent.json" "$work/data/host_specific.json"
out="$work/nixos-tombstone.out"
"$cf_agent" -K -I -f "$work/inputs/entry.cf" >"$out" 2>&1 || true
check "tombstone reported as not actuated, naming the nixos supervisor" \
grep -q "tendcf: service '$unit' tombstone not actuated: /etc/systemd/system is not a writable directory on this host .*supervisor nixos" "$out"
check "tombstone is not reported as a refusal" \
bash -c "! grep -q \"'$unit' refused\" '$out'"
check "the configuration's unit file is left in place (tombstone)" \
bash -c "[ -f '$shape/static/systemd/system/$unit' ]"
check "no systemctl invocation (tombstone)" no_systemctl "$out"
unshape; trap - EXIT
fi
fi

if [ "$fail" != 0 ]; then
for f in refused satisfied flavour host sysd-host sysd1 sysd2 sysd3 sysd4 sysd5 nixos-shape; do
for f in refused satisfied flavour host sysd-host sysd-host-tomb sysd1 sysd2 sysd3 sysd4 sysd5 nixos-shape nixos-tombstone; do
[ -f "$work/$f.out" ] || continue
echo; echo "--- cf-agent output ($f) ---"; cat "$work/$f.out"
done
Expand Down
Loading