LnL7 / nix-darwin

nix modules for darwin
MIT License
3.19k stars 456 forks source link

fix: Issue 947 set NIX_PROFILES for use-xdg-base-directories #962

Closed bestlem closed 2 weeks ago

bestlem commented 5 months ago

https://github.com/LnL7/nix-darwin/issues/947 -

Puts the directory that nHome Manager uses when xdg is enabled and use-xdg-base-directories is set (~/.local/state/nix/profile/bin) into NIX_PATHS and thus the paths in the shells.

HM uses this directory instead of ~/.nix-profiles in this case

bestlem commented 3 months ago

I hate POSIX shell.

I have now read all the nixes change and the shell manuals.

Do we have PAM for use under Darwin?

If not then the shell check on XDG_STATE_HOME is the correct way.

If not the nixes fix is a mess.

I think I can get my old way to run and see where an error is as the GitHub script does print out the PATH

But I do not see anime error message saying text X failed and what it compares,

bestlem commented 3 months ago

On reviewing more, that means I tried to work it out from bash/zsh documentation rather than copying nixes. NB I use fish and manually set the path so did not notice the latest change as per nixes does not work.

/etc/profile is the first file bash reads on an interactive login shell.

Apple's provided version runs path_helper and then sources /etc/bashrc

This means that XDG_STATE_HOME is not set before the changes here are read.

In zsh easier to follow /etc/zshenv loads the variables here and again is before ~/.zshenv so XDG* are not set.

So I think the change in nixos breaks things. But I have no Linux setup to test.

Even the fix I proposed and was the previous one in nixos only works by convention. That being that XDG_STATE_HOME is in ~/.local/state - If for example I wanted it in ~/Library/Application Support/xdgstate then it would fail.

sellout commented 2 months ago

This means that XDG_STATE_HOME is not set before the changes here are read.

Hrmm, looking at the code matches what you’re saying, but I’m not seeing /nix/profile in my PATH or NIX_PROFILES, so it seems like the XDG_STATE_HOME is set sufficiently early somehow. (Granted, my XDG_STATE_HOME is set to the standard place, and duplicates are removed in both cases, so it’s not that obvious, but I’m pretty sure that nothing is filtering non-existent paths from PATH)

Even the fix I proposed and was the previous one in nixos only works by convention. That being that XDG_STATE_HOME is in ~/.local/state - If for example I wanted it in ~/Library/Application Support/xdgstate then it would fail.

Yeah, this would be bad.

I have been thinking about setting my XDG_*_HOME to more Mac-specific places, so maybe this is the time to try it …

bestlem commented 2 months ago

I think what is the issue is mixing what home-manager sets up and what nixos/nix-darwin sets.

If running home-manager the hm_sessionvars.sh file sets the XDG variables after the code here is run. This is set in ~/.profile that is read by ~/.bash* files. So if as under most Unixes the login shell is read then these are set early. But macOS does not do that.

Another way out is never to run home-manager on its own and always via nix-darwin and with useUserPackages = true then everything is in "/etc/profiles/per-user/$USER" and so the PATHS in ~ have nothing in them.

Or the latter might depend on frameworks - I now use Snowfallorg/lib and the binaries end up in ${XDG_STATE_HOME}/nix/profile I had a previous incomplete one that copied things around so that HM just used the /etc paths.

bestlem commented 2 months ago

As for no /nix/profile you won't get it unless you are using some version of this PR.

nix-darwin justi does not enter any line in PATH for the XDG path - which was the simple thing I tried to fox by copying nixes

But I could not get the test to pass then nixes changed and then I actually looked at the issue and gone down too many rabbit holes.

sellout commented 2 months ago

As for no /nix/profile you won't get it unless you are using some version of this PR.

Yeah, see my earlier https://github.com/LnL7/nix-darwin/pull/962#discussion_r1763516601 – I am using the NixOS approach to this problem.

But I understand this is in some pretty fragile generated shell stuff (and repurposed for other shells, and PAM, etc.) so fingers crossed that there’s something that works in the general case.

bestlem commented 2 weeks ago

As a note macOS does have pam - wether that messes up the PATH I doN'T know.

For me I now have a biometric login (Apple Watch) which uses PAM for sudo so I am now happy to use the /etc/profiles/... PATH and so I don't have to deal with this mess-up of local and global PATHS.

I thus am not working on this.