discord: recreate module symlinks on every launch

As of commit b49b623a ("discord: use distro layout for stable on Linux", PR #515876),
Discord's launch script runs a `discord-stage-modules` script defined in `linux.nix`,
which creates symlinks from `~/.config/discord/<version>/modules/<module_name>`
to the corresponding Nix store paths so Discord can find them.
It checks whether this has already been done by looking for `installed.json` in the same dir,
which it also creates, and skips link creation if so.
However, since the dir is indexed by Discord version number,
the links are not refreshed when opening a new build of Discord with the same version number as an older build.
If the old build is then GC'd, the links break and Discord fails to start
with an error such as `Cannot find module 'discord_desktop_core'`.
Deleting the module folder entirely allows for it to be recreated and for Discord to start without issue.
This problem and solution are attested on the wiki, albeit with less explanation:
https://wiki.nixos.org/wiki/Discord#Crash_on_start-up.
The package fix is to delete and recreate the `modules` folder on each launch,
as it is small and does not contain any data meant to persist between launches:
it has only the module links, `installed.json`, and a `pending/` download directory
recreated by Discord's updater on startup.
The fix ensures that the links always point to the Nix store of the build of Discord being run,
so they will not be broken by GC.

Assisted-by: Claude Fable 5 in Claude Code
(cherry picked from commit 8f5437cf20)
This commit is contained in:
Mathtician 2026-06-11 00:30:46 -05:00 committed by github-actions[bot]
commit ccfb602b88

View file

@ -152,14 +152,13 @@ let
stageModules = writeShellScript "discord-stage-modules" ''
store_modules="$1"
modules_dir="''${XDG_CONFIG_HOME:-$HOME/.config}/${lib.toLower binaryName}/${version}/modules"
if [ ! -f "$modules_dir/installed.json" ]; then
mkdir -p "$modules_dir"
for m in ${lib.concatStringsSep " " (lib.attrNames moduleSrcs)}; do
ln -sfn "$store_modules/$m" "$modules_dir/$m"
done
echo '${builtins.toJSON (lib.mapAttrs (_: mod: { installedVersion = mod; }) moduleVersions)}' \
> "$modules_dir/installed.json"
fi
rm -rf "$modules_dir"
mkdir -p "$modules_dir"
for m in ${lib.concatStringsSep " " (lib.attrNames moduleSrcs)}; do
ln -sn "$store_modules/$m" "$modules_dir/$m"
done
echo '${builtins.toJSON (lib.mapAttrs (_: mod: { installedVersion = mod; }) moduleVersions)}' \
> "$modules_dir/installed.json"
'';
disableBreakingUpdates =