How to pass flake input to home-manager module

I want to configure doom-emacs declaratively within my nix flake following ryan4yin’s nix-config (ref). I don’t understand exactly how he’s passing the doomemacs non-flake input to the emacs home-manager module. This is a simplified version of what I’ve tried so far…

flake.nix

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    darwin = {
      url = "github:LnL7/nix-darwin/master";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    doomemacs = {
      url = "github:doomemacs/doomemacs";
      flake = false;
    };
    home-manager.url = "github:nix-community/home-manager";
  };
  outputs =
    { self
    , darwin
    , doomemacs
    , home-manager
    , nixpkgs
    } @inputs:
    {
      darwinConfigurations = {
        macos = darwin.lib.darwinSystem {
          system = "aarch64-darwin";
          specialArgs = inputs;
          modules = [
            home-manager.darwinModules.home-manager
            ./darwin
          ];
        };
      };
    };
}

darwin/default.nix

{ config, pkgs, ... }:

{

  imports = [
    ./home-manager.nix
  ];

  ...
}

darwin/home-manager.nix

{ config, pkgs, lib, home-manager ... }@inputs:

let
  user = "adam";
in
{
  imports = [ ];

  users.users.${user} = {
    name = "${user}";
    home = "/Users/${user}";
    isHidden = false;
    shell = pkgs.zsh;
  };

  # Enable home-manager
  home-manager = {
    useGlobalPkgs = true;
    extraSpecialArgs = { inherit inputs; };
    users.${user} = { pkgs, config, lib, ... }: {
      imports = [
        ../modules/doom-emacs
      ];
      home.stateVersion = "21.11";
    };
  };
}

modules/doom-emacs/default.nix

{
  config,
  lib,
  pkgs,
  doomemacs,
  ...
}:
with lib; let
  envExtra = ''
    export PATH="${config.xdg.configHome}/emacs/bin:$PATH"
  '';
  shellAliases = {
    e = "emacsclient --create-frame"; # gui
    et = "emacsclient --create-frame --tty"; # termimal
  };
  myEmacsPackagesFor = emacs: ((pkgs.emacsPackagesFor emacs).emacsWithPackages (epkgs: [
    epkgs.vterm
  ]));
in {
  options.modules.editors.emacs = {
    enable = mkEnableOption "Emacs Editor";
  };

  config = (mkMerge [
    {
      home.packages = with pkgs; [
        ## Doom dependencies
        git
        ripgrep
        gnutls # for TLS connectivity
        fd # faster projectile indexing
        imagemagick # for image-dired
        fd # faster projectile indexing
      ];

      programs.bash.bashrcExtra = envExtra;
      programs.zsh.envExtra = envExtra;
      home.shellAliases = shellAliases;

      xdg.configFile."doom" = {
        source = ./doom;
        force = true;
      };

      home.activation.installDoomEmacs = lib.hm.dag.entryAfter ["writeBoundary"] ''
        ${pkgs.rsync}/bin/rsync -avz --chmod=D2755,F744 ${doomemacs}/ ${config.xdg.configHome}/emacs/
      '';
    }

    (mkIf pkgs.stdenv.isDarwin (
      let
        # macport adds some native features based on GNU Emacs 29
        # https://bitbucket.org/mituharu/emacs-mac/src/master/README-mac
        emacsPkg = myEmacsPackagesFor pkgs.emacs29;
      in {
        home.packages = [emacsPkg];
        launchd.enable = true;
        launchd.agents.emacs = {
          enable = true;
          config = {
            ProgramArguments = [
              "${pkgs.bash}/bin/bash"
              "-l"
              "-c"
              "${emacsPkg}/bin/emacs --fg-daemon"
            ];
            StandardErrorPath = "${config.home.homeDirectory}/Library/Logs/emacs-daemon.stderr.log";
            StandardOutPath = "${config.home.homeDirectory}/Library/Logs/emacs-daemon.stdout.log";
            RunAtLoad = true;
            KeepAlive = true;
          };
        };
      }
    ))
  ]);
}

And this is what I get when I nix build the flake.

$ nix --experimental-features 'nix-command flakes' build #darwinConfigurations.macos.system
Starting build...
error:
       … while calling the 'derivationStrict' builtin

         at /builtin/derivation.nix:9:12: (source not available)

       … while evaluating derivation 'darwin-system-24.05.20240216.5863c27+darwin4.0e6857f'
         whose name attribute is located at /nix/store/bg5fbkfa5x53clcjf4p5p92k1l3w8x38-source/pkgs/stdenv/generic/make-derivation.nix:353:7

       … while evaluating attribute 'activationScript' of derivation 'darwin-system-24.05.20240216.5863c27+darwin4.0e6857f'

         at /nix/store/zijb0iz5kl0argc40l2fqfai4s8mbj91-source/modules/system/default.nix:95:7:

           94|
           95|       activationScript = cfg.activationScripts.script.text;
             |       ^
           96|       activationUserScript = cfg.activationScripts.userScript.text;

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: attribute 'doomemacs' missing

       at /nix/store/bg5fbkfa5x53clcjf4p5p92k1l3w8x38-source/lib/modules.nix:508:28:

          507|         builtins.addErrorContext (context name)
          508|           (args.${name} or config._module.args.${name})
             |                            ^
          509|       ) (lib.functionArgs f);

It seems the issue is that the doomemacs flake input isn’t available to my doom-emacs module. Any idea how I can fix this?

If I see it correctly you are passing inputs and not doomemacs to the modules, so you either have to change the extra args to inherit (inputs) doomemacs; or use ,inputs in the hm module and then access it via inputs.doomemacs

4 Likes

Ah, I see now. Indeed that fixed my issue! Thanks so much :blush:

1 Like