Skip to content

Instantly share code, notes, and snippets.

@elliottminns
Last active January 17, 2025 16:56
Show Gist options
  • Save elliottminns/211ef645ebd484eb9a5228570bb60ec3 to your computer and use it in GitHub Desktop.
Save elliottminns/211ef645ebd484eb9a5228570bb60ec3 to your computer and use it in GitHub Desktop.
system.activationScripts.applications.text = let
env = pkgs.buildEnv {
name = "system-applications";
paths = config.environment.systemPackages;
pathsToLink = "/Applications";
};
in
pkgs.lib.mkForce ''
# Set up applications.
echo "setting up /Applications..." >&2
rm -rf /Applications/Nix\ Apps
mkdir -p /Applications/Nix\ Apps
find ${env}/Applications -maxdepth 1 -type l -exec readlink '{}' + |
while read -r src; do
app_name=$(basename "$src")
echo "copying $src" >&2
${pkgs.mkalias}/bin/mkalias "$src" "/Applications/Nix Apps/$app_name"
done
'';
@shaoyanji
Copy link

line 14 needs to be revised to read -r for the latest nix flake update command per: https://www.shellcheck.net/wiki/SC2162

was weird to debug this and happened to recall the syntax of this script since it was more shell than nixlang.

system.activationScripts.applications.text = let
  env = pkgs.buildEnv {
    name = "system-applications";
    paths = config.environment.systemPackages;
    pathsToLink = "/Applications";
  };
in
  pkgs.lib.mkForce ''
  # Set up applications.
  echo "setting up /Applications..." >&2
  rm -rf /Applications/Nix\ Apps
  mkdir -p /Applications/Nix\ Apps
  find ${env}/Applications -maxdepth 1 -type l -exec readlink '{}' + |
  while read -r src; do
    app_name=$(basename "$src")
    echo "copying $src" >&2
    ${pkgs.mkalias}/bin/mkalias "$src" "/Applications/Nix Apps/$app_name"
  done
      '';

@elliottminns
Copy link
Author

line 14 needs to be revised to read -r for the latest nix flake update command per: https://www.shellcheck.net/wiki/SC2162

was weird to debug this and happened to recall the syntax of this script since it was more shell than nixlang.

system.activationScripts.applications.text = let
  env = pkgs.buildEnv {
    name = "system-applications";
    paths = config.environment.systemPackages;
    pathsToLink = "/Applications";
  };
in
  pkgs.lib.mkForce ''
  # Set up applications.
  echo "setting up /Applications..." >&2
  rm -rf /Applications/Nix\ Apps
  mkdir -p /Applications/Nix\ Apps
  find ${env}/Applications -maxdepth 1 -type l -exec readlink '{}' + |
  while read -r src; do
    app_name=$(basename "$src")
    echo "copying $src" >&2
    ${pkgs.mkalias}/bin/mkalias "$src" "/Applications/Nix Apps/$app_name"
  done
      '';

Thank you! Will update the video amendments as well.

@maximkhatskevich
Copy link

line 14 needs to be revised to read -r for the latest nix flake update command per: https://www.shellcheck.net/wiki/SC2162
was weird to debug this and happened to recall the syntax of this script since it was more shell than nixlang.

system.activationScripts.applications.text = let
  env = pkgs.buildEnv {
    name = "system-applications";
    paths = config.environment.systemPackages;
    pathsToLink = "/Applications";
  };
in
  pkgs.lib.mkForce ''
  # Set up applications.
  echo "setting up /Applications..." >&2
  rm -rf /Applications/Nix\ Apps
  mkdir -p /Applications/Nix\ Apps
  find ${env}/Applications -maxdepth 1 -type l -exec readlink '{}' + |
  while read -r src; do
    app_name=$(basename "$src")
    echo "copying $src" >&2
    ${pkgs.mkalias}/bin/mkalias "$src" "/Applications/Nix Apps/$app_name"
  done
      '';

Thank you! Will update the video amendments as well.

Would be even better if you update the code snippet itself, as most people will just go here and copy/paste from the snippet, only to find out it's broken. Thank you for the video!

@elliottminns
Copy link
Author

Done!

@shaoyanji
Copy link

for nix-unstable and your future viewers of your channel 25.05 and later will deprecated this line:

(pkgs.nerdfonts.override { fonts = [ "JetBrainsMono" ]; })

and it will go with this:

pkgs.nerd-fonts.jetbrains-mono

one the the downsides of nix is that reproducibility comes at the price if you want to get the benefits of better and cleaner nix language and have nix flake update command work.

@Djang0
Copy link

Djang0 commented Dec 9, 2024

I used the activation script in my config, but alacritty is still not visible in spotlight :/

@shaoyanji
Copy link

I used the activation script in my config, but alacritty is still not visible in spotlight :/

it does have to be an environment system package for it to be read.

this script works for kitty for example, but doesn't for wezterm because i have it as a home manager module. the creature comfort with spotlight isn't great because aliases are a little lower than a websearch. i use raycast as a spotlight replacement through homebrew.

but what is great about this script is that it gives you an opening to adapt it to add home manager aliases, just replace /Applications/Nix\ Apps to ~/Applications/Home\ Manager (i'm not a fan of bash).

@ricardogaspar2
Copy link

Just a suggestion, I've used this utility on my flake.nix and it worked like a charm. No need to define an activation script
https://github.com/hraban/mac-app-util
Credits to the developer @hraban

Thanks for the video tutorial, it really helped.

@waseemhnyc
Copy link

+1 ☝️ to using @hraban https://github.com/hraban/mac-app-util

Worked like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment