Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use wait4path with command and script launchd options #1052

Merged
merged 1 commit into from
Sep 22, 2024

Conversation

will-lol
Copy link
Contributor

Addresses my recent issue, #1043.

I've attempted to make this compatible with both the 'command' and 'script' options of 'launchd.daemons.'. This works fine on my machine, but some tests that check the launchd plist files fail because this will change the format of them slightly.

I thought I would put this first commit here in case anyone had any objections before I went ahead and rewrote all of the tests and modules to not use wait4path anymore.

@Samasaur1
Copy link
Contributor

I wonder if you still want to exec the actual command, which appears to be the previous behavior but looks like you removed it

modules/launchd/default.nix Outdated Show resolved Hide resolved
modules/launchd/default.nix Outdated Show resolved Hide resolved
@niklasravnsborg
Copy link
Contributor

I also had a similar problem as @will-lol described in the #1043 issue but with nextdns:

The service didn't start after reboot and the service state was stuck at spawn scheduled (sudo launchctl print system/org.nixos.nextdns). I now applied this refactor to my nix-darwin fork and now everything works like a charm.

I had to refactor the nextdns service to use command instead of serviceConfig.ProgramArguments in niklasravnsborg@697115e. Do I see it correctly that all services using launchd.daemons have to be refactored in a similar fashion?

@will-lol
Copy link
Contributor Author

will-lol commented Sep 8, 2024

Hi @niklasravnsborg. I believe so. I've refactored some modules that were using wait4path hack to use 'command' or 'script' instead. I am now working on getting tests to pass. I could include refactors of other modules that are manually using serviceConfig.ProgramArguments also. It may also be best for module maintainers to exercise their own judgement.

@niklasravnsborg
Copy link
Contributor

@will-lol If you want you can also pull in my nextdns fix here :) I'm looking forward to not maintaining my own fork anymore :D

@emilazy
Copy link
Collaborator

emilazy commented Sep 8, 2024

We could implement our own programArguments that uses lib.escapeShellArgs to construct an equivalent shell command, since the list interface is nicer and it would mean fewer changes to services.

@will-lol
Copy link
Contributor Author

will-lol commented Sep 8, 2024

All of the tests broken by this change are now fixed. I wonder if we'd prefer to deal with fixing problems with other modules that aren't yet using /bin/wait4path in some fashion in a separate issue or PR?
Also: many of the modules I migrated over from ProgramArguments and /bin/wait4path I do not use myself, and have not yet confirmed to be working on bare metal.
If required I'll work on that next weekend and hopefully we can get this merged. Thanks all!

serviceConfig.ProgramArguments = mkIf (cmd != "") [
"/bin/sh"
"-c"
"/bin/wait4path /nix/store && exec ${cmd}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any special reason, you used exec here? I think we could just drop it and it would make extending the cmd string easier, because one could still use basic shell syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using 'exec' was the previous behaviour. I also received a comment (#1052 (comment)) suggesting that I add it back.

I'm now of the view that chaining commands together with any special shell syntax belongs in a script. The shell that is used to execute the program arguments is also not obvious to the user of the abstraction. By using a script one would expect (by nix convention) that you are getting bash, which also has some extended syntax not supported by /bin/sh.

Totally open to anything else tho

@will-lol
Copy link
Contributor Author

will-lol commented Sep 21, 2024

Manual testing of the refactors I made that I've done on my machine:

@niklasravnsborg
Copy link
Contributor

@will-lol Do you know why some of the tests are broken?

@will-lol
Copy link
Contributor Author

Hah not totally sure.. I can't really see a reason why it failed that time. The error suggests hello.nix wasn't copied or checked into git? All checks are passing now.

Copy link
Collaborator

@Enzime Enzime left a comment

Choose a reason for hiding this comment

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

Can you squash all the commits into one commit?

modules/launchd/default.nix Outdated Show resolved Hide resolved
modules/launchd/default.nix Outdated Show resolved Hide resolved
modules/nix/linux-builder.nix Outdated Show resolved Hide resolved
modules/services/nix-daemon.nix Outdated Show resolved Hide resolved
modules/services/nix-optimise/default.nix Outdated Show resolved Hide resolved
@Enzime
Copy link
Collaborator

Enzime commented Sep 21, 2024

I wonder if the real fix is to get all users to migrate to an unencrypted Nix Store as it should still be encrypted automatically by FileVault see: DeterminateSystems/nix-installer#753 (comment)

It'll probably require users to uninstall nix-darwin then Nix and then reinstall both of them

@emilazy
Copy link
Collaborator

emilazy commented Sep 22, 2024

I personally use an encrypted store. An unencrypted store is not protected in the same way that FileVault volumes are, even though the storage is encrypted at rest; you can access such volumes in macOS Recovery without authentication. My understanding is that full encryption is also a hard requirement for many enterprise users.

I hope we can move towards a more structured interface per #1052 (comment), but this seems like an improvement on the status quo.

modules/services/karabiner-elements/default.nix Outdated Show resolved Hide resolved
modules/services/tailscale.nix Outdated Show resolved Hide resolved
Copy link
Collaborator

@Enzime Enzime left a comment

Choose a reason for hiding this comment

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

Other than that it looks fine to me

modules/services/karabiner-elements/default.nix Outdated Show resolved Hide resolved
addresses LnL7#1043

fix: use exec in launchd daemon config

fix: dont use a script thats in the nix store

fix: remove manual wait4path in linux-builder

fix: remove manual wait4path in karabiner elements

fix: remove manual wait4path in nix-daemon

fix: remove manual wait4path in nix-optimise

fix: remove manual wait4path in tailscaled

fix: autossh test

Revert "fix: remove manual wait4path in nix-daemon"

This reverts commit 6aec084.

fix: remove bad exec

Reapply "fix: remove manual wait4path in nix-daemon"

This reverts commit c8f136e.

fix: update autossh test

to reflect changes in f86e613

fix: services-activate-system-changed-label-prefix test

fix: services-buildkite-agent test

fix: services-activate-system test

fix: escape ampersand

fix: services-lorri test

fix: services-nix-optimise test

fix: services-nix-gc test

refactor: use script rather than command in daemon

fix: use config.command for clarity

style: fix indentation

fix: use lib.getExe rather than directly pointing to file

revert: a87fc7b

- mistaken refactor meant that service waited for nix store and not the relevant path
Copy link
Collaborator

@Enzime Enzime left a comment

Choose a reason for hiding this comment

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

LGTM

Thanks for the contribution

@will-lol will-lol closed this Sep 22, 2024
@will-lol will-lol reopened this Sep 22, 2024
@niklasravnsborg
Copy link
Contributor

niklasravnsborg commented Sep 22, 2024

I wonder if the real fix is to get all users to migrate to an unencrypted Nix Store as it should still be encrypted automatically by FileVault see: DeterminateSystems/nix-installer#753 (comment)

@Enzime I think even an unencrypted store might suffer the same problem when using in combination with launchd services, since the OS might try to start those services before the store is mounted (while the mounting itself is done in a launchd service).

@niklasravnsborg
Copy link
Contributor

@will-lol Do I see correctly there some other services missing regarding this update to use the script, right? How should we proceed on those? Merge this first and wait for module maintainers to move their logic over as well?

I personally just noticed it regarding nextdns and would be available to update and test it there.

# For unknown reasons this daemon will fail if VirtualHIDDeviceClient is not exec'd.
"/bin/wait4path /nix/store && exec \"${pkgs.karabiner-elements.driver}/Library/Application Support/org.pqrs/Karabiner-DriverKit-VirtualHIDDevice/Applications/Karabiner-DriverKit-VirtualHIDDeviceClient.app/Contents/MacOS/Karabiner-DriverKit-VirtualHIDDeviceClient\""
];
command = "${pkgs.karabiner-elements.driver}/Library/Application Support/org.pqrs/Karabiner-DriverKit-VirtualHIDDevice/Applications/Karabiner-DriverKit-VirtualHIDDeviceClient.app/Contents/MacOS/Karabiner-DriverKit-VirtualHIDDeviceClient";
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it intentional that just this one uses command and the other ones use script?

@Enzime
Copy link
Collaborator

Enzime commented Sep 22, 2024

Tested this locally and looks like it's working

@Enzime Enzime merged commit f592752 into LnL7:master Sep 22, 2024
12 checks passed
@utkarshgupta137
Copy link

For those of us who pinned nixpkgs to an earlier version to use Karabiner 14.3, this breaks it.

@ofalvai
Copy link

ofalvai commented Sep 25, 2024

Came here to investigate the same issue. My nix-darwin follows nixpkgs-stable to keep using Karabiner 14.3 and updating nix-darwin to the latest version breaks Karabiner. Rolling back to 6374cd7e50aa057a688142eed2345083047ad884 makes it work again.

@Enzime
Copy link
Collaborator

Enzime commented Sep 25, 2024

See #1092

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants