agenix icon indicating copy to clipboard operation
agenix copied to clipboard

Disable `KeepAlive.Crashed` option to prevent unnecessary restarts

Open yzx9 opened this issue 4 months ago • 3 comments

We previously set KeepAlive.Crashed = false. However, this option causes the script to restart automatically after 10 seconds (the minimum restart interval enforced by launchd), even if it has already run successfully.

As a result, you may observe excessive runs:

launchctl print gui/501/org.nix-community.home.activate-agenix | grep runs

This behavior can also be confirmed in the logs:

sudo log show --last boot --predicate "process == 'launchd' AND composedMessage CONTAINS 'org.nix-community.home.activate-agenix'"

Therefore, this PR removes this option to prevent unnecessary restarts.

document
> man launchd.plist | grep Crashed -B42 -A2
KeepAlive <boolean or dictionary of stuff>
This  optional  key  is used to control whether your job is to be kept continuously running or to let demand and conditions
control the invocation. The default is false and therefore only demand will start the job. The value may be set to true  to
unconditionally  keep  the  job  alive.  Alternatively,  a dictionary of conditions may be specified to selectively control
whether launchd keeps a job alive or not. If multiple keys are provided, launchd ORs them, thus providing maximum flexibil‐
ty to the job to refine the logic and stall if necessary. If launchd finds no reason to restart the job, it falls back  on
demand  based invocation.  Jobs that exit quickly and frequently when configured to be kept alive will be throttled to con‐
serve system resources. The use of this key implicitly implies RunAtLoad, causing launchd to speculatively launch the job.

      SuccessfulExit <boolean>
      If true, the job will be restarted as long as the program exits and with an exit status of zero.  If false,  the  job
      will be restarted in the inverse condition.  This key implies that "RunAtLoad" is set to true, since the job needs to
      run at least once before an exit status can be determined.

...

      Crashed <boolean>
      If  true,  the  the  job  will be restarted as long as it exited due to a signal which is typically associated with a
      crash (SIGILL, SIGSEGV, etc.). If false, the job will be restarted in the inverse condition.

close #308

yzx9 avatar Oct 27 '25 07:10 yzx9

Is this only an issue for home-manager users?

$ launchctl print system/org.nixos.activate-agenix | grep runs
        runs = 1

n8henrie avatar Oct 28 '25 12:10 n8henrie

Yes, KeepAlive.Crashed is not enabled in the nix-darwin module.

After some investigation, I found that these two launchd agents use different KeepAlive conditions. This divergence has been there since their introduction in PR #141 and PR #180.

https://github.com/ryantm/agenix/blob/9ba0d85de3eaa7afeab493fed622008b6e4924f5/modules/age.nix#L343-L356

https://github.com/ryantm/agenix/blob/9274b82816ae4450ff13f634113e99a83d1bd4b1/modules/age-home.nix#L219-L226

yzx9 avatar Oct 28 '25 13:10 yzx9

Can confirm this to fix https://github.com/ryantm/agenix/issues/308

srid avatar Dec 04 '25 18:12 srid