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

Automatically link macOS applications to ~/Applications #956

Closed
mpscholten opened this issue Jul 3, 2016 · 24 comments
Closed

Automatically link macOS applications to ~/Applications #956

mpscholten opened this issue Jul 3, 2016 · 24 comments
Assignees
Labels
macos Nix on macOS, aka OS X, aka darwin

Comments

@mpscholten
Copy link
Contributor

(previously: NixOS/nixpkgs#16455)

Currently, when installing macOS GUI applications like iTerm2 or Sequel Pro, you always have to manually symlink the applications from ~/.nix-profile/Applications to ~/Applications. This is bad user experience. E.g. homebrew cask can automatically do this for me.

Maybe we can find a way to do this automatically if the host OS is macOS. Let me know what you think about this idea :)

@joelmo
Copy link
Contributor

joelmo commented Jul 3, 2016

Maybe this could be supported by nix-env, running a hook script placed under the nix-support folder.

@mpscholten
Copy link
Contributor Author

Great suggestion :) Is there already a simple hook system in nix-env? Otherwise I'd try to build a very simple prototype for this in the next days.

@joelmo
Copy link
Contributor

joelmo commented Jul 3, 2016

I can't see any such system in nix-env. I think it should be safe to make one as long as the system can be disabled.

@veprbl
Copy link
Member

veprbl commented Jul 4, 2016

Can't you:

mv ~/.nix-profile/Applications/* ~/Applications/
rmdir ~/.nix-profile/Applications
ln -s ~/Applications ~/.nix-profile/Applications

@mpscholten
Copy link
Contributor Author

On the next nix-env -i the whole ~/.nix-profile will be rebuilt thus loosing the ~/Applications symlink. So that's not going to work.

@Sean1708
Copy link

Just FYI guys, this is far more complicated than you might expect and I would strongly suggest reading that thread and all the links in it in their entirety before making a change like this.

@matthewbauer
Copy link
Member

matthewbauer commented Nov 25, 2016

So I've been using a script to do this available here: https://raw.githubusercontent.com/matthewbauer/macNixOS/master/link-apps.sh.

Proposal

Add post-create-profile-hook as a config option to Nix.

Users specify a script or executable to run after nix has successfully completed "createUserEnv", meaning that software installed by nix-env has been successfully added to the user profile. This script could look through $HOME/.nix-profile and do things to make the created derivations discoverable by the operating system. This hook should not be used to modify the profile in any way.

This hook is designed for non-NixOS systems that need to do some extra things after the $HOME/.nix-profile is created. The main use case is to link up Mac app bundles into ~/Applications/ but the idea is to make it generic enough that it could be used on any non-NixOS operating system.

This should be fairly easy to implement. It would work similarly configuration-wise to the "preBuildHook". The actual script execution would probably be done immediately after line 148 in src/nix-env/user-env.cc.

@mpscholten
Copy link
Contributor Author

Great suggestion. If it's easier to implement we could also just hard code the hook for now (so the user cannot change the hook). Then it just would be something like system("<path to nix installation>/scripts/post-create-profile-hook") in the user-env.cc.

Just got the idea for another very cool use case: We could also use the hook to automatically create /etc/paths.d/nix on macOS if it not exists. This way the nix profile is added to the PATH automatically. This way installing nix on macOS, is really just the curl ... | bash and nothing more (currently you still have to manually add the nix profile to the path).

Also thanks for sharing your script. I was already using a similar script, but it was never removing the old dead symlinks. Going to replace my script with yours 👍

@matthewbauer
Copy link
Member

Just got the idea for another very cool use case: We could also use the hook to automatically create /etc/paths.d/nix on macOS if it not exists. This way the nix profile is added to the PATH automatically. This way installing nix on macOS, is really just the curl ... | bash and nothing more (currently you still have to manually add the nix profile to the path).

I think that would be better as just an install step - there's no reason to rerun that each time it regenerates the profile. We can set /etc/paths.d/nix to /nix/var/profiles/default in a custom installer.

@NightMachinery
Copy link

Any updates?

@stale
Copy link

stale bot commented Feb 15, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Feb 15, 2021
@gpanders
Copy link

I am also interested in a way to do this. Are there any updates? The linked issues also seem to be unresolved/stale.

@stale stale bot removed the stale label Mar 21, 2021
@Atemu
Copy link
Member

Atemu commented Mar 21, 2021

There is a solution at the bottom of this: nix-community/home-manager#1341

@SansGuidon
Copy link

There is a solution at the bottom of this: nix-community/home-manager#1341

Thanks @Atemu, I have a newbie question : how to use your solution in practice? I'm not using home-manager yet, and I'm totally new to Nix.

@Atemu
Copy link
Member

Atemu commented Jul 16, 2021

@MorganGeek just add the file to your imports. If you need help with that, make a post in the Discourse and ping me.

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/mac-applications-installed-by-nix-are-not-loaded-by-spotlight/14129/1

@stale
Copy link

stale bot commented Apr 17, 2022

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Apr 17, 2022
@cherryblossom000
Copy link

This issue is still relevant. I would love to stop using Homebrew, but currently still use it for installing casks/macOS applications.

@stale stale bot removed the stale label Jul 6, 2022
@fricklerhandwerk
Copy link
Contributor

I think the general problem can and should be addressed by external tools wrapping Nix, such as done here with home-manager.

@edolstra @domenkozar objections to closing this issue?

@fricklerhandwerk fricklerhandwerk added the macos Nix on macOS, aka OS X, aka darwin label Oct 17, 2022
@hraban
Copy link
Member

hraban commented Dec 29, 2022

If anything, I would advise aliases rather than symlinks. Spotlight picks up aliases. I use something like this (warning: untested):

# Install all nix top level graphical apps
if [[ -d ~/.nix-profile/Applications ]]; then
	(cd ~/.nix-profile/Applications;
	for f in *.app ; do
		mkdir -p ~/Applications/
		rm -f "$HOME/Applications/$f"
		# Mac aliases don’t work on symlinks
		f="$(readlink -f "$f")"
		# Use Mac aliases because Spotlight doesn’t like symlinks
		osascript -e "tell app \"Finder\" to make new alias file at POSIX file \"$HOME/Applications\" to POSIX file \"$f\"" ; done
	done
	)
fi

That being said: it's not without drawbacks. If possible, it's strongly advised to find a way to cp -r the entire .app folder, as homebrew does. Homebrew used to use links, and eventually changed course.

I do use an alias for Emacs.app though, and it's fine. The nixpkgs build of emacs breaks when you cp it to ~/Applications :/ (but that's a bug report for another time)

@fricklerhandwerk
Copy link
Contributor

fricklerhandwerk commented Jan 10, 2023

Discussed in Nix team meeting 2023-01-06:

  • @fricklerhandwerk: as written above I think this should be closed as wontfix, it is outside of Nix's responsibilities. should be done by NixOS, nix-darwin, Home Manager.
    • @thufschmitt: we could do it if there was a lightweight Nix-y way
  • agreement, won't fix

@fricklerhandwerk fricklerhandwerk closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2023
@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2023-01-06-nix-team-meeting-minutes-21/24573/1

@jost-s
Copy link

jost-s commented Apr 28, 2023

@fricklerhandwerk The issue was moved here from the Nixpkgs repo with the rationale that Nixpkgs doesn't have enough write permissions and that it's very similar to managing ~/.nix-profile
NixOS/nixpkgs#16455 (comment)

So where should it go from here, you think? Not every Nix user under macOS uses nix-darwin, so probably home-manager?

@fricklerhandwerk
Copy link
Contributor

Either should do in principle. But conversely one can use nix-darwin without Home Manager, so probably both have to support that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
macos Nix on macOS, aka OS X, aka darwin
Projects
Archived in project
Development

No branches or pull requests