How to source .emacs config in home-manager

Hi there,

I am trying to use home-manager to manage my .emacs config file.

Right now, I have emac’s enabled with home-manager and for the extraConfig option, i want it to source the actual .emacs file I have stored in my .config directory.

Here is what I have right now, but I’m not sure on how to go about this:

programs.emacs = {
    enable = true;
    extraConfig = ??? (i want to source the file here)
};

I tried using home.file.".config/.emacs".source = ... but again, not sure how exactly to input the path here.

Any help would be greatly appreciated. Please let me know if I should provide more of my configuration files and whatnot. Thanks!

extraConfig requires a path from the store. So your .emacs should be part of your NixOS configuration, and its path should be set relatively, without quotes.

e.g.

extraConfig = ./.emacs;

I’m assuming you mean that the .emacs file should be part of my configuration.nix? If so, I’m confused on how I would do this. Could you show me? And if not, I’m still kinda confused… :sweat_smile:

From what I read from the home-manager doc’s the extraConfig option is a string, so by putting a path there, I get an error:

error: A definition for option `programs.emacs.extraConfig' is not of type `strings concatenated with "\n"'. Definition values:
       - In `/nix/store/8pkdhwffgyw8arz1vn1qdp90g4m72qsy-source/home.nix': /nix/store/8pkdhwffgyw8arz1vn1qdp90g4m72qsy-source/.emacs

Ah, apologies. I misread because I use nix-doom-emacs-unstraightened and it uses file path.

I checked the hm option and it is indeed just a long string. According to this comment, what you might be looking for is builtins.readFile. I tested on repl as below:

~ ❱ echo -en 'foo\nbar' > foobar
~ ❱ cat foobar
foo
bar⏎                                                                                                                                                                       
~ ❱ nix repl
Lix 2.91.0
Type :? for help.
nix-repl> builtins.readFile ./foobar
"foo\nbar"

nix-repl> 
1 Like

Thanks for the reply. I did some playing around with the repl (didn’t know that existed!) and the file is accessible. I put this line in my config like so:

programs.emacs = {
    enable = true;
    extraConfig = builtins.readFile ../config/.emacs;
};

In theory, this should work, but I get this error:

error: access to absolute path '/nix/store/config/.emacs' is forbidden in pure eval mode (use '--impure' to override)

I did some googling and it turns out that the .emacs file should be in the same version controlled directory as the nixos config files, but I’m not really sure on what to do.

My current config file structure is like so:

  • all my nixos config is stored in /home/user/nixos while the emacsn config file
  • all my other config files for that matter are stored in /home/user/.config/.

What would be the best approach here? Do I move my entire nixos config folder to be inside of .config and then version control the entire .config folder or just move the .emacs file to the nixos config folder?

Sorry if I was yapping too much! Thanks again!

Everything should be together in one folder. It doesn’t really matter where that folder is, other than I strongly recommend that you do not make $HOME or $HOME/.config into a flake. (You will waste a lot of time copying files into the store, because the whole path will get copied if you don’t use git repo or such.)
Make a separate directory just for the config code, and put everything into that.

1 Like

Your comment made me look a bit deeper into my nixos config files. It seems I was already doing what you recommended, but I had just forgotten! The folder structure was just taken from a video I watched a while ago! :sweat_smile:

Thanks for your help both of you!