Cannot copy default.el file to Emacs directory

I posted on Reddit but still have no answer, so I post it here to get more exposure

My goal: I want to overwrite cmigemo-dictionary path

My method
Based on instructions from Configuring Emacs, I create a default.el file and let Emacs automatically load it

Here’s my code

{ config, pkgs, ... }:
let
  unstable = import <nixos-unstable> {};
  unstable-pkgs = with pkgs; [
    unstable.cmigemo
  ];
  myNixEmacsConfig = pkgs.writeText "default.el" ''
(setq migemo-dictionary "${unstable.cmigemo}/share/migemo/utf-8")
  '';
  copyNixEmacsConfig = pkgs.runCommand "default.el" {} ''
      mkdir -p $out/share/emacs/site-lisp
      cp ${myNixEmacsConfig} $out/share/emacs/site-lisp/default.el
    '';
  myEmacs = pkgs.emacsWithPackages (epkgs: (with epkgs.melpaStablePackages; [
    copyNixEmacsConfig
  ]));
in
{
...
  programs = {
    emacs = {
      enable = true;
      package = myEmacs;
    };
  };
...

After running home-manager switch, I see that default.el is created at /nix/store/<hash>-default.el with the following content (this is desirable)

(setq migemo-dictionary "/nix/store/5jjpxix76zd2zgj0w235ch39lyw6wc3y-cmigemo-1.3e/share/migemo/utf-8")

However, when I check the Emacs path with the following command, I don’t see any default.el file

ls -ltra $(dirname $(dirname $(readlink -f $(which emacs))))/share/emacs/site-lisp

The output is

-r--r--r-- 1 root root  106 Jan  1  1970 subdirs.el
-r--r--r-- 1 root root 1746 Jan  1  1970 site-start.elc
-r--r--r-- 1 root root 2720 Jan  1  1970 site-start.el
dr-xr-xr-x 4 root root 4096 Jan  1  1970 ..
dr-xr-xr-x 2 root root 4096 Jan  1  1970 .

It looks like default.el isn’t copied to this location

I don’t know what went wrong. Could you please help?

I think everything is working as it should; though maybe not as you expected.

The default.el file ends up in the emacs-packages-deps derivation, not the main emacs-with-packages derivation. If you take a look at the emacs command, it’s a shell script that adds the emacs-packages-deps to emac’s ‘load-path’.

example:

default.nix:

{ pkgs ? import <nixpkgs> {} }:
let defaultEl = pkgs.writeText "default.el" ''
    (message "hello")
  '';
    package = pkgs.runCommand "package" {} ''
      mkdir -p $out/share/emacs/site-lisp
      ln -s ${defaultEl} $out/share/emacs/site-lisp/default.el
    '';
    in
pkgs.emacsWithPackages (epkgs: [ package ])
nix-build

cat result/bin/emacs
#! /nix/store/k8p54jg8ipvnfz435mayf5bnqhw4qqap-bash-4.4-p23/bin/bash -e
export EMACSLOADPATH=$EMACSLOADPATH${EMACSLOADPATH:+':'}'/nix/store/w7pzvpbkpavzjxy0pin2sjpcx1wfcm19-emacs-packages-deps/share/emacs/site-lisp:'
exec "/nix/store/whi80rhpkhawii6cm82j4g05sfnnihjs-emacs-27.1/bin/emacs"  "$@"

cat /nix/store/w7pzvpbkpavzjxy0pin2sjpcx1wfcm19-emacs-packages-deps/share/emacs/site-lisp/default.el
(message "hello")

I also confirmed that this emacs derivation does load the default.el I specified, I got a ‘hello’ in my messages when running ./result/bin/emacs.